简体   繁体   English

为什么关闭客户端后服务器停止运行?

[英]Why the server stops running when I close the Client?

I am writing my first Client-Server program in Java using Sockets. 我正在使用Socket用Java编写我的第一个Client-Server程序。 I am using Eclipse as the IDE. 我正在使用Eclipse作为IDE。 When I am testing the communication between both programs (server and client) I run first the server using the command prompt and then I run the client in Eclipse. 在测试两个程序(服务器和客户端)之间的通信时,我首先使用命令提示符运行服务器,然后在Eclipse中运行客户端。 Everything works fine, I can read from and write to the socket, however, when I close the client program in Eclipse, the server program closes too. 一切正常,我可以读取和写入套接字,但是,当我在Eclipse中关闭客户端程序时,服务器程序也会关闭。 Why is this happening? 为什么会这样呢? The server is supposed to be running by itself in the command prompt, it is not dependent on a client. 该服务器应该在命令提示符下自行运行,而不依赖于客户端。

Also I would like to know if there is any possibility I can run both programs in Eclipse instead of opening the server in the command prompt first. 我也想知道是否有可能我可以在Eclipse中运行这两个程序,而不是先在命令提示符下打开服务器。

Here is my code for both programs: 这是我的两个程序的代码:

Server: 服务器:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerPrg {

public static void main(String[] args) {

    System.out.println("Server is running.....");

    try {
        ServerSocket socketSer = new ServerSocket(4444);
        Socket clientSocket = socketSer.accept();

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = (new BufferedReader (new InputStreamReader(clientSocket.getInputStream())));

        BufferedReader stdIn = (new BufferedReader (new InputStreamReader(System.in)));

        System.out.println("Client: " + in.readLine());

        String input ;
        while((input = stdIn.readLine()) != null)
            {   out.println(input);
                System.out.println("Client: " + in.readLine());
            }
    }

    catch (Exception e) {System.out.println("CAN'T CREATE SERVERSOCKET. PROBABLY THE PORT IS BEING USED   " + e);}

} //end main
} //end public class

Client: 客户:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientPrg {

public static void main(String[] args) {

    int portNumber = 4444;

    try {
        Socket clientSocket = new Socket("127.0.0.1", portNumber);
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

        String input;
        while ((input = stdIn.readLine()) != null)
        {
            out.println(input);
            System.out.println("Server: " + in.readLine());
        }


    } catch(Exception e)
    {
        System.out.println(e);
        System.out.println("CAN'T CONNECT TO THE SERVER");
    }

} //end main
} // end public class

Your server lacks a loop around accepting client sockets. 您的服务器缺少接受客户端套接字的循环。

This means that after your client socket is accepted, it will exit because there is no flow control element that will have it attempt to accept a second client socket. 这意味着在接受您的客户端套接字后,它将退出,因为没有流控制元素会使其尝试接受另一个客户端套接字。

A simple loop around accepting client sockets is probably not exactly what you want. 围绕接受客户端套接字的简单循环可能并不是您想要的。 That is because there will be only one Thread in the solution, which means that while a client is being handled, other clients won't be able to be accepted. 那是因为解决方案中只有一个线程,这意味着在处理一个客户端时,其他客户端将无法被接受。

There are many ways to handle the situation above. 有很多方法可以解决上述情况。 One of the simplest is to create a thread for every accepted client to handle the client's communications. 最简单的方法之一是为每个接受的客户端创建一个线程来处理客户端的通信。 While this is initially simple, it does not scale very well. 尽管这最初很简单,但是扩展性不是很好。 With a large number of clients, the thread count will rise, and most computers can handle many more network connections than threads. 随着大量的客户端,线程数将增加,并且大多数计算机可以处理的网络连接要多于线程。

The scope of talking about services that scale well is far to big to address here; 讨论可扩展性好的服务的范围在这里远远不够。 but, after you get familiar with one thread per client processing, start looking at Java NIO. 但是,在熟悉了每个客户端处理的一个线程之后,就开始研究Java NIO。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当webservice停止工作时,为什么我看到很多CLOSE_WAIT状态的套接字? - Why am i seeing lots of sockets in CLOSE_WAIT status when webservice stops working? 每当我关闭时,vps ssh 之后 jar 停止运行 - vps ssh whenever i close, after time jar stops running 服务器的套接字不输出数据单元客户端代码停止运行 - Server's socket not outputting data unit client code stops running 如何知道客户端何时停止ping Java服务器? - How to know when client stops ping-ing Java server? 运行长的MySQL线程时,Spigot服务器“停止响应” - Spigot Server “stops responding ” when running long MySQL thread 使用Java ProcessBuilder运行haskell可执行文件:为什么hGetLine仅在关闭outputStream时返回? - Running haskell executable using Java ProcessBuilder: Why does hGetLine only return when I close outputStream? Java中的多客户端服务器,也许线程无法正常运行,或者我不知道,无法找出原因 - Multi-Client Server in Java, Maybe threads not running properly or i dont know , cant find out why 何时使用Java日志记录和正在运行的Jetty服务器关闭FileHandler - When to close the FileHandler using java logging with a running jetty server 每当我修改服务器时,SSE客户端都会停止工作(服务器发送事件) - SSE Client Stops Working Anytime I Modify the Server (Server Sent Event) 音乐停止时强制关闭-Android - force close when music stops - Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM