简体   繁体   中英

Reusing sockets in server-client

I am trying to create a client-server system: my server is a raspberry pi which is running a python webserver on it, and my client is on a different pc and is written is Java. The idea is that the server collects data and when it gets a request from a client, it sends the data to the client. My client should request the data, wait for 10 seconds and request again etc.

Currently this system is working, but after a day or so, the client starts getting a lot (but not continuously) socket timeouts. I think that this may be the case because for each request I create a new socket for communication and I think that after a day the sockets run out or something like that. This is the code the client executes every 10 seconds:

    public static String getData() throws Exception {
    TreeSet<Integer> primes = MathUtils.primesSieve(10000);
    try {
        String data = "";
        Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
        socket.setReuseAddress(true);
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        int msg = ColUtils.drawRandomlyWithReplacement(primes, 1, ArrayList::new).get(0);
        out.write(msg+"");
        out.flush();
        String input;
        while ((input = in.readLine()) != null) {
            data += input;
            if (!data.endsWith("#" + prod(msg))) {
                throw new Exception("WRONG ECHO");
            }
        }
        socket.close();

        return data;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

I tried fixing it by having a socket which is a member of the encapsulating class, but after a singe request the inputstream stopped working. Is there any way where I can keep using a single socket for ALL communications with the server? Or is this the recommended way of doing this sort of communication?

Try first closing the socket and input, output streams. As in your code there is no quarantee that you are releasing the acquired objects.

PrintWriter out = null;
BufferedReader in = null;
Socket socket = null;
try {
  ...//your statements
} catch (Exception ex) {
  //catch or whatever
} finally {
  if (out != null) out.close();
  if (in != null) in.close();
  if (socket != null) socket.close();
}

try to make the Socket object static If possible that would created only once and read the data every 10 sec Otherwise u can instantiate it before calling the getData method and then read it. Doing so will make only 1 copy of Socket.

And I don't think u are running out of ports. The reason might be quit simple that your Program is not receiving the data before the time out. and it is a normal case in a bad network

Socket generally waits indefinitely until it receives data if the timeout is not set Programmatically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM