美文网首页
Java网络聊天程序

Java网络聊天程序

作者: 粑粑八成 | 来源:发表于2020-02-02 10:18 被阅读0次

    UDP:
    1、指定socket端口
    2、在发送的包中指定发送到的地址和端口
    3、发送的包中的端口必须和接受端的socket端口相同
    java网络编程UDP
    IO流,字符字节流转换,输入流
    多线程

    package UdpTest;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.SocketException;
    
    public class TalkReceive implements Runnable {
    
      private DatagramSocket server;
    
      public TalkReceive(int port) {
        try {
          this.server = new DatagramSocket(port);
        } catch (SocketException e) {
          e.printStackTrace();
        }
      }
    
      @Override
      public void run() {
        while (true) {
          byte[] container = new byte[1024 * 60]; // 60k
          DatagramPacket packet = new DatagramPacket(container, container.length);
          // 4、阻塞式接收包裹receive(DatagramSocket p)
          try {
            server.receive(packet);
            // 5、分析数据
            //    byte[] getData()
            //    getLength()
            byte[] datas = packet.getData();
            int len = packet.getLength();
            String msg = new String(datas, 0, len); // 制定长度,必须,否则默认按字节数组长度,equal比较会报错,我日
            if (msg.equals("exit")) {
              break;
            }
            System.out.println("对方说:" + msg);
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        // 6、释放资源
        server.close();
      }
    }
    
    package UdpTest;
    
    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 TalkSend implements Runnable {
    
      private DatagramSocket client;
      private BufferedReader reader;
      private String toIP;
      private int toPort;
    
      public TalkSend(int port, String toIp, int toPort) {
        this.toIP = toIp;
        this.toPort = toPort;
        try {
          this.client = new DatagramSocket(port);
          this.reader = new BufferedReader(new InputStreamReader(System.in));
        } catch (SocketException e) {
          e.printStackTrace();
        }
      }
    
      @Override
      public void run() {
        while (true) {
          String msg = null;
          try {
            msg = this.reader.readLine();
            byte[] datas = msg.getBytes();
            // 3、封装成DatagramSocket包裹,需要指定目的地
            DatagramPacket packet = new DatagramPacket(datas, datas.length,
                new InetSocketAddress(this.toIP,
                    this.toPort));
            // 4、发送接收包裹receive(DatagramSocket p)
            this.client.send(packet);
            if (msg.equals("exit")) {
              break;
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        client.close();
      }
    }
    
    package UdpTest;
    
    public class TalkStudent {
    
      public static void main(String[] args) {
        new Thread(new TalkSend(7777, "localhost", 9999)).start(); //发送
    
        new Thread(new TalkReceive(8888)).start(); // 接收
      }
    }
    
    package UdpTest;
    
    public class TalkTeacher {
    
      public static void main(String[] args) {
        new Thread(new TalkReceive(9999)).start(); // 接收
    
        new Thread(new TalkSend(5555, "localhost", 8888)).start(); //发送
      }
    }
    

    相关文章

      网友评论

          本文标题:Java网络聊天程序

          本文链接:https://www.haomeiwen.com/subject/migfxhtx.html