Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Multiple ServerSocket problem

    Hi. I have a main class which instantiates two instances of a class named 'Server' and each of these instantiated servers listen on different ports (using threads). Each of these instantiated servers wait for 4 clients before executing some intended codes.
    The client's GUI retrieves the lists of the instantiated servers and the ports they are listening to. And depending on the user choice, the client reconnected to the chosen server's port.
    The client is made to print out the port it connects to.

    The problem is that when I open a few client windows, and have the first 4 connect to server1, everything works fine. But then when the rest of the clients try to connect to server2, non of them are able to establish a connection even though the port they were trying to connect was that of server2.

    There are a lot of codes, so please do tell me which part would u like to see.
    Anything logically wrong here?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Multiple ServerSocket problem

    none of them are able to establish a connection
    Is server2 listening on the port (calling accept()) when those clients try to connect?

    What if you do server2 first and then server1?

  3. #3
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Multiple ServerSocket problem

    server1 and server2 are listening on different ports and both of them do call the method accept() in the thread that waits for clients to connect.

    I didn't want to complicate the explanation, so I didn't say beforehand but, when I try to connect to server 2, only the first client would connect, the rest of the clients trying to connect to server 2 just hang there and don't establish any connection to the server.

    When all of the servers are closed, the clients which hung would print out "Connection reset".
    Last edited by anoorally; 02-23-2012 at 11:47 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Multiple ServerSocket problem

    What do the servers do after they return from the call to accept()?
    They should immediately create a new thread, pass the socket to the new thread and loop back to call accept() again.

    Do you have printlns after each accept() to show when they return?

  5. #5
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Multiple ServerSocket problem

    This is part of the code of the thread that listens for client on the server. It waits for 4 clients to connect:

    Java Code:
    while (clients.size()!=4) {
                        System.out.println("Waiting for connection...");
    
                        Socket client = server.accept();
                        clients.add(client);
    
                        System.out.println("\tClient " + clients.size() + " connected!");
    
                        DataOutputStream out = new DataOutputStream(client.getOutputStream());
                        outStream.add(out);
                        Objectout.add(new ObjectOutputStream(out));
    
                        DataInputStream in = new DataInputStream(client.getInputStream());
                        inStream.add(in);
                        Objectin.add(new ObjectInputStream(in));
                        
                        String turn = "userTurn "+ (Objectout.size()-1);
                        
                        Objectout.get(Objectout.size() -1).writeObject(turn);
                    }
    The above code save the client's reference and there input & output streams respective in ArrayList.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Multiple ServerSocket problem

    You need a loop and a thread to keep the current connection from freezing the server.

  7. #7
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Multiple ServerSocket problem

    Thats actually how it is. Here is the whole thread class with the while loop included:

    Java Code:
        public static class listenClient implements Runnable {
    
    
            public void run() {
                try {
                    while (clients.size()!=4) {
                        System.out.println("Waiting for connection...");
    
    
                        Socket client = server.accept();
                        clients.add(client);
    
    
                        System.out.println("\tClient " + clients.size() + " connected!");
    
    
                        DataOutputStream out = new DataOutputStream(client.getOutputStream());
                        outStream.add(out);
                        Objectout.add(new ObjectOutputStream(out));
    
    
                        DataInputStream in = new DataInputStream(client.getInputStream());
                        inStream.add(in);
                        Objectin.add(new ObjectInputStream(in));
                        
                        String turn = "userTurn "+ (Objectout.size()-1);
                        
                        Objectout.get(Objectout.size() -1).writeObject(turn);
                    }
                    
                    for(int i=0;i<clients.size();i++){
                        new Thread(new constantReading(i)).start();
                    }
                    
                    try {
                        generateTiles();
                    } catch (Exception e) {
                        print("Error in clientWait: " + e.getMessage());
                    }
                    
                } catch (Exception e) {
                    e.printStackTrace();
                    print("Error in constant reading: " + e.getMessage());
                }
            }
        }
    Note that the thread in line 32 is another thread that constantly waits for input from each client so as not to freeze the server.
    Last edited by anoorally; 02-24-2012 at 12:11 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Multiple ServerSocket problem

    What is printed out by server2 when it executes?
    Add some more printlns after the lines inside the loop that could be blocking and causing the server to freeze.

  9. #9
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Multiple ServerSocket problem

    The code doesnt blocks, but I got this weird "Client 5 connected" output. Note that there is the main class that instantiate server1 and server2, so output from both servers are displayed in the main class's console and is as follows:
    run:
    Listening on port: 7778
    Waiting for connection...
    Listening on port: 7779
    Waiting for connection...
    Client 1 connected!
    Waiting for connection...
    Client 2 connected!
    Waiting for connection...
    Client 3 connected!
    Waiting for connection...
    Client 4 connected!
    server READ-thread started
    server READ-thread started
    server READ-thread started
    server READ-thread started
    Client 5 connected!
    Waiting for connection...


    I opened 6 clients, the first 4 connecting to server2...everything went normally as it should. Then the 5th client tried connecting to server1, but the output shows "client 5 connected" and when the 6th client tries connecting to server1, it hangs.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Multiple ServerSocket problem

    I'm confused now. I thought you said:
    when the rest of the clients try to connect to server2, none of them are able to establish a connection
    Now you show that at least 5 clients were able to connect????
    output from both servers are displayed in the main class's console
    You need to enhance your print out to show which server is printing which message

  11. #11
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Multiple ServerSocket problem

    Sorry for the confusion. But from what i've been testing, it seems that even if the loop's condition is to allow 4 clients to connect, more than 4 clients are being able to connect to the same server.
    I can't understand what can be the cause.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Multiple ServerSocket problem

    Continue debugging by adding descriptive printlns that show what the code is doing.

    more than 4 clients are being able to connect to the same server.
    How can you tell which server is being connected to?

  13. #13
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Multiple ServerSocket problem

    I modified the code so that the each server is given its identifying name through its constructor. Upon instantiating, this is what is printed out:
    Server "null" is listening on port: 7778
    Waiting for connection...
    Server "Server 1" is listening on port: 7779
    Waiting for connection...

    The code which instantiates the servers are:
    Java Code:
            createServer("Server 1");
            createServer("Server 2");
    This is the function that creates a server:
    Java Code:
        public static void createServer(String sName) {
            int p = port++;
            new Server(sName, p);
            serverMap.put(sName, p);
        }
    This is the "Server" class's constructor:
    Java Code:
    public Server(String serverName, int port){
            try {
                server = new ServerSocket(port);
                print("Server \""+ sName + "\" is listening on port: "+ port);
                sName = serverName;
                
                new Thread(new listenClient()).start();
    
            } catch (Exception e) {
                print("Error in main: " + e.getMessage());
            }
        }
    Why is the 2nd server's name read as "null"?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Multiple ServerSocket problem

    Why is the 2nd server's name read as "null"?
    The variable sName has not been given a value when you print it!!!

    Is sName static?
    Last edited by Norm; 02-24-2012 at 02:25 PM.

  15. #15
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Multiple ServerSocket problem

    I solved the 'null' name being given, but now when I run the servers, this is what I get:
    Server "Server 1" is listening on port: 7778
    Server "Server 2" is listening on port: 7779
    Waiting for connection...[Server 2]

    Waiting for connection...[Server 2]


    Seems that the listening thread of Server 2 somehow runs twice, and there are no listening thread of Server 1. But I don't understand what might be causing this.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Multiple ServerSocket problem

    Are there any static variables?

  17. #17
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Multiple ServerSocket problem

    Yes. In the main class there are the followings:
    Java Code:
        private static int port = 7778;
        private static Map<String, Integer> serverMap = new HashMap<String, Integer>();
    In the "Server" class which is instantiated these are the ones relevant to the connections:
    Java Code:
        private ServerSocket server;
        private static ArrayList<Socket> clients = new ArrayList<Socket>();
        private static ArrayList<DataInputStream> inStream = new ArrayList<DataInputStream>();
        private static ArrayList<DataOutputStream> outStream = new ArrayList<DataOutputStream>();
        private static ArrayList<ObjectOutputStream> Objectout = new ArrayList<ObjectOutputStream>();
        private static ArrayList<ObjectInputStream> Objectin = new ArrayList<ObjectInputStream>();
        
        private static String sName;

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Multiple ServerSocket problem

    private static String sName;

  19. #19
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Multiple ServerSocket problem

    changed it to:
    private String sName;

    Server 1 listens on port 7778 and Server 2 on 7779
    even if a client is made to connect to port 7778, according to output, Server 2 is still handling the connections, any idea why?

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,776
    Rep Power
    25

    Default Re: Multiple ServerSocket problem

    Why are there any static variables? Are the two servers sharing too much?

Page 1 of 2 12 LastLast

Similar Threads

  1. Serversocket for external IP
    By Knetic in forum Networking
    Replies: 2
    Last Post: 02-16-2011, 03:32 PM
  2. Having trouble with ServerSocket
    By providence in forum New To Java
    Replies: 1
    Last Post: 01-29-2011, 04:05 AM
  3. HTTP ServerSocket
    By Dennis in forum Advanced Java
    Replies: 0
    Last Post: 12-01-2010, 03:27 PM
  4. ServerSocket.accept() using 100%+ CPU
    By pagod in forum Networking
    Replies: 2
    Last Post: 05-28-2010, 10:42 AM
  5. problem with ServerSocket on Linux
    By gabriel in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •