简体   繁体   中英

Maximum socket connections available

I am trying my hand at socket programming. I built a simple echo server that prints the client text on the screen and sends back a thank you message to the client. However when I run the client (which individually spawns 10000 requests in a loop) sometimes i get "connection refused" exceptions in some client threads. Sometimes all go through without any exception.

Following is my server code :

public class WebServer {
    static int hitCount = 0;

    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(7777, 10000);
        while (true) {
            Socket defaultSocket = serverSocket.accept();
            new Thread(new ServerSlave(defaultSocket)).start();
            System.out.println("Size is :" + hitCount);
        }

    }
}

class ServerSlave implements Runnable {
    Socket clientSocket;

    public ServerSlave(Socket socket) {
        clientSocket = socket;
        WebServer.hitCount++;
    }

    public void run() {

        try {

            DataInputStream inputStream = new DataInputStream(clientSocket.getInputStream());
            DataOutputStream outputStream = new DataOutputStream(clientSocket.getOutputStream());
            System.out.println(inputStream.readUTF());
            outputStream.writeUTF("Thank you for contacting the web server");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                clientSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Following is my client code :

public class Client {
    static int excepCount=0;
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 10000; i++) {
            new Thread(new Worker("" + i)).start();
        }
        Thread.sleep(10000);
        System.out.println( Client.excepCount);
    }
}


class Worker implements Runnable {
    String clientName;

    public Worker(String name) {
        clientName = name;
    }

    public void run() {
        System.out.println("Process started for : " + clientName);
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1", 7777);
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
            outputStream.writeUTF("Hello socket. Client number " + clientName + "here");
            InputStream inFromServer = socket.getInputStream();
            DataInputStream in =
                    new DataInputStream(inFromServer);
            System.out.println("Server says " + in.readUTF());
            System.out.println("Closing socket");

        } catch (IOException e) {
            Client.excepCount++;
            e.printStackTrace();
        }finally{
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Not sure what I might be doing wrong. Any suggestions ?

You're expecting too much. It just isn't realistic for a client to form 10,000 connections at maximum speed. You're forgetting about the TIME_WAIT state, and the fact that there are only 64k minus several dozen client-side ports available. It isn't a realistic test. If you want to load-test your server you will need quite a few client hosts, or a longer interval between connections.

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