首页 > 代码库 > Java实现Socket通信

Java实现Socket通信

一对一通信:

服务器端:

public class ServerDemo {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
BufferedReader reader = null;
try {
//设置服务端的端口号
serverSocket = new ServerSocket(8081);
//等待客户端发送请求
socket = serverSocket.accept();
//接收客户端发来的请求
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
boolean flag = true;
while (flag){
String read = reader.readLine();
if("结束".equals(read)){
flag = false;
}
System.out.println("服务器接收消息:"+read);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
reader.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


客户端:
public class ClientDemo {
public static void main(String[] args) {
Socket socket = null;
//使用PrintWriter时,调用println、print的方法时,可以不再调用flush方法进行刷新
PrintWriter writer = null;
try {
//客户端socket指定服务器的地址和端口号
socket = new Socket("127.0.0.1", 8081);
//发送消息到服务器
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())));
for (int i = 0; i < 10; i++) {
writer.println("发送消息 " + i);
}
writer.println("结束");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
System.out.println("close......");
writer.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

多客户端通信
服务端1:
public class ServerDouble {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
try {
//设置服务端的端口号
serverSocket = new ServerSocket(8081);
//等待客户端发送请求
System.out.println("正在等待连接……");
while (true){
socket = serverSocket.accept();
System.out.println("连接上了:"+socket.getPort());
new ServerDoubleDemo(socket);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务端2:
public class ServerDoubleDemo extends Thread {
Socket socket = null;
BufferedReader reader = null;
public ServerDoubleDemo(Socket socket) {
this.socket = socket;
try {
reader = new BufferedReader(new InputStreamReader( socket.getInputStream()));
start();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void run() {
try {
boolean flag = true;
while (flag){
String str = reader.readLine();
if("结束".equals(str)){
flag = false;
}
System.out.println("接收消息:"+str);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
reader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

客户端同上:
public class ClientDemo {
public static void main(String[] args) {
Socket socket = null;
//使用PrintWriter时,调用println、print的方法时,可以不再调用flush方法进行刷新
PrintWriter writer = null;
try {
//客户端socket指定服务器的地址和端口号
socket = new Socket("127.0.0.1", 8081);
//发送消息到服务器
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())));
for (int i = 0; i < 10; i++) {
writer.println("发送消息 " + i);
}
writer.println("结束");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
System.out.println("close......");
writer.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}




Java实现Socket通信