Here's the code for server side :
这是服务器端的代码:
public class EchoServer {
public static void main(String[] args) {
int port = 8080;
try {
ServerSocket server = new ServerSocket(port);
Socket cliSocket = server.accept();
Scanner in = new Scanner(cliSocket.getInputStream());
PrintWriter write = new PrintWriter(cliSocket.getOutputStream(),true);
String message;
while((message=in.nextLine()) != null){
write.println(message+" added");
}
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and here's the client side :
这是客户端:
public class EchoClient {
public static void main(String[] args) {
String ip = "localhost";
int port = 8080;
try {
Socket client = new Socket(ip, port);
PrintWriter write = new PrintWriter(client.getOutputStream(), true);
Scanner in = new Scanner(client.getInputStream());
Scanner read = new Scanner(System.in);
String input;
while((input=read.nextLine()) != null){
write.println(input);
System.out.println("sent by server:" + in.nextLine());
}
write.close();
in.close();
read.close();
client.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Now when I run the server and then the client, it works. But if close the client app, and i run it once again, the server won't allow connection.
现在当我运行服务器然后运行客户端时,它可以运行。但是如果关闭客户端应用程序,并且我再次运行它,服务器将不允许连接。
What is the solution in situations like this?
这种情况下的解决方案是什么?
2 个解决方案
#1
The easiest solution is that every time you accept a connection, you launch a new thread to handle the client. That would allow you to handle any number of clients, and also deal with general TCP issues like sockets that are stuck open when clients are killed.
最简单的解决方案是每次接受连接时,都会启动一个新线程来处理客户端。这将允许您处理任意数量的客户端,并且还处理一般TCP问题,例如在客户端被杀时被卡住的套接字。
Something like this:
像这样的东西:
public class EchoServer {
public static void main(String[] args) {
int port = 8080;
ServerSocket server = new ServerSocket(port);
while (true) {
//wait for next client to connect
Socket cliSocket = server.accept();
//hand off socket to another thread
MyHandler handler = new MyHandler(cliSocket);
Thread clientHandler = new Thread(handler);
clientHandler.start();
}
}
}
public class MyHandler implements Runnable {
public MyHandler(Socket cliSocket)
{
//store socket
}
@override
public void run()
{
while(true) {
//handle client comms
}
}
}
#2
Your server program only accepts one client connection and exits after handling the connection.
您的服务器程序只接受一个客户端连接,并在处理连接后退出。
If you want it to repeatedly accept client connections, you need to use a loop around the code you have in main()
如果您希望它重复接受客户端连接,您需要使用main()中的代码循环
#1
The easiest solution is that every time you accept a connection, you launch a new thread to handle the client. That would allow you to handle any number of clients, and also deal with general TCP issues like sockets that are stuck open when clients are killed.
最简单的解决方案是每次接受连接时,都会启动一个新线程来处理客户端。这将允许您处理任意数量的客户端,并且还处理一般TCP问题,例如在客户端被杀时被卡住的套接字。
Something like this:
像这样的东西:
public class EchoServer {
public static void main(String[] args) {
int port = 8080;
ServerSocket server = new ServerSocket(port);
while (true) {
//wait for next client to connect
Socket cliSocket = server.accept();
//hand off socket to another thread
MyHandler handler = new MyHandler(cliSocket);
Thread clientHandler = new Thread(handler);
clientHandler.start();
}
}
}
public class MyHandler implements Runnable {
public MyHandler(Socket cliSocket)
{
//store socket
}
@override
public void run()
{
while(true) {
//handle client comms
}
}
}
#2
Your server program only accepts one client connection and exits after handling the connection.
您的服务器程序只接受一个客户端连接,并在处理连接后退出。
If you want it to repeatedly accept client connections, you need to use a loop around the code you have in main()
如果您希望它重复接受客户端连接,您需要使用main()中的代码循环