用UDP实现客户端与服务器通信-Java

使用Java进行网络编程,用 UCP实现服务器客户端之间通信,并且服务器会给客户端返回一个响应

1.服务器端

代码如下(示例):

package Server;
import java.io.IOException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import java.util.Arrays;
//服务器端
public class UdpServer {
    private DatagramSocket socket = null;//创建一个DatagramSocket对象socket用来表示客户端

    public UdpServer(int port)throws SocketException{
        //构造方法
        socket = new DatagramSocket(port);
    }



    public void start() throws IOException {
        System.out.println("服务器启动");
        while (true){
            DatagramPacket packet1 = new DatagramPacket(new byte[4096],4096);
            //实例化一个DatagramPacket对象packet1用来存放将要在客户端读到的信息
            socket.receive(packet1);
            //读取客户端信息放入packet1
            String request = new String(packet1.getData(),0,packet1.getLength());
            //将读到的DatagramPacket类型信息转换成字符串类型存储在request中
            String response = process(request);
            //创建一个String类型的response用来存放计算的响应
            DatagramPacket packet2 = new DatagramPacket(response.getBytes(),
                    response.getBytes().length,packet1.getSocketAddress());
            //将得到的响应转换成字符串类型放置在packet2中
            socket.send(packet2);
            //将得到的转换后的响应发送给客户端
            System.out.printf("[%s;%d] req:%s ;resp:%s\n",packet2.getAddress().toString(),
                    packet2.getPort(),request,response);
            //打印当前这次请求响应的处理中间结果.
        }
    }


    public String process(String request) {
        //计算响应
        return request;
    }

    public static void main(String[] args) throws IOException{
        UdpServer server = new UdpServer(9090);
        //实例化UdpServer对象为server,输入端口号为9090
        server.start();
    }
}

2.客户端

代码如下(示例):

package Server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;


public class UdpEchoClient {
    private DatagramSocket socket = null;//创建一个DatagramSocket对象socket用来和服务器端通信

    private String serverIP = null;//创建一个字符串变量来保存服务器IP地址

    private int serverPort = 0;// 创建一个int变量来保存端口号


    public UdpEchoClient (String serverIP,int serverPort) throws SocketException {
        //UdpEchoClient类的构造方法,变量IP和端口号
        socket = new DatagramSocket();
        this.serverIP = serverIP;
        this.serverPort = serverPort;
    }
    public void start() throws IOException {
        System.out.println("客户端启动");
        Scanner scanner = new Scanner(System.in);

        while(true){

            System.out.printf(">");
            String request = scanner.next();
            if(request.equals("next")){
                System.out.println("bye,bye");
                break;
            }
            //从键盘读取输入信息,遇到next就停止

            DatagramPacket datagramPacket1 = new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(serverIP),serverPort);
            //实例化一个DatagramPacket对象datagramPacket1用来存放从键盘读取的信息

            socket.send(datagramPacket1);
            //将读取的信息datagramPacket1发送给服务器端

            DatagramPacket datagramPacket2 = new DatagramPacket(new byte[4096],4096);
            //实例化一个DatagramPacket对象datagramPacket2用来存放从服务器端读取的信息

            socket.receive(datagramPacket2);
            //从服务器端读取信息放入datagramPacket2中

            String response = new String(datagramPacket2.getData(),0,datagramPacket2.getLength());
            //将从服务器端读取信息datagramPacket2转换成String类型放在response

            System.out.println(response);
            //打印转换后的信息response

        }
    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient client = new UdpEchoClient("127.0.0.1",9090);
        //实例化UdpEchoClient对象client
        client.start();
    }
}



---


你可能感兴趣的:(java,udp,服务器)