其他分享
首页 > 其他分享> > 网络通讯模型

网络通讯模型

作者:互联网

通络通信模型

发送方实现

package Liu.UdpTalk;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;


public class Talkclient implements Runnable {
    BufferedReader buffer = null;
    //创建一个数据插座
    DatagramSocket ds = null;
    private int toPort;
    private int fromPort;
    private String toIP;
    public Talkclient(int toPort, int fromPort, String toIP) {
        this.toPort = toPort;
        this.fromPort = fromPort;
        this.toIP = toIP;
        try {
            ds = new DatagramSocket(this.fromPort);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            buffer = new BufferedReader(new InputStreamReader(System.in));
            String data = null;
            try {
                data = buffer.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //将数据打包成数据包
            DatagramPacket dp = new DatagramPacket(data.getBytes(), 0, data.getBytes().length, new InetSocketAddress(this.toIP, this.toPort));
            //发送数据
            try {
                ds.send(dp);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (data.equals("bye")) {
                break;
            }
        }

        //关闭数据
        ds.close();
    }
}

接收方实现

package Liu.UdpTalk;

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


public class TalkSever implements Runnable {
    DatagramSocket socket = null;
    private int Port;
    private String msgFrom;

    public TalkSever(int port, String msgFrom) {
        this.Port = port;
        this.msgFrom = msgFrom;
    }

    @Override
    public void run() {
        try {
            socket=new DatagramSocket(this.Port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
        while (true) {
                //准备接收包裹
                byte[] container = new byte[100];
                DatagramPacket packet = new DatagramPacket(container, 0, container.length);
            try {
                socket.receive(packet);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //断开连接 bye
                byte[] Data = packet.getData();
                int len = packet.getLength();
                String receiveData = new String(Data, 0, len);
                System.out.println(this.msgFrom + ":" + receiveData);
                if (receiveData.equals("bye")) {
                    break;
                }
            }

        this.socket.close();
    }
}

多线程演示

public class Talk1 {
    public static void main(String[] args) throws SocketException {
        new Thread(new TalkSever(9999, "老师")).start();
        new Thread(new Talkclient(8888, 7777, "127.0.0.1")).start();
    }
}



public class Talk {
    public static void main(String[] args) throws SocketException {
        new Thread(new TalkSever(8888, "学生")).start();
        new Thread(new Talkclient(9999, 7776, "127.0.0.1")).start();
    }
}

标签:java,String,模型,网络通讯,import,new,net,public
来源: https://www.cnblogs.com/123xian/p/16023562.html