Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

How to test the backlog function of ServerSocket class

Hi,
I have written a client server simple Java Application.
It uses Java ServerSocket Class
int backLog = 1;
ServerSocket serverSocket = new ServerSocket(port, backLog);

Open in new window

As per my understanding the server will accept incoming request and return the response.
And during this request response cycle if server is in the middle of processing an earlier request. It will put that request in a queue. The length of the queue is defined by backLog. And if the number of request exceeds this backLog then it will throw some exception for any further request.

Hope the above understanding is correct.
Now i put backlog = 1 purposefully so as to see the case where the exception actually occurs. But since servers are too fast in responding. How do i achieve this.
Is there any utility which can help me sending multiple simultaneous request to the server.

Thanks
Avatar of Rohit Bajaj
Rohit Bajaj
Flag of India image

ASKER

I noticed one behavior which i dont entirely understand. Here is the error that server threw :
Server read error
java.net.SocketException: Connection reset
   at java.base/java.net.SocketInputStream.read(SocketInputStream.java:186)
   at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
   at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
   at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
   at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
   at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185)
   at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
   at java.base/java.io.BufferedReader.readLine(BufferedReader.java:326)
   at java.base/java.io.BufferedReader.readLine(BufferedReader.java:392)
   at Worker.run(JokeServer.java:28)

Open in new window


I had two programs written like :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class Test1 {
    public static void main(String[] args) {
        Socket socket;
        BufferedReader bufferedReader;
        PrintStream printStream;
        String textFromServer;
        try {
            socket = new Socket("localhost", 4545);
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            printStream = new PrintStream(socket.getOutputStream());
            printStream.flush();
            for (int i = 0; i <= 1; i++) {
                textFromServer = bufferedReader.readLine();
                if (textFromServer != null)
                    System.out.println(textFromServer);
            }
        } catch (IOException ioe) {
            System.out.println("Socket error.");
            ioe.printStackTrace();
        }
    }

}


Open in new window

and
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class Test2 {
    public static void main(String[] args) {
        Socket socket;
        BufferedReader bufferedReader;
        PrintStream printStream;
        String textFromServer;
        try {
            socket = new Socket("localhost", 4545);
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            printStream = new PrintStream(socket.getOutputStream());
            printStream.flush();
        } catch (IOException ioe) {
            System.out.println("Socket error.");
            ioe.printStackTrace();
        }
    }

}


Open in new window

Initially i run the server code.
Then i run Test1.java
Till now its fine.
But whenever i run Test2.java
I get that exception.
Also i intentionally didnt closed the socket was thinking that could help me gain understanding of the backlog parameter. But The test2.java still errored out when i changed the backlog to 2.

What could be the reason for this

Thanks



Avatar of krakatoa
The (server) port is already in use on your loopback I believe, and so you won't be able to host more than one client on it.
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Until you use another networked machine to run the second client, your 'testing' of the backlog parameter remains a moot point.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you have no more points you need to ask about this subject, it would be appreciated if you would now close this question.