使用UDP实现聊天功能

发送端:


import java.io.IOException;
import java.net.*;
import java.util.Scanner;

class MySend implements Runnable {

    @Override
    public void run() {
        while (true) {
            Scanner scan = new Scanner(System.in);
            String msg = scan.next();
            DatagramSocket sendSocket = null;
            try {
                sendSocket = new DatagramSocket();
            } catch (SocketException e) {
                e.printStackTrace();
            }

            byte[] bytes = msg.getBytes();
            InetAddress inetAddress = null;
            try {
                inetAddress = InetAddress.getByName("192.168.31.126");
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, inetAddress, 5200);
            try {
                sendSocket.send(datagramPacket);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //关闭
            sendSocket.close();

        }
    }

}

public class SendDemo {
    public static void main(String[] args) throws IOException {
        System.out.println("发送端");

       MySend mySend= new MySend();
       Thread thread = new Thread(mySend);
       thread.start();



    }
}

接收端:

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

class MyReceive implements Runnable{

    @Override
    public void run() {
        while (true) {
            DatagramSocket receiveSocked = null;
            try {
                receiveSocked = new DatagramSocket(5200);
            } catch (SocketException e) {
                e.printStackTrace();
            }

            // while()
            byte[] bytes = new byte[1024];
            //创建一个报文,用来接受传过来的报文
            DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);

            //接受
            try {
                receiveSocked.receive(datagramPacket);
            } catch (IOException e) {
                e.printStackTrace();
            }
            InetAddress ip = datagramPacket.getAddress();
            //返回数据缓冲区。接受到的数据一般在缓冲区
            //datagramPacket.getLength():得到报文的长度
            //datagramPacket.getData():得到报文的信息
            System.out.println(ip.getHostAddress() + "-----" + new String(datagramPacket.getData(), 0, datagramPacket.getLength()));
            receiveSocked.close();
        }
    }
}
public class ReceiveDemo {
    public static void main(String[] args) throws IOException {
        System.out.println("接收端");
        MyReceive myReceive= new MyReceive();
        Thread thread = new Thread(myReceive);
        thread.start();

    }
}
运行结果:
 
  
 
  
 
  
 
  
 
  
 
  
分析总结:
 
  
 
  
先运行接收端
此代码是发送给自己的,要与别人聊天,就把发送端的
inetAddress = InetAddress.getByName("192.168.31.126");中的ip改为别人的,把另一个人的此代码的ip改为自己的即可
 
  
群聊的话,改成广播就行,即ip的
 
  
"192.168.31.126"的126改为255,就可以实现群聊功能,
当然,要保证接收端的端口号一致,即
receiveSocked = new DatagramSocket(5200);
5200为端口号
 
  
 
  
 
  



你可能感兴趣的:(使用UDP实现聊天功能)