Java实现使用UDP与TCP小例

文若杂谈

TCP

TCP在计网中的位置十分重要,很多协议的运行都是以TCP为基础来运行的,虽然TCP的开销比UDP开销大,但其实现的可靠性交付是一大特点,其还具有流量控制、拥塞控制等特性,在面试中也少不了谈其三次握手、四次挥手以及其他问题。

public class LengthCalculator extends  Thread{
    private Socket socket;

    public LengthCalculator(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run(){
        try{
            OutputStream os =socket.getOutputStream();
            InputStream is =socket.getInputStream();
            int ch=0;
            byte[] buff =new byte[1024];
            ch=is.read(buff);
            String content =new String(buff,0,ch);
            System.out.println(content);
            os.write(String.valueOf(content.length()).getBytes());
            is.close();
            os.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class TCPClient {
    public static void main(String[] args) throws IOException {
        Socket socket=new Socket("127.0.0.1",65000);
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream();
        out.write("Hello World!".getBytes());
        int ch=0;
        byte[] bytes=new byte[1024];
        ch=in.read(bytes);
        String content=new String(bytes,0,ch);
        System.out.println("接收到内容content:"+content);
        in.close();
        out.close();
        socket.close();
    }
}

public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss =new ServerSocket(65000);
        while(true){
            Socket socket=ss.accept();
            new LengthCalculator(socket).start();
        }
    }
}

UDP

UDP是数据报传输协议,其可传输的大小取决于数据报的大小,不像TCP协议一样受到MTU最大传输的限制,也不用像TCP一样实现报文切片等特性进行传输,但UDP的主要传输速率取决于双方的带宽,且不提供可靠性交付。

public class UDPClient {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket =new DatagramSocket();
        byte[] buf ="hello world".getBytes();
        //将IP封装为InetAddress对象
        InetAddress address =InetAddress.getByName("127.0.0.1");
        //将要发送数据封装成DatagramPacket对象,填写上ip地址与端口号
        DatagramPacket packet =new DatagramPacket(buf,buf.length,address,65001);
        socket.send(packet);

        byte[] data =new byte[100];
        //创建DatagramPacket对象来存储服务端发送的数据
        DatagramPacket receivedPacket = new DatagramPacket(data,data.length);
        socket.receive(receivedPacket);
        //将数据取出来并打印
        String content = new String(receivedPacket.getData(),0,receivedPacket.getLength());
        System.out.println(content);
    }
}

public class UDPServer {
    public static void main(String[] args) throws IOException {
        //服务端接收客户端发送的数据报
        DatagramSocket socket =new DatagramSocket(65001);
        //存储从客户端收到的内容
        byte[] buff =new byte[100];
        //接收客户端发送的内容,并将内容封装进DatagramPacket对象中
        DatagramPacket packet =new DatagramPacket(buff,buff.length);
        socket.receive(packet);
        //从DatagramPacket中获取真正存储的数据
        byte[] data =packet.getData();
        //将数据从二进制转化为字符串形式
        String content =new String(data,0,packet.getLength());
        System.out.println(content);
        //将要发送给客户端的数据换成二进制
        byte[] sendedContent =String.valueOf(content.length()).getBytes();
        //服务端给客户端发送数据报
        //从DatagramPacket对象中获取到数据的来源地址与端口号
        DatagramPacket packetToClient =new DatagramPacket(sendedContent,sendedContent.length,packet.getAddress(),packet.getPort());
        socket.send(packetToClient);
    }
}
阅读 53

4 声望
0 粉丝
0 条评论
你知道吗?

4 声望
0 粉丝
宣传栏