UDP echo server 和 echo client
UDP echo server
- 创建一个
DatagramSocket
,这是send()
和receive()
的结点。 - 尝试读取
packet
,如果没有数据读到,receive()
会阻塞。 - 解析
receivePacket
的内容 - 处理接收到的报文
- 打包并发送处理结果
public class UdpEchoServer1 {
private DatagramSocket socket = null;
public UdpEchoServer1(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.getBytes().length,requestPacket.getAddress(), requestPacket.getPort());
socket.send(responsePacket);
System.out.printf("[%s:%d] request:%s response:%s\n",
requestPacket.getAddress().toString(),
requestPacket.getPort(),
request,
response);
}
}
public String process(String s){
return s;
}
public static void main(String[] args) throws IOException {
UdpEchoServer1 server = new UdpEchoServer1(8000);
server.start();
}
}
UDP echo client
- 读取要发送的内容
- 打包并发送报文到服务器端
- 接收服务器端返回的报文
- 解析报文
public class UdpEchoClient1 {
private DatagramSocket socket = null;
public UdpEchoClient1() throws SocketException {
this.socket = new DatagramSocket();
}
public void start() throws IOException {
Scanner in = new Scanner(System.in);
while(true){
System.out.print(">");
String request = in.nextLine();
DatagramPacket requestPacket = new DatagramPacket(request.getBytes(),request.getBytes().length,
InetAddress.getByName("127.0.0.1"),8000);
socket.send(requestPacket);
DatagramPacket receivePacket = new DatagramPacket(new byte[4096],4096);
socket.receive(receivePacket);
String receive = new String(receivePacket.getData(),0, receivePacket.getLength());
System.out.printf("request:%s response:%s\n",request,receive);
}
}
public static void main(String[] args) throws IOException {
UdpEchoClient1 client = new UdpEchoClient1();
client.start();
}
}
TCP echo server 和echo client
TCP echo server
- 创建
ServerSocket
,建立连接 - 处理请求,构造
InputStream
和OutputStream
public class TcpEchoServer {
private ServerSocket serverSocket = null;
public TcpEchoServer(int port) throws IOException {
this.serverSocket = new ServerSocket(port);
}
public void start() throws IOException {
System.out.println("服务器启动");
ExecutorService pool = Executors.newCachedThreadPool();
while(true){
Socket clientSocket = serverSocket.accept();
pool.submit(new Runnable() {
@Override
public void run() {
try {
processConnect(clientSocket);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void processSocket(Socket clientSocket) throws IOException {
System.out.printf("[%s:%d] 建立连接\n",clientSocket.getInetAddress().toString(),clientSocket.getPort());
try(InputStream inputStream = clientSocket.getInputStream();
OutputStream outputStream = clientSocket.getOutputStream()){
Scanner scanner = new Scanner(inputStream);
PrintWriter printWriter = new PrintWriter(outputStream);
while(true){
if(!scanner.hasNext()){
System.out.printf("[%s:%d] 断开连接\n",clientSocket.getInetAddress().toString(),
clientSocket.getPort());
break;
}
String request = scanner.next();
String response = process(request);
printWriter.println(response);
printWriter.flush();
System.out.printf("[%s:%d] request=%s response=%s\n",clientSocket.getInetAddress().toString(),
clientSocket.getPort(), request,response);
}
}finally {
clientSocket.close();
}
}
private String process(String request) {
return request;
}
public static void main(String[] args) throws IOException {
TcpEchoServer server = new TcpEchoServer(8000);
server.start();
}
}
TCP echo client
- 根据IP,port创建
Socket
,建立连接 - 发送消息
- 处理响应
public class TcpEchoClient {
private Socket socket = null;
public TcpEchoClient() throws IOException {
this.socket = new Socket("127.0.0.1",8000);
}
public void start() throws IOException {
Scanner scanner = new Scanner(System.in);
try(InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream()) {
Scanner input = new Scanner(inputStream);
PrintWriter output = new PrintWriter(outputStream);
while (true) {
System.out.print(">");
String request = scanner.next();
output.println(request);
output.flush();
String response = input.next();
System.out.printf("request:%s response:%s\n", request, response);
}
}
}
public static void main(String[] args) throws IOException {
TcpEchoClient client = new TcpEchoClient();
client.start();
}
}