JAVA学习第六十课 — UDP协议 &基于多线程模拟简单的QQ聊天程序
2014-11-01 16:17
946 查看
UDP传输
UDP有发送端和接受端,有两大类,DatagramSocket、DatagramPacket
建立发送端和接收端
建立数据包
调用Socket的接收发送方法
关闭Socket
注意:发送端和接收端是两个独立的运行程序
UDP发送端演示
构造数据报包,用来将长度为
public static void UDPsendDemo() throws IOException {
System.out.println("发送端");
/*
* 创建UDP传输的发送端:
* 1.建立UDP的Socket服务
* 2.将要发送的数据封装到数据包中
* 3.用UDP的Socket服务将数据包发送出去
* 4.关闭Socket服务
*/
//建立UDP的Socket服务,使用DatagramSocket对象
DatagramSocket ds = new DatagramSocket();
//不指定端口号,发送端,就是随机的
//数据封装成包
String str = "udp发送";
//使用DatagramPacket将数据封装到该对象的包中
byte[] by = str.getBytes();
DatagramPacket dp = new DatagramPacket(by,by.length,InetAddress.getByName("127.0.0.1"),6534);
//通过UDP的Socket服务将数据包发送出去,send方法
ds.send(dp);
ds.close();
}
UDP接收端演示
public static void UDPreceiveDemo() throws IOException {
//接受端
System.out.println("接收端");
/*
* 创建UDP传输的接收端:
* 1.建立UDP的Socket服务,因为要接收数据,必须要明确端口号
* 2.创建数据包,用于存储接收到的数据,以便于用数据包的方法解析这些数据
* 3.使用Socket服务的receive方法接收的数据存储到数据包中
* 4.通过数据包中的方法解析包中数据
* 5.关闭
*/
//建立UDP的Socket服务,使用DatagramSocket对象
DatagramSocket ds = new DatagramSocket(6534);//接收端口6534
//创建数据包
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
//使用接收方法将数据存储到数据包
ds.receive(dp);//注意该方法是阻塞式的
//使用数据包对象的方法,解析其中的数据,如:地址、端口、内容
String ip = dp.getAddress().getHostAddress();//获取ip地址字符串
int port = dp.getPort();//获取端口
String datatext = new String(dp.getData(),0,dp.getLength());//将数据封装成字符串对象
System.out.println("ip:"+ip+"port : "+port+"data:"+datatext);
//关闭
ds.close();
}
基于多线程模拟简单的QQ聊天程序
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
class Receive implements Runnable{
private DatagramSocket ds;
public Receive(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();
int port = dp.getPort();
String datatext = new String(dp.getData(),0,dp.getLength());
if(datatext.equals("over")){
System.out.println("ip:"+ip+"port : "+port+"退出聊天室");
}
else{
System.out.println("ip:"+ip+"port : "+port+"data:"+datatext);
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
class Send implements Runnable{
private DatagramSocket ds;
public Send(DatagramSocket ds){
this.ds = ds;
}
public void run()
{
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine())!=null){
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.255"),6534);
//IP的有效位是0-254,255是广播,也就是127.0.0,这一IP字段上所有存活的电脑都可以接收到我发的信息
ds.send(dp);
if("over".equals(line))break;
}
ds.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
public class Main {
public static void main(String[] args) throws IOException{
DatagramSocket dsend =new DatagramSocket();
DatagramSocket dr =new DatagramSocket(6534);
Send send = new Send(dsend);
Receive receive = new Receive(dr);
Thread t1 = new Thread(send);
Thread t2 = new Thread(receive);
t1.start(); t2.start();
}
}
UDP有发送端和接受端,有两大类,DatagramSocket、DatagramPacket
建立发送端和接收端
建立数据包
调用Socket的接收发送方法
关闭Socket
注意:发送端和接收端是两个独立的运行程序
UDP发送端演示
[b]DatagramPacket(byte[] buf, int length, InetAddress address, int port)
构造数据报包,用来将长度为
length的包发送到指定主机上的指定端口号。[/b]
public static void UDPsendDemo() throws IOException {
System.out.println("发送端");
/*
* 创建UDP传输的发送端:
* 1.建立UDP的Socket服务
* 2.将要发送的数据封装到数据包中
* 3.用UDP的Socket服务将数据包发送出去
* 4.关闭Socket服务
*/
//建立UDP的Socket服务,使用DatagramSocket对象
DatagramSocket ds = new DatagramSocket();
//不指定端口号,发送端,就是随机的
//数据封装成包
String str = "udp发送";
//使用DatagramPacket将数据封装到该对象的包中
byte[] by = str.getBytes();
DatagramPacket dp = new DatagramPacket(by,by.length,InetAddress.getByName("127.0.0.1"),6534);
//通过UDP的Socket服务将数据包发送出去,send方法
ds.send(dp);
ds.close();
}
UDP接收端演示
public static void UDPreceiveDemo() throws IOException {
//接受端
System.out.println("接收端");
/*
* 创建UDP传输的接收端:
* 1.建立UDP的Socket服务,因为要接收数据,必须要明确端口号
* 2.创建数据包,用于存储接收到的数据,以便于用数据包的方法解析这些数据
* 3.使用Socket服务的receive方法接收的数据存储到数据包中
* 4.通过数据包中的方法解析包中数据
* 5.关闭
*/
//建立UDP的Socket服务,使用DatagramSocket对象
DatagramSocket ds = new DatagramSocket(6534);//接收端口6534
//创建数据包
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
//使用接收方法将数据存储到数据包
ds.receive(dp);//注意该方法是阻塞式的
//使用数据包对象的方法,解析其中的数据,如:地址、端口、内容
String ip = dp.getAddress().getHostAddress();//获取ip地址字符串
int port = dp.getPort();//获取端口
String datatext = new String(dp.getData(),0,dp.getLength());//将数据封装成字符串对象
System.out.println("ip:"+ip+"port : "+port+"data:"+datatext);
//关闭
ds.close();
}
基于多线程模拟简单的QQ聊天程序
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
class Receive implements Runnable{
private DatagramSocket ds;
public Receive(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();
int port = dp.getPort();
String datatext = new String(dp.getData(),0,dp.getLength());
if(datatext.equals("over")){
System.out.println("ip:"+ip+"port : "+port+"退出聊天室");
}
else{
System.out.println("ip:"+ip+"port : "+port+"data:"+datatext);
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
class Send implements Runnable{
private DatagramSocket ds;
public Send(DatagramSocket ds){
this.ds = ds;
}
public void run()
{
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine())!=null){
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.255"),6534);
//IP的有效位是0-254,255是广播,也就是127.0.0,这一IP字段上所有存活的电脑都可以接收到我发的信息
ds.send(dp);
if("over".equals(line))break;
}
ds.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
public class Main {
public static void main(String[] args) throws IOException{
DatagramSocket dsend =new DatagramSocket();
DatagramSocket dr =new DatagramSocket(6534);
Send send = new Send(dsend);
Receive receive = new Receive(dr);
Thread t1 = new Thread(send);
Thread t2 = new Thread(receive);
t1.start(); t2.start();
}
}
相关文章推荐
- 黑马程序员-JAVA学习之用多线程实现简单UDP聊天程序
- 一个简单的基于udp协议的聊天程序
- 基于Tcp&UDP协议的简单Socket通信实例(JAVA)
- unix学习笔记<2> 多线程 udp聊天程序 简单实例
- 基于UDP协议的Java聊天小程序!
- Java网络编程 - 基于UDP协议 实现简单的聊天室程序
- 网络编程与多线程的应用--基于socket udp编写一个简单聊天程序
- 基于UDP协议的简单聊天程序
- 初识用UDP协议做的windows窗口聊天程序---此处是双向的"异步"多线程对话框通信.
- 【Java网络编程】UDP协议实例——简单的在线聊天程序
- 基于UDP简单聊天程序
- 利用TCP和UDP协议,实现基于Socket的小聊天程序
- [原]java专业程序代写(qq:928900200),学习笔记之基础入门<jsp>(二十六)
- [原]java专业程序代写(qq:928900200),学习笔记之基础入门<SQL_Server_常用查询>(二十二)
- 黑马程序员 用UDP协议在dos命令行里模拟一个聊天程序
- 在.net中使用Udp协议创建简单的聊天程序
- 基于UDP的聊天程序-JAVA
- 基于UDP协议的网络程序 (学习)
- [原]java专业程序代写(qq:928900200),学习笔记之基础入门<SQL_Server_视图_函数_存储过程_触发器等>(二十三)
- 基于UDP 的QQ聊天程序一