Java TCP Server doesn't accept client request after receiving 16384 bytes MAC OS

I'm conducting an experiment to see how long it takes the TCP in java. First I start the server. Then call the function client_tcp many times, more than 50000 times. And measure the time it takes to connect, and send and receive 1 byte. When the server get more than 16384 requests (sometimes varies), the client can't connect to the server.

I don't know if it is because of the receive buffer size in the server socket. In my case, ss.getReceiveBufferSize() = 131072.

Here is the code:

public synchronized void server_tcp(int port) {
    ServerSocket ss;
    Socket       so;
    InputStream  is;
    OutputStream os;

    try {           
        ss = new ServerSocket(port);
    } catch (Exception e) {
        System.out.println("Unable to connect to port " + port +
                " TCP socket.");
        return;
    }

    while (true) {
        try {
            so = ss.accept();
            is = so.getInputStream();
            os = so.getOutputStream();
            int ch = is.read();
            os.write(65);
            so.close();
        } catch (IOException e) {
            System.out.println("Something went wrong.");
        } catch (InterruptedException e) {
            System.out.println("Bye.");
        }
    }
}

public void client_tcp(String host, int port) {

    Socket       so = null;
    InputStream  is = null;
    OutputStream os = null;

    try {
        so = new Socket(host, port);
    } catch (UnknownHostException e) {
        System.err.println("Error Host not found.");
        return;
    } catch (IOException e) {
        Syste.err.println("Error Creating socket.");
        return;
    }

    try {
        os = so.getOutputStream();
        is = so.getInputStream();

        os.write(65);

        is.read();

        os.close();
        is.close();
        so.close();
    } catch (IOException e) {
        System.err.println("Error.");
        return;
    }
}

What's wrong?

Thank you.

Answers


You are creating a massive number of sockets almost at once and the OS is not having time enough to release them. You could add a tiny delay (to be experimentally tuned) to the loop that invokes the client_tcp() method.

for(int i=0; i<50000; i++) {
    new SocketReuse().client_tcp("127.0.0.1", 4444);
    Thread.sleep(2); // 2 milliseconds delay
}

Need Your Help

Import code modules programmatically to Excel workbook

c# excel vba

I create a workbook dynamically. I want to import code modules (Module1.bas) and (UserForm1.frm) when creating it.

php: how to identify empty row in textarea

php string textarea row

I have a textarea and I'm trying to check if row is not empty. Seems these doesn't work:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.