Java Socket实现简易聊天室

网友投稿 284 2023-01-26

Java Socket实现简易聊天室

java-Socket编程实现简易聊天室(TCP),供大家参考,具体内容如下

实现一个服务器接收多个客户端

测试:

首先启动服务器,然后启动三个客户端,输入三个不同的用户名,分别在聊天室发消息

看其他客户端是否能接收到消息

效果如下图:

一号发消息

可以在二号和三号客户端接收到消息

服务器设计

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;

import java.util.List;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

public class Server1 {

static public List list = new ArrayList();

static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

public static void main(String[] args) {

//创建一个ServerSocket监听端口

try (ServerSocket server = new ServerSocket(5678)) {

while (true) { // 利用死循环不停的监听端口

Socket s = server.accept();// 利用Socket服务器的accept()方法获取客户端Socket对象。

addClient(s);

}

} catch (IOException e) {

e.printStackTrace();

}

}

// 添加客户端

private static void addClient(Socket s) {

String name;

try {

BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

name = in.readLine();

Client c = new Client(name, s);// 创建客户端处理线程对象

System.out.println(name);

list.add(c);

cachedThreadPool.execute(c);

} catch (IOException e) {

e.printStackTrace();

}

}

static class Client implements Runnable {

String name; // 客户端名字

Socket s = null; // 保存客户端Socket对象

BufferedReader in;

PrintWriter out;

Client(String name, Socket s) {

this.s = s;

this.name = name;

try {

in = new BufferedReader(new InputStreamReader(s.getInputStream()));

out = new PrintWriter(s.getOutputStream());

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public void run() {

try {

while (true) {

String str = in.readLine();

for (int j = 0; j < list.size(); j++) {

Server1.Client c = list.get(j);

if (c != this) {

System.out.println(str);

c.send(str+"-|1|2|-"+name);

}

}

if (str.equals("end"))

break;

}

try {

s.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

public void send(String str) throws IOException {

out.println(str);

out.flush();

}

}

}

客户端设计

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

import java.util.Scanner;

public class Client1 {

public static String name = null;

static Socket socket = null;

static BufferedReader in;

static PrintWriter out;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("输入用户名:>");

name = sc.nextLine();

try {

// socket = new Socket("116.62.173.212", 5678);

socket = new Socket("127.0.0.1", 5678);

System.out.println(socket);

System.out.println("已连接服务器>");

out = new PrintWriter(socket.getOutputStream());

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

Thread receive = new Thread(new receiveThread());

receive.start();

out.println(name);

out.flush();

String msg;

while (true) {

// System.out.print("你说:>");

msg = sc.nextLine();

out.println(msg);

out.flush();

if (msg.equalsIgnoreCase("end"))

break;

}

} catch (IOException e) {

e.printStackTrace();

}

}

static class receiveThread implements Runnable{

@Override

public void run() {

try {

while (socket!=null) {

String str = in.readLine();

String message = str.split("-\\|1\\|2\\|-")[0];

String name = str.split("-\\|1\\|2\\|-")[1];

System.out.println(name + ":>" + message);

}

}catch(Exception e) {

e.printStackTrace();

}

}

}

}

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

上一篇:免费api调试平台(api开发平台)
下一篇:快递100开放api接口(快递100接口怎么收费)
相关文章

 发表评论

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