Java实现TCP Socket和UDP Socket是网络编程中非常常见的任务。在此,我将向你介绍Java实现TCP和UDP Socket的完整攻略。
TCP Socket
步骤
Java实现TCP Socket的步骤如下:
1.创建一个ServerSocket对象,绑定IP地址和端口号
ServerSocket serverSocket = new ServerSocket(port, backlog, InetAddress.getByName(ip));
2.调用accept()方法监听客户端请求,并返回一个新的Socket对象
Socket socket = serverSocket.accept();
3.根据Socket对象获取输入输出流进行通信
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
4.使用输入输出流进行通信(收发消息)
// 发送消息
writer.println("Hello, World!");
writer.flush();
// 接收消息
String message = reader.readLine();
5.关闭Socket以及ServerSocket
socket.close();
serverSocket.close();
示例说明
下面提供一个简单的示例,实现一个TCP Socket客户端和服务器端的简单通信(以字符串形式发送和接收消息):
服务器端
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888, 50, InetAddress.getByName("127.0.0.1"));
System.out.println("Server started!");
Socket socket = serverSocket.accept();
System.out.println("Client connected: " + socket.getInetAddress().getHostAddress());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
String message = "";
while (!message.equals("exit")) {
message = reader.readLine();
System.out.println("Received message: " + message);
writer.println("Echo: " + message);
writer.flush();
}
socket.close();
serverSocket.close();
}
}
客户端
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 8888);
System.out.println("Connected to server!");
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
String message = "";
Scanner sc = new Scanner(System.in);
while (!message.equals("exit")) {
System.out.print("Enter message: ");
message = sc.nextLine();
writer.println(message);
writer.flush();
String reply = reader.readLine();
System.out.println("Received reply: " + reply);
}
socket.close();
}
}
UDP Socket
步骤
Java实现UDP Socket的步骤如下:
1.创建一个DatagramSocket对象(可以指定本地端口)
DatagramSocket socket = new DatagramSocket(port);
2.发送数据报文(数据+目标地址)
byte[] buf = "Hello, World!".getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getByName(ip), port);
socket.send(packet);
3.接收数据报文
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
4.关闭DatagramSocket
socket.close();
示例说明
下面提供一个简单的示例,实现一个UDP Socket客户端和服务器端的简单通信(以字符串形式发送和接收消息):
服务器端
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);
System.out.println("Server started!");
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
while (true) {
socket.receive(packet);
String message = new String(buf, 0, packet.getLength());
System.out.println("Received message: " + message);
// echo back
byte[] replyBuf = ("Echo: " + message).getBytes();
DatagramPacket replyPacket = new DatagramPacket(replyBuf, replyBuf.length, packet.getAddress(), packet.getPort());
socket.send(replyPacket);
}
// socket.close();
}
}
客户端
public class UDPClient {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
System.out.println("Connected to server!");
String message = "";
Scanner sc = new Scanner(System.in);
while (!message.equals("exit")) {
System.out.print("Enter message: ");
message = sc.nextLine();
byte[] buf = message.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 8888);
socket.send(packet);
byte[] replyBuf = new byte[1024];
DatagramPacket replyPacket = new DatagramPacket(replyBuf, replyBuf.length);
socket.receive(replyPacket);
String reply = new String(replyBuf, 0, replyPacket.getLength());
System.out.println("Received reply: " + reply);
}
socket.close();
}
}
以上就是Java实现TCP Socket和UDP Socket的完整攻略以及两个示例。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java实现TCP socket和UDP socket的实例 - Python技术站