Several examples of the communication of the Java Socket (UDP)

UDP Introduction

java.net.ServerSocket and java.net.Socket is built on TCP protocol basis. Reliable transport protocol TCP, UDP but speed is not fast.
UDP drawback is unreliable and can not guarantee that the data will be able to reach the destination, and the target reception packet order.
UDP for transmission of video, audio. The TCP protocol for reliable applications, such as FTP, HTTP.
UDP api in java mainly java.util.DatagramSocket class. Each DatagramSocket with a local address binding, can each DatagramSocket UDP packet to any remote DatagramSocket, UDP packets can be received from any of a remote DatagramSocket.

TCP Client and Server communication must first establish a connection, UDP do not establish a connection, send data packets directly.

Examples

public class EchoServer {
    private int port = 8000;
    private DatagramSocket socket;

    public EchoServer() throws IOException {
        socket = new DatagramSocket(port); //与本地的一个固定端口绑定
        System.out.println("服务器启动");
    }

    public String echo(String msg) {
        return "echo:" + msg;
    }

    public void service() {
        while (true) {
            try {
                byte[] buf = new byte[512];
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);  //接收来自任意一个EchoClient的数据报
                String msg = new String(packet.getData(), 0, packet.getLength());
                System.out.println(packet.getAddress() + ":" + packet.getPort() + ">" + msg);

                //给EchoClient回复一个数据报
                packet.setData(echo(msg).getBytes());
                socket.send(packet);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String args[]) throws IOException {
        new EchoServer().service();
    }
}
public class EchoClient {
    private String remoteHost = "localhost";
    private int remotePort = 8000;
    private DatagramSocket socket;

    public EchoClient() throws IOException {
        socket = new DatagramSocket(); //与本地的任意一个UDP端口绑定
    }

    public static void main(String args[]) throws IOException {
        new EchoClient().talk();
    }

    public void talk() throws IOException {
        try {
            InetAddress remoteIP = InetAddress.getByName(remoteHost);

            BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
            String msg = null;
            while ((msg = localReader.readLine()) != null) {
                byte[] outputData = msg.getBytes();
                DatagramPacket outputPacket = new DatagramPacket(outputData,
                        outputData.length, remoteIP, remotePort);
                socket.send(outputPacket);  //给EchoServer发送数据报

                byte[] bytes = new byte[512];
                DatagramPacket inputPacket = new DatagramPacket(bytes, bytes.length);
                socket.receive(inputPacket);
                System.out.println(new String(inputPacket.getData(), 0, inputPacket.getLength()));
                if (msg.equals("bye")) {
                  break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            socket.close();
        }
    }
} 
Published 55 original articles · won praise 6 · views 889

Guess you like

Origin blog.csdn.net/smith789/article/details/104342086