基于UDP實現(xiàn)聊天室功能
本文實例為大家分享了UDP實現(xiàn)聊天室功能的具體代碼,供大家參考,具體內(nèi)容如下
項目結(jié)構(gòu)

data.java
package udp;
import java.net.InetAddress;
public class data {
InetAddress Address;
int Port;
public InetAddress getAddress() {
return Address;
}
public void setAddress(InetAddress address) {
Address = address;
}
public int getPort() {
return Port;
}
public void setPort(int port) {
Port = port;
}
}
服務(wù)器端
Server.java
package udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.ArrayList;
public class Server {
DatagramSocket socket = null;
ArrayList<data> client;
public Server() {
try {
socket = new DatagramSocket(8888);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
client = new ArrayList<data>();
}
public void s_r(){
try {
while(true) {
byte[] buf = new byte[3000];
//接收數(shù)據(jù)的數(shù)據(jù)包
DatagramPacket packet = new DatagramPacket(buf,buf.length);
socket.receive(packet);
//地址
InetAddress clientAddress = packet.getAddress();
//端口號
int clientPort = packet.getPort();
data d = new data();
d.setAddress(clientAddress);
d.setPort(clientPort);
int i=0;
//判斷客戶端集合里是否存在發(fā)送新消息的客戶端
for(;i<client.size();i++) {
if(client.get(i).getAddress().equals(clientAddress)&&client.get(i).getPort()==clientPort) {
break;
}
}
if(i==client.size()) {
client.add(d);
}
String s=new String(packet.getData()).trim()+"來自:"+clientAddress.getHostAddress()+":"+clientPort;
System.out.println(s);
//把信息發(fā)給每個客戶端
for(data c:client) {
try {
//地址
InetAddress cAddress = c.getAddress();
//端口號
int cPort = c.getPort();
buf = s.getBytes();
//創(chuàng)建要發(fā)送的數(shù)據(jù)包
packet = new DatagramPacket(buf,buf.length,cAddress,cPort);
socket.send(packet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(socket!=null)socket.close();
}
}
public static void main(String[] args) {
Server s = new Server();
s.s_r();
}
}
客戶端
package udp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class Client {
DatagramSocket socket = null;
DatagramPacket packet;
InetAddress address = null;
Client(){
try {
socket = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void s_r() {
try {
// 把表示服務(wù)器端IP地址的字符串轉(zhuǎn)換成InetAddress對象
address = InetAddress.getByName("127.0.0.1");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String s = "登陸";
byte[] b = s.getBytes();
packet = new DatagramPacket(b,b.length,address,8888);
try {
socket.send(packet);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//發(fā)送消息的線程
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
String sendStr;
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
try {
while((sendStr = stdIn.readLine())!=null) {
byte[] buf = sendStr.getBytes();
packet = new DatagramPacket(buf,buf.length,address,8888);
socket.send(packet);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
//接收消息的線程
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
byte[] buf_1 = new byte[3000];
// 使用空字節(jié)數(shù)組構(gòu)造空數(shù)據(jù)包
DatagramPacket packet = new DatagramPacket(buf_1,buf_1.length);
try {
while(true) {
socket.receive(packet);
String received = new String(packet.getData(),0,packet.getLength()).trim();
System.out.println("接收到的信息:"+received);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
public static void main(String[] args) {
Client c = new Client();
c.s_r();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java IText異常NoClassDefFoundError: org/bouncycastle
在使用Java進(jìn)行PDF文檔操作時,iText是一個非常強(qiáng)大的庫,然而,在實際開發(fā)過程中,可能會遇到一些異常情況,其中之一就是??NoClassDefFoundError: org/bouncycastle/asn1/ASN1Encodable??,本文將探討這個錯誤的原因及其解決方案,需要的朋友可以參考下2025-02-02
MyEclipse去除網(wǎng)上復(fù)制下來的代碼帶有的行號(正則去除行號)
這篇文章主要介紹了MyEclipse去除網(wǎng)上復(fù)制下來的代碼帶有的行號(正則去除行號)的相關(guān)資料,需要的朋友可以參考下2017-10-10
Flink開發(fā)IDEA環(huán)境搭建與測試的方法
這篇文章主要介紹了Flink開發(fā)IDEA環(huán)境搭建與測試的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
@FeignClient?path屬性路徑前綴帶路徑變量時報錯的解決
這篇文章主要介紹了@FeignClient?path屬性路徑前綴帶路徑變量時報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
詳解在Spring?Boot中使用數(shù)據(jù)庫事務(wù)
本篇文章主要介紹了詳解在Spring?Boot中使用數(shù)據(jù)庫事務(wù),具有一定的參考價值,感興趣的小伙伴們可以參考一下<BR>2017-05-05
Spring?Boot?使用斷言讓你的代碼在上線前就通過“體檢”(最新整理)
斷言是一種編程技巧,用于在代碼中插入檢查點,驗證程序的狀態(tài)是否符合預(yù)期,如果斷言失敗,程序會拋出一個錯誤,幫助你快速發(fā)現(xiàn)和修復(fù)bug,本文給大家介紹Spring?Boot?斷言:讓你的代碼在上線前就通過“體檢”,感興趣的朋友一起看看吧2025-03-03
Java FineReport報表工具導(dǎo)出EXCEL的四種方式
這篇文章主要介紹了Java FineReport報表工具導(dǎo)出EXCEL的四種方式的相關(guān)資料,需要的朋友可以參考下2016-03-03

