Kahtaf Alam
Kahtaf Alam

Reputation: 463

Java socket keeps freezing

I'm trying to implement a simple server(java application) and client(android app), where the client sends a string about 10 times a second. Everything works fine for a minute or so, after which the server stops receiving messages from the client. Relevant code below.

ClientThread.java

public class ClientThread implements Runnable{
static Socket socket;
static String message = "";
InetAddress serverAddr;
BufferedOutputStream bos;

public ClientThread(String message){
    ClientThread.message = message;
}

@Override
public void run() {
    try{
        serverAddr = InetAddress.getByName(SERVER_IP);
        if(socket != null && socket.isConnected())socket.close();
        socket = new Socket(serverAddr, SERVER_PORT);
        bos = new BufferedOutputStream (socket.getOutputStream());
        OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
        osw.write(message);
        osw.flush();
        socket.shutdownOutput();
        socket.close();
    }catch (Exception e) {
    }
}
}

ServerThread.java

public class ServerThread extends Thread{
    private ServerSocket serverSocket;
    static String clientSentence;

   public ServerThread(int port) throws IOException, AWTException{
      serverSocket = new ServerSocket(port);
   }

   public void run() {
      while(true){
         try{
            Socket server = serverSocket.accept();
            BufferedReader d = new BufferedReader(new InputStreamReader(server.getInputStream()));
            clientSentence = d.readLine();
            System.out.println(clientSentence);
            server.close();
         }catch(IOException e){
            e.printStackTrace();
            break;
         }
      }
   }
}

ClientThread.java is called about 10 times a second using:

Thread clientThread = new Thread(new ClientThread(message));
clientThread.start();

ServerThread.java is initialized and started using:

t = new ServerThread(8888);
t.start();

Any thoughts on why this would freeze after running for a bit? The only way to fix it is to restart the server, after which the same problem happens again after a minute. I spent a lot of time researching this issue but was unable to find a solution.

EDIT: I figured out the server freezes at the clientSentence = d.readLine(); part. Any idea why?

Upvotes: 1

Views: 1510

Answers (2)

user207421
user207421

Reputation: 310860

Your thread never exits and you keep creating new ones. So you run out of something: thread space, sockets, FDs, ...

This is all wrong. Either your thread should loop or you should create a new one. Not both.

Also:

  1. You should use a single connection, not a new one per message.
  2. You are reading lines but to sending them, unless the data already contains a newline, which it shouldn't.

Upvotes: 0

Aubin
Aubin

Reputation: 14853

60 connection per second, one minute running: 3600 connections per minute.

Closing a socket doesn't release immediately the associated file descriptor. You may run out of resource at OS layer.

Try to run netstat on server side to see the active, pending and closed connections.

You may read this post on SU.

Upvotes: 1

Related Questions