博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java基础----UDP
阅读量:2186 次
发布时间:2019-05-02

本文共 3891 字,大约阅读时间需要 12 分钟。

package com.cug.net03;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;public class ReceiveDemo {	public static void main(String[] args) throws IOException {		//接收端Socket		DatagramSocket ds = new DatagramSocket(12306);				byte[] buf = new byte[1024];		DatagramPacket dp = new DatagramPacket(buf,buf.length);				ds.receive(dp);				System.out.println(dp.getAddress().getHostAddress()+				","+new String(dp.getData(),0,dp.getLength()));				ds.close();	}}

package com.cug.net03;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class SendDemo {	public static void main(String[] args) throws IOException {		DatagramSocket ds = new DatagramSocket();				byte[] buf = "I Love you,Java!!!".getBytes();		DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("bluze"),12306);				ds.send(dp);				ds.close();	}}
package com.cug.net04;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;public class ReceiveDemo {	public static void main(String[] args) throws IOException {		//接收端Socket		DatagramSocket ds = new DatagramSocket(12306);				while(true){			byte[] buf = new byte[1024];			DatagramPacket dp = new DatagramPacket(buf,buf.length);						ds.receive(dp);						System.out.println(dp.getAddress().getHostAddress()+					","+new String(dp.getData(),0,dp.getLength()));		}		//		ds.close();	}}
package com.cug.net04;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class SendDemo {	public static void main(String[] args) throws IOException {		DatagramSocket ds = new DatagramSocket();				while(true){			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));			String line = br.readLine();			if(line.equals("886")){				break;			}			byte[] buf = line.getBytes();			DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("bluze"),12306);						ds.send(dp);		}				ds.close();	}}
package com.cug.net05;import java.net.DatagramSocket;import java.net.SocketException;public class ChatRoom {	public static void main(String[] args) throws SocketException {		DatagramSocket dsReceive = new DatagramSocket(12306);		DatagramSocket dsSend = new DatagramSocket();				ReceiveThread rt = new ReceiveThread(dsReceive);		SendThread st = new SendThread(dsSend);				Thread t1 = new Thread(rt);		Thread t2 = new Thread(st);				t1.start();		t2.start();	}}
package com.cug.net05;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;public class ReceiveThread implements Runnable {	DatagramSocket ds;	public ReceiveThread(DatagramSocket ds){		this.ds = ds;	}	@Override	public void run() {		try{			while(true){				byte[] buf = new byte[1024];				DatagramPacket dp = new DatagramPacket(buf,buf.length);								ds.receive(dp);								System.out.println(dp.getAddress().getHostAddress()+						","+new String(dp.getData(),0,dp.getLength()));			}		}catch(IOException e){			e.printStackTrace();		}	}}
package com.cug.net05;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class SendThread implements Runnable {	DatagramSocket ds;	public SendThread(DatagramSocket ds){		this.ds = ds;	}	@Override	public void run() {		try{			while(true){				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));				String line = br.readLine();				if(line.equals("886")){					break;				}				byte[] buf = line.getBytes();				DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("bluze"),12306);								ds.send(dp);			}						ds.close();		}catch(IOException e){			e.printStackTrace();		}			}}

转载地址:http://nmhkb.baihongyu.com/

你可能感兴趣的文章
Centos7 or Other Linux RPM包查询下载
查看>>
运行springboot项目出现:Type javax.xml.bind.JAXBContext not present
查看>>
Java中多线程向mysql插入同一条数据冲突问题
查看>>
Idea Maven项目使用jar包,添加到本地库使用
查看>>
FastDFS集群架构配置搭建(转载)
查看>>
HTM+CSS实现立方体图片旋转展示效果
查看>>
FFmpeg 命令操作音视频
查看>>
问题:Opencv(3.1.0/3.4)找不到 /opencv2/gpu/gpu.hpp 问题
查看>>
目的:使用CUDA环境变量CUDA_VISIBLE_DEVICES来限定CUDA程序所能使用的GPU设备
查看>>
问题:Mysql中字段类型为text的值, java使用selectByExample查询为null
查看>>
程序员--学习之路--技巧
查看>>
解决问题之 MySQL慢查询日志设置
查看>>
contOS6 部署 lnmp、FTP、composer、ThinkPHP5、docker详细步骤
查看>>
TP5.1模板布局中遇到的坑,配置完不生效解决办法
查看>>
PHPstudy中遇到的坑No input file specified,以及传到linux环境下遇到的坑,模板文件不存在
查看>>
TP5.1事务操作和TP5事务回滚操作多表
查看>>
composer install或composer update 或 composer require phpoffice/phpexcel 失败解决办法
查看>>
TP5.1项目从windows的Apache服务迁移到linux的Nginx服务需要注意几点。
查看>>
win10安装软件 打开时报错 找不到 msvcp120.dll
查看>>
PHPunit+Xdebug代码覆盖率以及遇到的问题汇总
查看>>