UDP聊天室
程序员文章站
2022-06-30 09:08:19
...
我们已经了解了UDP的协议,那么现在我们就简单的做一个UDP的聊天室
我们定义一个 UDPChatDemo类,里面有主方法,还定义一个发送端的线程类SendThread,为了记录名字,所以用继承方式,一个接收端的线程类Receive,主线程启动1个接收端,2个发送端。
package com.wangLuo;
import java.io.*;
import java.net.*;
import java.util.Date;
/**
* @author 林高禄
* @create 2020-05-21-17:32
*/
public class UDPChatDemo{
public static void main(String[] args) {
Receive receive = new Receive();
Thread thread = new Thread(receive);
thread.setPriority(10);
thread.start();
createSent("林高禄");
createSent("徐辉强");
}
public static void createSent(String name){
SendThread sendThread = new SendThread(name);
sendThread.start();
}
}
class SendThread extends Thread{
public SendThread(String name) {
super(name);
}
@Override
public void run() {
try {
send();
} catch (IOException e) {
e.printStackTrace();
}
}
private void send() throws IOException {
DatagramSocket ds = new DatagramSocket();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = br.readLine())!= null){
String newLine = Thread.currentThread().getName()+"--"+new Date().toString()+"\n\r"+line;
byte[] bytes = newLine.getBytes();
DatagramPacket dp = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.1.55"),10);
ds.send(dp);
if("6了".equals(line)){
break;
}
}
ds.close();
}
}
class Receive implements Runnable{
@Override
public void run() {
try {
receive();
} catch (IOException e) {
e.printStackTrace();
}
}
private void receive() throws IOException {
DatagramSocket ds = new DatagramSocket(10);
while(true){
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
ds.receive(dp);
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println(new String(data,0,length));
}
}
}
聊天记录:
灰色的字是键盘输入的,白色的字是接收端输出的,这个聊天记录有点乱,还有空行什么的,是因为我们是用多线程来做,键盘输入的时候线程时随机的,并且输入完毕的时候,不一定到接收线程接收,有可能是另一个客户端线程,所以会出现这种情况,如果想改进,那就得把客户端单独拿出来另一个主方法,我们修改一下
接收端代码
package com.wangLuo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @author 林高禄
* @create 2020-05-22-17:18
*/
public class Receive {
public static void main(String[] args) throws IOException {
Receive receive = new Receive();
receive.receive();
}
private void receive() throws IOException {
DatagramSocket ds = new DatagramSocket(10);
while(true){
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
ds.receive(dp);
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println(new String(data,0,length));
}
}
}
客户端(发送端)代码
package com.wangLuo;
import java.io.*;
import java.net.*;
import java.util.Date;
/**
* @author 林高禄
* @create 2020-05-22-16:56
*/
public class Send {
private String name;
public Send(String name) {
this.name = name;
}
public static void main(String[] args) throws IOException {
Send send = new Send("林高禄");
send.send();
}
private void send() throws IOException {
DatagramSocket ds = new DatagramSocket();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = br.readLine())!= null){
String newLine = this.getName()+"--"+new Date().toString()+"\n\r"+line;
byte[] bytes = newLine.getBytes();
DatagramPacket dp = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.1.55"),10);
ds.send(dp);
if("6了".equals(line)){
break;
}
}
ds.close();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
启动接收端,启动客户端,然后把客户端的名字改一下,再启动一次客户端,此时一共启动了2个客户端
package com.wangLuo;
import java.io.*;
import java.net.*;
import java.util.Date;
/**
* @author 林高禄
* @create 2020-05-22-16:56
*/
public class Send {
private String name;
public Send(String name) {
this.name = name;
}
public static void main(String[] args) throws IOException {
Send send = new Send("徐辉强");
send.send();
}
private void send() throws IOException {
DatagramSocket ds = new DatagramSocket();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = br.readLine())!= null){
String newLine = this.getName()+"--"+new Date().toString()+"\n\r"+line;
byte[] bytes = newLine.getBytes();
DatagramPacket dp = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.1.55"),10);
ds.send(dp);
if("6了".equals(line)){
break;
}
}
ds.close();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
那么就进入连天了,以下是聊天记录
客户端的记录:
接收端的聊天记录:
上一篇: PHP常用的排序和查找算法
下一篇: 填充路径时使用的非零环绕规则