我正在使用线程构建客户端 - 服务器应用程序,但出于某种原因,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);
}
}
ClientThread:
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);
}
}
}
ClientTest:
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();
}
}
答案 0 :(得分:0)
在ClientTesst
课程中,你有这个:
private static Socket socket;
private static BufferedReader inFromServer;
private static BufferedWriter outToServer;
并且因为所有这些变量都是static
,这意味着客户x
和y
将使用相同的socket
,outToServer
和{{1} }。
将此变量作为实例变量(通过删除inFromServer
),以便static
的每个实例都具有其套接字和流。
答案 1 :(得分:0)
以下作品 -
class ClientThreadNew extends Thread implements Runnable {
protected String username;
protected Socket clientSocket;
protected BufferedReader inFromClientB;
protected BufferedWriter outToClient;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public ClientThreadNew(Socket clientSocket, String username) throws IOException {
this.clientSocket = clientSocket;
this.username = username;
}
public void run() {
try {
outToClient = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
inFromClientB = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String incomingMessage, outgoingMessage;
while ((incomingMessage = inFromClientB.readLine()) != null) {
setUsername(incomingMessage);
System.out.println("From client: " + incomingMessage);
outgoingMessage = "hii";
Thread.sleep(2000);
outToClient.write(outgoingMessage);
outToClient.newLine();
outToClient.flush();
System.out.println("From server: " + outgoingMessage);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class ClientTest {
private Socket socket;
private BufferedReader inFromServer;
private BufferedWriter outToServer;
private String hostname;
private int portnum;
public ClientTest(String hostname, int portnum) throws IOException {
this.hostname = hostname;
this.portnum = portnum;
}
public void connect() throws IOException {
socket = new Socket(hostname, portnum);
outToServer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Connection Established");
}
public String listUsers() throws IOException {
return inFromServer.readLine();
}
public void sendMessage(String Msg) throws IOException {
outToServer.write(Msg);
outToServer.newLine();
outToServer.flush();
}
public static void main(String[] args) throws Exception {
ClientTest x = new ClientTest("localhost", 4444);
x.connect();
x.sendMessage("client x");
String messageFromServer;
while ((messageFromServer = x.listUsers()) != null) {
System.out.println("From server: " + messageFromServer);
Thread.sleep(2000);
x.sendMessage("client x");
System.out.println("From client: client x");
}
}
}