Java_UDP聊天
来源:互联网 发布:蓝桥杯c语言300试题 编辑:程序博客网 时间:2024/04/29 02:52
import java.io.*;import java.net.*;class UdpSend implements Runnable{private DatagramSocket ds;public UdpSend(DatagramSocket ds){this.ds = ds;}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();DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("10.100.83.188"),10003);ds.send(dp);}}catch(Exception e){throw new RuntimeException("发送失败!!");}}}class UdpRece implements Runnable{private DatagramSocket ds;public UdpRece(DatagramSocket ds){this.ds = ds;}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 chatDemo {public static void main(String[] args) throws Exception {DatagramSocket sendSocket = new DatagramSocket();DatagramSocket receSocket = new DatagramSocket(10003);new Thread(new UdpSend(sendSocket)).start();new Thread(new UdpRece(receSocket)).start();}}
0 0
- Java_UDP聊天
- java_UDP
- Java_UDP和TCP
- JAVA_UDP发送端和接受端
- JAVA_UDP发送端和接受端
- 聊天
- 聊天
- 聊天
- 聊天
- 聊天
- 聊天
- 聊天
- 聊天
- 聊天
- 聊天
- 聊天
- 聊天
- 聊天
- c++对象基础
- AC自动机 hdu2222 Keywords Search
- linux c fopen修改已经存在的文件
- Javascript的this用法
- Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage
- Java_UDP聊天
- 开启Ionic之旅
- Oracle几种清除数据的方式,在平台用别的医院数据库做初始化时有用
- 新版本SDWebImage使用
- Qt获取系统环境变量
- Java_TCP登录
- ant打包
- centos桌面版NetworkManager
- git基础指南