关于java中控制台聊天的小程序望大神留步修改,谢谢啦!分数全给了

我这个错在哪,为什么聊天客户端收不到,服务端也收不到,最后希望是,客户端说一句话,然后其他的客户端都能够收到我说的这句话,谢谢啦!谢谢!服务端:publicclassSe... 我这个错在哪,为什么聊天客户端收不到,服务端也收不到,最后希望是,客户端说一句话,然后其他的客户端都能够收到我说的这句话,谢谢啦!谢谢!

服务端:
public class Server { Socket s = null; public static List<Chat> chats = new ArrayList<Chat>();
public static void main(String[] args) { new Server().start(); }
public void start() { try { // 指定端口开始监听 ServerSocket ss = new ServerSocket(3456); // 阻塞并接受客户端连接请求 while (true) { s = ss.accept(); Chat chat = new Chat(s); // 每接收一个客户端就起一个单独的聊天线程 new Thread(chat).start(); chats.add(chat); System.out.println("Join a client thread!"); System.out.println("Current client thread amount: " + chats.size()); } } catch (IOException e) { e.printStackTrace(); } }
}//聊天线程class Chat implements Runnable { BufferedReader br = null; BufferedWriter bw = null; Socket s = null;
public Chat(Socket s) { this.s = s; } //将客户端发来的消息打印并重发回去 @Override public void run() { try { br = new BufferedReader(new InputStreamReader(s.getInputStream())); bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String str = null; bw.write("hello"); while (true) { if ((str = br.readLine()) != null) { System.out.println("Client say: " + str); bw.write(str); bw.flush(); }
} } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); }
}
}}
}
客户端:
public class Client implements Runnable {
Socket s = null; BufferedWriter write = null; BufferedReader read = null; Scanner input = new Scanner(System.in);
public static void main(String[] args) { Client client = new Client(); client.start(); }
public void start() {
String str = null; try { s = new Socket("127.0.0.1", 3456); read = new BufferedReader(new InputStreamReader(s.getInputStream())); write = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); new Thread(this).start(); //负责发送消息给服务端 while (true) { str = input.next(); if (str.equalsIgnoreCase("bye")) { break; } write.write(str); write.flush(); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { write.close(); read.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } } } //负责接收服务端的消息 @Override public void run() { String str = null; try { while (true) { str = read.readLine(); System.out.println("Client say:" + str); } } catch (IOException e) { e.printStackTrace(); }
}
}
代码贴上来 格式就变这样了,贴到eclipse上就好了
展开
 我来答
匿名用户
推荐于2016-02-25
展开全部
因为你使用了BufferedReader的readLine方法,这个方法要读取到换行符才会停止阻塞。所以要在所有write的字符串末尾加上换行符("\r\n")
最后帮你改得:
服务器端:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server extends Thread{
Socket s = null;
public static List<Chat> chats = new ArrayList<Chat>();

public static void main(String[] args) {
new Server().start();
}

public void run() {
try {
// 指定端口开始监听
ServerSocket ss = new ServerSocket(3456);
// 阻塞并接受客户端连接请求
while (true) {
s = ss.accept();
Chat chat = new Chat(s);
// 每接收一个客户端就起一个单独的聊天线程
new Thread(chat).start();
chats.add(chat);
System.out.println("Join a client thread!");
System.out.println("Current client thread amount: " + chats.size());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

// 聊天线程
class Chat implements Runnable {
BufferedReader br = null;
BufferedWriter bw = null;
Socket s = null;

public Chat(Socket s) {
this.s = s;
try {
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
} catch (Exception e) {
e.printStackTrace();
}
}

// 将客户端发来的消息打印并重发回去
@Override
public void run() {
try {
String str = null;
bw.write("hello");
bw.write("\r\n");
bw.flush();
while (true) {
if ((str = br.readLine()) != null) {
System.out.println("Client say: " + str);
bw.write(str);
bw.write("\r\n");
bw.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
==============================
客户端:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client implements Runnable {
Socket s = null;
BufferedWriter write = null;
BufferedReader read = null;
Scanner input = new Scanner(System.in);

public static void main(String[] args) {
Client client = new Client();
client.start();
}

public void start() {
String str = null;
try {

s = new Socket("127.0.0.1", 3456);
read = new BufferedReader(new InputStreamReader(s.getInputStream()));
write = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
new Thread(this).start();
//负责发送消息给服务端
while (true) {
str = input.next();
if (str.equalsIgnoreCase("bye")) {
break;
}
write.write(str);
write.write("\r\n");
write.flush();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
write.close();
read.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

//负责接收服务端的消息
@Override
public void run() {
String str = null;
try {
while (true) {
str = read.readLine();
System.out.println("Client say:" + str);
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式