Socket编程之TCP/UDP
程序员文章站
2022-07-14 10:04:49
...
1:
TCP编程(一对多):
服务器端:
package com.sqtoon.socket.tcp;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpSocketServer {
public static final int TCP_PORT = 8888;
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(TCP_PORT);
System.out.println("启动服务器, 等待客户端连接...");
while (true) {
final Socket socket = serverSocket.accept();
new Thread(new Runnable() {
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
@Override
public void run() {
try {
while (true) {
dataInputStream = new DataInputStream(socket.getInputStream());
String msg = dataInputStream.readUTF(); // 接收客户端消息
System.out.println("接收到Client发送过来的消息: " + msg);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeUTF("服务器已处理消息: " + msg); // 返回给客户端消息
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dataInputStream.close();
dataOutputStream.close();
socket.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}).start();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端:
package com.sqtoon.socket.tcp;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.UUID;
public class TcpSocketClient {
private static final String uuid = UUID.randomUUID().toString();
public static void main(String[] args) {
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket("127.0.0.1", TcpSocketServer.TCP_PORT);
while (true) {
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeUTF(uuid + "这是客户端发给服务器的消息..." + System.currentTimeMillis());
dataInputStream = new DataInputStream(socket.getInputStream());
String msg = dataInputStream.readUTF();
System.out.println("接收服务器返回的消息: " + msg);
Thread.sleep(1000 * 3);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dataOutputStream.close();
dataInputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2:
UDP编程(一对一):
服务器端:
package com.sqtoon.socket.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UdpSocketServer {
public static final int UDP_PORT = 6666;
public static final String CHATSET = "UTF-8";
public static void main(String[] args) {
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket(UDP_PORT);
byte[] buf = new byte[0];
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
while (true) {
buf = new byte[UdpSocketClient.BUF_LENGTH];
datagramPacket.setData(buf);
datagramSocket.receive(datagramPacket);
String msg = new String(buf, CHATSET);
System.out.println("接收到客户端发送的消息: " + msg);
datagramPacket.setSocketAddress(new InetSocketAddress("127.0.0.1", 5555));
datagramPacket.setData(("服务器处理的消息: " + msg).getBytes(CHATSET));
datagramSocket.send(datagramPacket);
datagramPacket.setData(buf);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
datagramSocket.close();
}
}
}
客户端:
package com.sqtoon.socket.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UdpSocketClient {
public static final int UDP_PORT = 5555;
public static final int BUF_LENGTH = 1024;
public static void main(String[] args) {
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket(UDP_PORT);
byte[] buf = new byte[0];
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
while (true) {
buf = "客户端的消息".getBytes(UdpSocketServer.CHATSET);
datagramPacket.setData(buf);
datagramPacket.setSocketAddress(new InetSocketAddress("127.0.0.1", UdpSocketServer.UDP_PORT));
datagramSocket.send(datagramPacket);
buf = new byte[BUF_LENGTH];
datagramPacket.setData(buf, 0, buf.length);
datagramSocket.receive(datagramPacket);
String msg = new String(buf, UdpSocketServer.CHATSET);
System.out.println("接收到服务器端返回的消息: " + msg.length() + msg);
Thread.sleep(1000 * 2);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
datagramSocket.close();
}
}
}
上一篇: python模块------psutil
下一篇: SOCKET 编程TCP/IP、UDP