8535.com-新浦京娱乐场官网|欢迎您

java 网络编程之UDP通信和简单的群聊程序,javau

来源:http://www.dnamique.com 作者:计算机网络 人气:55 发布时间:2019-10-07
摘要:java 网络编程之UDP通信和简单的群聊程序,javaudp UDP通信需要明确的几点: UDPSender端: import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import ja

java 网络编程之UDP通信和简单的群聊程序,javaudp

UDP通信需要明确的几点:

UDPSender端:

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

public class UDPSender {

    public static void main(String[] args) {
        DatagramSocket dgs = null;
        try {
            // 1. 创建udp发送端的socket,一般使用随机发送端口
            dgs = new DatagramSocket();

            // 2. 创建udp报文包对象
            // 2.1 创建数据发送缓冲区
            String text = "Hello World! I'm coming";
            byte[] buf = text.getBytes();

            // 2.2 创建发送数据报文对象
            InetSocketAddress isa = new InetSocketAddress("192.168.0.124",8888);
            DatagramPacket dgp = new DatagramPacket(buf,buf.length,isa);
            //DatagramPacket dgp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.0.124"), 8888);

            // 3.发送udp报文
            dgs.send(dgp);

        } catch (SocketException e1) {
            e1.printStackTrace();
        } catch (UnknownHostException e2) {
            e2.printStackTrace();
        } catch (IOException e3) {
            e3.printStackTrace();
        } finally {
            // 4. 关闭套接字
            dgs.close();
        }
    }
}

UDPRecver端:

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

public class UDPRecver {

    public static void main(String[] args) {
        while(true){
            // 1.创建udp接收套接字,接收端必须指定正确的端口
            DatagramSocket dgs = null;
            try {
                dgs = new DatagramSocket(8888);

                // 2. 创建udp接收数据包对象
                byte[] buf = new byte[1024];
                DatagramPacket dgp = new DatagramPacket(buf, buf.length);

                // 3.从套接字中接收数据到数据包中
                dgs.receive(dgp);

                // 4.展示udp发送端相关信息,包括发送的数据
                String data = new String(dgp.getData(), 0, dgp.getLength());
                String ip = dgp.getAddress().getHostAddress();
                int port = dgp.getPort();
                System.out.println("Data: "+data+"   ip: "+ip+"   port: "+port);
            } catch (SocketException s) {
                s.printStackTrace();
            } catch (IOException i) {
                i.printStackTrace();
            } finally {
                dgs.close();
            }
        }
    }
}

UDP实现群聊:

思路:

import java.io.BufferedInputStream;
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 QunChat {
    public static void main(String[] args) throws SocketException {
        //发送端和接收端套接字,接收端套接字端口为8888,需要传递给发送端的报文对象
        DatagramSocket send_socket = new DatagramSocket();
        DatagramSocket recv_socket = new DatagramSocket(8888);

        Sender sender = new Sender(send_socket,8888);
        Recver recver = new Recver(recv_socket);

        Thread send_thread1 = new Thread(sender);
        Thread recv_thread1 = new Thread(recver);
        send_thread1.start();
        recv_thread1.start();
    }
}

class Sender implements Runnable {
    private DatagramSocket send_sock;
    private int dest_port;
    Sender(DatagramSocket s,int port){  //初始化时就指定目标端口
        this.send_sock = s;
        this.dest_port = port;
    }

    public void run() {
        while(true) {
            try {
                //群聊发送目标,以广播为例
                InetSocketAddress isa = new InetSocketAddress("192.168.0.255", dest_port);
                //从键盘接收数据
                BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
                String line;
                while((line=bufr.readLine())!=null) {
                    //如果发送的是bye,则断开,且不发送给接收端
                    if(line.equals("bye")) break;

                    byte[] buf = line.getBytes();
                    DatagramPacket dp = new DatagramPacket(buf,buf.length, isa);
                    send_sock.send(dp);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                send_sock.close();
            }
        }
    }
}

class Recver implements Runnable {
    private DatagramSocket recv_sock;
    Recver(DatagramSocket socket){
        this.recv_sock = socket;
    }

    public void run() {
        while(true) {
            try {
                //接收报文对象
                byte[] buf = new byte[1024];
                DatagramPacket dp = new DatagramPacket(buf, buf.length);
                recv_sock.receive(dp);
                String src_ip = dp.getAddress().getHostAddress();
                String data = new String(dp.getData(),0,dp.getLength());
                if(data.equals("bye")) System.out.println(src_ip +" leaving");;
                System.out.println("Recviving data: "+ data+" from "+src_ip );
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

注:若您觉得这篇文章还不错请点击右下角推荐,您的支持能激发作者更大的写作热情,非常感谢!

网络编程之UDP通信和简单的群聊程序,javaudp UDP通信需要明确的几点: UDPSender端: import java.io.IOException; import java.net.DatagramPacket; import...

本文由8535.com-新浦京娱乐场官网|欢迎您发布于计算机网络,转载请注明出处:java 网络编程之UDP通信和简单的群聊程序,javau

关键词:

频道精选

最火资讯