Java UDP实现聊天功能代码【转】
感谢大佬大佬!!!:https://www.cnblogs.com/woshijpf/p/3735684.html
我以前经常写的是基于TCP的网络编程,由于TCP建立连接鼻血要经过三次握手连接,服务器端需要阻塞式等待客户端的连接。而UDP则是可以直接向目的地址的目的端口上发送数据包,由于它只负责发送出去就好,不管对方是否正确接受到与否,所以当网络性能不好时它容易出现丢包的问题。(注意:UDP是基于数据报为单位进行传输的,而TCP是一种基于流进行传输的)
但是UDP很好的模拟了我们呢平时聊天的方式,可以很好的实现连续多次发送和接受,也就是简单的QQ聊天的功能。
现在来简要介绍Java中有关UDP编程相关的类:
一个是DatagramPacket,主要用来将数据字节填充到UDP数据报中,用来解包接受数据的,用来收和发UDP数据报。一个是DatagramSocket主要用来是建立客户端和服务器端,用来接受和发送数据报数据的。
(1)DatagramPacket类
UDP首部向IP首部添加了8个字节,包含了源端口和目标端口,IP首部以后内容的长度和校验和,最多有65507个字节数。UDP所使用的端口和TCP使用的端口不一样的,是一个final类。
对于接受数据,将接受到的数据存储到DatagramPacket,然后从该对象中读取数据。
对于发送数据,将发送的数据先存到DatagramPacket中,然后将该对象发送。
以下是我写的简单的一个聊天程序:
Send_Thread.java
package Chat_UDP;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Scanner;
public class Send_Thread extends Thread{
//发送的socket端
private DatagramSocket sender = null;
//待发送的目标地址
private InetSocketAddress address = null;
//从键盘输入
Scanner scan = new Scanner(System.in);
public Send_Thread(DatagramSocket sender,InetSocketAddress address)
{
this.sender = sender;
this.address = address;
}
@Override
public void run() {
// TODO Auto-generated method stub
try
{
while(true)
{
//输入待发送的内容
String input = scan.nextLine();
if(input.equals("exit"))
break;
byte[] data = null;
data = input.getBytes("UTF-8");
//创建UDP数据报
DatagramPacket pack = new DatagramPacket(data, data.length,address);
sender.send(pack);
}
System.out.println("Exit!");
}catch(IOException e)
{
System.out.println("IOException");
}
}
}
Receive_Thread.java
package Chat_UDP;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Receive_Thread extends Thread {
private static final int MAX_RECEIVE_BUFFER = 1024;
private DatagramSocket server;
private DatagramPacket packet;
byte[] buffer = new byte[MAX_RECEIVE_BUFFER];
public Receive_Thread(DatagramSocket server)
{
this.server = server;
packet = new DatagramPacket(buffer, buffer.length);
}
@Override
public void run() {
// TODO Auto-generated method stub
try
{
while(true)
{
//接收数据包
server.receive(packet);
String s = new String(packet.getData(),packet.getOffset(),packet.getLength(),"UTF-8");
System.out.println(packet.getAddress()+" at port "+packet.getPort()+" Says :"+s);
packet.setLength(buffer.length);
}
}
catch(IOException e)
{
System.out.println("IOException");
}
}
}
Chat_Client.java
package Chat_UDP;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class Chat_Client {
private static final int DEST_PORT = 9000;
private static final int SEND_PORT = 10000;
private static final int RECE_PORT = 8888;
private static final String IP = "127.0.0.1";
public static void main(String[] args)
{
try{
Send_Thread send_thread = null;
Receive_Thread rece_thread = null;
InetSocketAddress address = null;
//创建待接受数据包的目的机的端口号和IP地址
address = new InetSocketAddress(IP, DEST_PORT);
//创建发送的Socket端
DatagramSocket sendsocket = new DatagramSocket(SEND_PORT);
//创建接受的Socket端
DatagramSocket recesocket = new DatagramSocket(RECE_PORT);
//发送线程建立
send_thread = new Send_Thread(sendsocket, address);
//接受线程的建立
rece_thread = new Receive_Thread(recesocket);
send_thread.start();
rece_thread.start();
}catch(Exception e)
{
System.out.println("Exception!");
}
}
}
补充:
https://baike.baidu.com/item/UDP/571511?fr=aladdin
https://baike.baidu.com/item/TCP/IP协议
https://www.jianshu.com/p/fb03d7af30bb
https://jingyan.baidu.com/article/76a7e409fb5873fc3a6e1577.html