基于UDP实现聊天室功能

网友投稿 764 2023-01-27

基于UDP实现聊天室功能

基于UDP实现聊天室功能

本文实例为大家分享了UDP实现聊天室功能的具体代码,供大家参考,具体内容如下

项目结构

data.java

package udp;

import java-.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;

}

}

服务器端

Server.java

package udp;

import java.io.IOException;

import java-.DatagramPacket;

import java-.DatagramSocket;

import java-.InetAddress;

import java-.SocketException;

import java.util.ArrayList;

public class Server {

DatagramSocket socket = null;

ArrayList client;

public Server() {

try {

socket = new DatagramSocket(8888);

} catch (SocketException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

client = new ArrayList();

}

public void s_r(){

try {

while(true) {

byte[] buf = new byte[3000];

//接收数据的数据包

DatagramPacket packet = new DatagramPacket(buf,buf.length);

socket.receive(packet);

//http://地址

InetAddress clientAddress = packet.getAddress();

//端口号

int clientPort = packet.getPort();

data d = new data();

d.setAddress(clientAddress);

d.setPort(clientPort);

int i=0;

//判断客户端集合里是否存在发送新消息的客户端

for(;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);

//把信息发给每个客户端

for(data c:client) {

try {

//地址

InetAddress cAddress = c.getAddress();

//端口号

int cPort = c.getPort();

buf = s.getBytes();

//创建要发送的数据包

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-.DatagramPacket;

import java-.DatagramSocket;

import java-.InetAddress;

import java-.SocketException;

import java-.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 {

// 把表示服务器端IP地址的字符串转换成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();

}

//发送消息的线程

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];

// 使用空字节数组构造空数据包

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();

}

}

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);

//把信息发给每个客户端

for(data c:client) {

try {

//地址

InetAddress cAddress = c.getAddress();

//端口号

int cPort = c.getPort();

buf = s.getBytes();

//创建要发送的数据包

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-.DatagramPacket;

import java-.DatagramSocket;

import java-.InetAddress;

import java-.SocketException;

import java-.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 {

// 把表示服务器端IP地址的字符串转换成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();

}

//发送消息的线程

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];

// 使用空字节数组构造空数据包

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();

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:混合app开发vue(混合app开发前端用什么技术)
下一篇:免费app运营模式(App的运营模式)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~