Java UDP使用Socket进行网络聊天(2)之群聊版

来源:互联网 发布:最大的网络直播平台 编辑:程序博客网 时间:2024/04/30 04:19

作者 : 卿笃军

原文地址:http://blog.csdn.net/qingdujun/article/details/39312241


本文演示,使用Socket进行网络聊天之群聊,实现客户端给"局域网"网段里面的所以机器发送广播,当发送“886”的时候,表示客户端关闭。

1)客户端,给服务器发送数据,发送“886”表示关闭客户端。

2)服务器,一直监听9527端口,将监听到的数据打印在控制台上。

3)客户端+服务器版本,实现既可以发送数据,又可以接受数据的多线程聊天程序。

需要注意的几点是:1)这里是给局域网发送广播。  2)局域网广播地址:192.168.1.255  3)如果弹出Exception in thread "main" java.net.BindException: Address already in use: Cannot bind 问题,请换一个端口号(该端口号还没有被释放)。


一、客户端:主要使用了从键盘录入,和当发送“886”数据包的时候,关闭客户端。

package udp.send2.qdj;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class CUdpSend2 {public static void main(String[] args) throws Exception {//从本机1234端口发送数据DatagramSocket ds = new DatagramSocket(1234);//从键盘录入BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));String line = null;while ((line=bufr.readLine())!=null){if ("886".equals(line))break;byte[] buf = line.getBytes();//将数据打包,发送到对法机器的9527端口DatagramPacket dp = new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.1.254"),9527);//发送数据ds.send(dp);}//关闭资源ds.close();}}
二、服务器端,主要是一直监听9527端口,并将监听到的数据打印到控制台上。

1)控制台可以拖放出来,如果需要放进去点击"最小化"左边那个的最下面那个选项就可以。

2)切换控制台,可以点击"最小化"左边第2个选项。

package udp.rece2.qdj;import java.net.DatagramPacket;import java.net.DatagramSocket;public class CUdpRece2 {public static void main(String[] args) throws Exception{//监听9527端口DatagramSocket ds = new DatagramSocket(9527);while (true){byte[] buf = new byte[1024];//定义一个数据包DatagramPacket dp = new DatagramPacket(buf, buf.length);//接受数据包ds.receive(dp);//提取数据String ip = dp.getAddress().getHostAddress();String data = new String(dp.getData(),0,dp.getLength());System.out.println("ip号:"+ip+"\n数据data:"+data);}}}

三、综合上述,编写一个既可以接受又可以发送的多线程群聊程序:

package udp.chat.qdj;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;//发送端class Send implements Runnable{private DatagramSocket ds;public Send(DatagramSocket s){ds = s;}public void run() {try {//从键盘录入BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));String line = null;while ((line=bufr.readLine())!=null){if ("886".equals(line))break;byte[] buf = line.getBytes();//将数据打包,发送到对法机器的5656端口   //局域网广播地址:192.168.1.255DatagramPacket dp = new DatagramPacket(buf, buf.length,InetAddress.getByName("10.100.56.210"),5656);//发送数据ds.send(dp);}} catch (Exception e) {throw new RuntimeException("发送端失败");}}}//接收端class Rece implements Runnable{private DatagramSocket ds;public Rece(DatagramSocket s){ds = s;}public void run() {try {while (true){byte[] buf = new byte[1024];//定义一个数据包DatagramPacket dp = new DatagramPacket(buf, buf.length);//接受数据包ds.receive(dp);//提取数据String ip = dp.getAddress().getHostAddress();String data = new String(dp.getData(),0,dp.getLength());System.out.println(ip+":"+data);}} catch (Exception e) {throw new RuntimeException("接受端失败");}}}public class CUdpChat {public static void main(String[] args) throws Exception{//发送端可以不指定端口,系统随机分配一个DatagramSocket sendSocket = new DatagramSocket();DatagramSocket receSocket = new DatagramSocket(5656);//开启多线程new Thread(new Send(sendSocket)).start();new Thread(new Rece(receSocket)).start();}}

显示界面:



参考文献: Java视频 毕向东 主讲

原文地址:http://blog.csdn.net/qingdujun/article/details/39312241

2 0
原创粉丝点击