Table of contents
Create various parameters for Server
Implement specific Server content
Implement specific Client content
Purpose
We write a udp version of echo server and echo client
After sending any message through the client on your own computer, the server will receive and send the message
Return the content unchanged without doing any processing.
first step
Create two java files, the commands are Server and Client
Write Server(server)
We must first understand that in the network, the server must be started first. Only when the server starts first can our client send instructions to the server.
So let's go into the Server.java file first and start writing our code
Step 2
Create various parameters for Server
First to operate the network, we need a package called DatagramSocket provided by Java.
Then when creating an object and instantiating the object, we need to manually configure a port for it
third step
Implement specific Server content
The following is the specific meaning of each code
the fourth step
Write Client(client)
Implement specific Client content
First we still create a Socket to manipulate data
Here’s a look at the difference between the two
The following is the specific meaning of each code
Overall process
Let’s go over the overall process again
1. The server creates a DatagramPacket package and waits for message delivery from the client (blocking if there is no message)
2. The client attaches its own IP address through the corresponding port number, packages the message and sends it to the server for him to process the message.
3. The server accepts the message package, processes the message, repackages the processed message, and returns it to the client with the IP address.
4. The client creates a new package and receives the processed data.
total code
Server
import java.net.DatagramSocket;
import java.net.SocketException;
public class MyServer {
private DatagramSocket socket = null;
MyServer(int port) throws SocketException {
this.socket = new DatagramSocket(port);
}
public void start() throws IOException {
System.out.println("服务器启动!");
while(true){
DatagramPacket requestPacket = new DatagramPacket(new byte[4096],4096);
socket.receive(requestPacket);
String request = new String(requestPacket.getData(),0, requestPacket.getLength());
String response = process(request);
DatagramPacket responsePacket = new DatagramPacket(response.getBytes(),0,response.length(),
requestPacket.getSocketAddress());
socket.send(responsePacket);
System.out.printf("[%s:%d] req: %s, resp: %s\n", requestPacket.getAddress().toString(),
requestPacket.getPort(), request, response);
}
}
public String process(String request){
return request;
}
public static void main(String[] args) throws IOException {
MyServer myServer = new MyServer(9090);
myServer.start();
}
}
Client
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
public class MyClient {
DatagramSocket socket = null;
int server_port;
String server_ip;
MyClient(String server_ip,int server_port) throws SocketException {
this.socket = new DatagramSocket();
this.server_ip = server_ip;
this.server_port = server_port;
}
public void start() throws IOException {
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("->");
String request = sc.next();
DatagramPacket requestPacket = new DatagramPacket(request.getBytes(),0,request.length(),
InetAddress.getByName(server_ip),server_port);
socket.send(requestPacket);
DatagramPacket responsePacket = new DatagramPacket(new byte[4096],4096);
socket.receive(responsePacket);
String response = new String(responsePacket.getData(),0,responsePacket.getLength());
System.out.printf("req: %s, resp: %s\n", request, response);
}
}
public static void main(String[] args) throws IOException {
MyClient myClient = new MyClient("127.0.0.1",9090);
myClient.start();
}
}