java服务器没有显示连接成功,多线程服务器聊天Java - 连接但没有消息

试图构建一个简单的多线程聊天服务器,通过命令提示符运行。客户端连接到服务器,服务器将保存多个客户端,但是当试图从一个客户端发送消息到另一个客户端,或者甚至通知另一个用户登录的客户端时,客户端命令提示符上都不会显示任何内容。多线程服务器聊天Java - 连接但没有消息

public class Server {

private static ServerSocket servSock;

private static Socket clientSock;

private static ArrayList clientList;

private static int IDcount = 0;

public static void main(String args[]){

// Get command line arguments.

if (args.length != 3) {

System.out.println("Required arguments: server port, block duration, timeout");

return;

}

int port = Integer.parseInt(args[0]);

int blockDur = Integer.parseInt(args[1]);

int timeout = Integer.parseInt(args[2]);

try{

servSock = new ServerSocket(port);

clientList = new ArrayList();

}

catch(IOException ex){

System.err.println(ex);

}

while (true) {

try {

clientSock = servSock.accept();

ClientThread thread = new ClientThread(clientSock);

clientList.add(thread);

thread.start();

} catch (IOException e) {

System.out.println(e);

}

}

}

private synchronized static void broadcast(String msg){

System.out.print(msg);

for(int i = 0; i < clientList.size(); i++){

ClientThread client = clientList.get(i);

client.send(msg);

}

}

synchronized static void unlist(int id){

for(int i = 0; i < clientList.size(); i++){

ClientThread thread = clientList.get(i);

if(thread.id == id){

clientList.remove(i);

return;

}

}

}

static class ClientThread extends Thread {

Socket sock;

BufferedReader tIn;

PrintWriter tOut;

int id;

String username;

String msg;

ClientThread(Socket sock){

id = IDcount++;

this.sock = sock;

try{

tIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));

tOut = new PrintWriter(sock.getOutputStream());

username = tIn.readLine();

broadcast(username + " logged in");

}

catch(IOException ex){

System.err.println(ex);

}

}

public void run(){

boolean loggedIn = true;

while(loggedIn){

try{

msg = tIn.readLine();

}

catch (IOException ex){

System.err.println(ex);

}

String[] parts = msg.split("\\s",2);

String type = parts[0];

客户端代码类似于

public class Client{

private static Socket clientSock;

private static BufferedReader in;

private static PrintWriter out;

private static Scanner scan;

public static void main(String[] args) throws IOException {

if (args.length != 2) {

System.out.println("Required arguments: server IP, server port");

return;

}

String host = args[0];

int port = Integer.parseInt(args[1]);

clientSock = new Socket(host, port);

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

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

scan = new Scanner(System.in);

new ListenFromServer().start();

boolean online = true;

System.out.println("Enter your username:");

String username = scan.nextLine();

out.println(username);

while(online){

System.out.println("> ");

String msg = scan.nextLine();

String[] parts = msg.split("\\s");

String type = parts[0];

send(msg);

if(type.equalsIgnoreCase("logout")){

online = false;

}

}

logoff();

}

static void send(String msg) throws IOException{

out.println(msg);

}

private static void logoff() throws IOException{

in.close();

out.close();

scan.close();

clientSock.close();

}

static class ListenFromServer extends Thread{

public void run(){

while(true){

try{

String msg = in.readLine();

System.out.println(msg);

}

catch(IOException ex){

System.err.println(ex);

}

}

}

}

}

+0

您是否试过单步执行代码?此外,你不想要的地方出现错误。 –

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值