1 UDP通信
客户端UDPClient
public class UDPClient {
public static void main(String[] args) throws IOException {
// 获取本地服务器地址
InetAddress server_address = InetAddress.getLocalHost();
// 创建数据报套接字以连接到服务器
DatagramSocket client = new DatagramSocket();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("please enter:");
String s = sc.nextLine();
if (s.equals("exit")) {
break;
}
byte[] bytes = s.getBytes();
// 创建数据报包以发送消息到服务器
DatagramPacket sendPacket = new DatagramPacket(bytes, bytes.length, server_address, 8080);
client.send(sendPacket);
}
}
}
服务端UDPSerever
public class UDPServer {
public static void main(String[] args) throws IOException {
// 创建一个数据报套接字,并将其绑定到端口8080,用于监听来自客户端的请求
DatagramSocket server = new DatagramSocket(8080);
byte[] buffer = new byte[1024];
// 创建一个数据报包,用于接收客户端发送的数据,指定接收缓冲区和缓冲区长度
DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
while (true) {
// 接收数据报包中的数据
server.receive(receivePacket);
int len = receivePacket.getLength();
String data = new String(receivePacket.getData(), 0, len);
String ip = receivePacket.getAddress().getHostAddress();
int port = receivePacket.getPort();
System.out.println("Received: " + "[ip " + ip + " port " + port + "] " + data);
}
}
}
2 TCP通信
客户端TCPClient
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 9999);
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("please enter:");
String s = scanner.nextLine();
if (s.equals("exit")) {
socket.close();
break;
}
dos.writeUTF(s);
dos.flush();
}
}
}
服务端TCPServer
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
DataInputStream dis = new DataInputStream(is);
while (true) {
String s = dis.readUTF();
System.out.println("ip " + socket.getInetAddress().getHostAddress() + " port "+ socket.getPort() + " " + s);
System.out.println("--------------");
}
}
}
服务端TCPServerMultiThread(多线程)
public class TCPServerMultiThread {
public static void main(String[] args) throws IOException {
// 主线程负责接收客户端连接
ServerSocket serverSocket = new ServerSocket(9999);
while(true){
Socket accept = serverSocket.accept();
System.out.println("online-UP: ip " + accept.getInetAddress().getHostAddress() + " port "+ accept.getPort());
// 创建子线程并启动
new ServerThread(accept).start();
}
}
}
class ServerThread extends Thread{
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
InputStream is = socket.getInputStream();
DataInputStream dis = new DataInputStream(is);
while (true) {
String s = dis.readUTF();
System.out.println("ip " + socket.getInetAddress().getHostAddress() + " port "+ socket.getPort() + " " + s);
System.out.println("--------------");
}
} catch (IOException e) {
System.out.println("offline-DOWN: ip " + socket.getInetAddress().getHostAddress() + " port "+ socket.getPort());
e.printStackTrace();
}
}
}