【发布时间】:2015-03-06 14:48:20
【问题描述】:
我正在使用线程构建客户端-服务器应用程序,但由于某种原因,BufferedWriter 不会通过套接字将数据发送到客户端。
运行客户端后,我还收到了Socket Exception: Connection Reset。
我错过了什么?
服务器:
public class Servernew {
protected String username;
protected ServerSocket serverSocket;
protected static LinkedList<ClientThreadNew> clients;
public static LinkedList<ClientThreadNew> getClients() {
return clients;
}
public Servernew(int port) {
try {
serverSocket = new ServerSocket(port);
clients = new LinkedList<ClientThreadNew>();
System.out.println("Server is now running on port " + port);
while (true) {
Socket clientSocket = serverSocket.accept();
ClientThreadNew clientThread = new ClientThreadNew(
clientSocket, username);
clients.addLast(clientThread);
clientThread.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new Servernew(4444);
}
}
客户端线程:
public class ClientThreadNew extends Thread implements Runnable {
protected String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
protected Socket clientSocket;
protected BufferedReader inFromClientB;
protected BufferedWriter outToClient;
public ClientThreadNew(Socket clientSocket, String username)
throws IOException {
this.clientSocket = clientSocket;
this.username = username;
inFromClientB = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
outToClient = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
}
public void run() {
try {
while (true) {
String incomingMessage = inFromClientB.readLine();
if (incomingMessage != null) {
setUsername(incomingMessage);
outToClient.write("hii");
outToClient.newLine();
outToClient.flush();
}
}
} catch (IOException e) {
System.out.print(e);
}
}
}
客户端测试:
public class ClientTesst {
private static Socket socket;
private static BufferedWriter outToServer;
private String hostname;
private int portnum;
private static BufferedReader inFromServer;
public ClientTesst(String hostname, int portnum) throws IOException {
this.hostname = hostname;
this.portnum = portnum;
}
public void connect() throws UnknownHostException, IOException {
socket = new Socket(hostname, portnum);
System.out.println("Connection Established");
}
public String listUsers() throws IOException {
inFromServer = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String listOfUsers = inFromServer.readLine().toString();
return (listOfUsers);
}
public void sendMessage(String Msg) throws IOException {
outToServer = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
outToServer.write(Msg);
outToServer.newLine();
outToServer.flush();
}
public static void main(String[] args) throws Exception {
ClientTesst x = new ClientTesst("localhost", 4444);
x.connect();
x.sendMessage("client x");
ClientTesst y = new ClientTesst("localhost", 4444);
y.connect();
y.sendMessage("client y");
y.listUsers();
}
}
【问题讨论】:
-
您是否遇到任何错误?
-
@radimpe 我得到一个“套接字异常:连接重置”
-
考虑将您的错误日志添加到问题中(通过编辑它),您可能会得到一些反馈。确保添加尽可能多的堆栈跟踪以识别错误。
-
不是你的问题的答案,只是为了给你指出另一个解决方案,我写了 DataFetcher 在我搞乱套接字和 InputStreams 的那一天:sourceforge.net/p/tus/code/HEAD/tree/tjacobs/io DataFetcher 使用回调工作,所以您将 DataFetcher 连接到 InputStream 并注册一个或多个 DataListener。它被构建为同步或异步工作
标签: java sockets client server