为什么我不能多次 运行 这个简单的服务器-客户端应用程序的客户端?
Why can't i run the client side of this simple server-client app more than once?
这是服务器端的代码:
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();
}
}
}
这是客户端:
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();
}
}
}
现在,当我 运行 服务器和客户端时,它可以正常工作。但是如果关闭客户端应用程序,我 运行 它再次,服务器将不允许连接。
遇到这种情况有什么解决办法?
您的服务器程序只接受一个客户端连接,并在处理完连接后退出。
如果你想让它重复接受客户端连接,你需要在你的代码周围使用循环 main()
最简单的解决方案是每次接受连接时,启动一个新线程来处理客户端。这将允许您处理任意数量的客户端,还可以处理一般的 TCP 问题,例如客户端被终止时卡在打开状态的套接字。
像这样:
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
}
}
}
这是服务器端的代码:
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();
}
}
}
这是客户端:
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();
}
}
}
现在,当我 运行 服务器和客户端时,它可以正常工作。但是如果关闭客户端应用程序,我 运行 它再次,服务器将不允许连接。
遇到这种情况有什么解决办法?
您的服务器程序只接受一个客户端连接,并在处理完连接后退出。
如果你想让它重复接受客户端连接,你需要在你的代码周围使用循环 main()
最简单的解决方案是每次接受连接时,启动一个新线程来处理客户端。这将允许您处理任意数量的客户端,还可以处理一般的 TCP 问题,例如客户端被终止时卡在打开状态的套接字。
像这样:
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
}
}
}