Results 1 to 20 of 22
Thread: Multiple ServerSocket problem
- 02-23-2012, 10:24 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
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?
- 02-23-2012, 10:39 PM #2
Re: Multiple ServerSocket problem
Is server2 listening on the port (calling accept()) when those clients try to connect?none of them are able to establish a connection
What if you do server2 first and then server1?
- 02-23-2012, 10:45 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
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 10:47 PM.
- 02-23-2012, 10:54 PM #4
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?
- 02-23-2012, 10:59 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
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:
The above code save the client's reference and there input & output streams respective in ArrayList.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); }
- 02-23-2012, 11:02 PM #6
Re: Multiple ServerSocket problem
You need a loop and a thread to keep the current connection from freezing the server.
- 02-23-2012, 11:07 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
Re: Multiple ServerSocket problem
Thats actually how it is. Here is the whole thread class with the while loop included:
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.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()); } } }Last edited by anoorally; 02-23-2012 at 11:11 PM.
- 02-23-2012, 11:42 PM #8
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.
- 02-23-2012, 11:50 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
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.
- 02-24-2012, 12:12 AM #10
Re: Multiple ServerSocket problem
I'm confused now. I thought you said:
Now you show that at least 5 clients were able to connect????when the rest of the clients try to connect to server2, none of them are able to establish a connection
You need to enhance your print out to show which server is printing which messageoutput from both servers are displayed in the main class's console
- 02-24-2012, 12:18 AM #11
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
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.
- 02-24-2012, 02:23 AM #12
Re: Multiple ServerSocket problem
Continue debugging by adding descriptive printlns that show what the code is doing.
How can you tell which server is being connected to?more than 4 clients are being able to connect to the same server.
- 02-24-2012, 12:31 PM #13
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
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:
This is the function that creates a server:Java Code:createServer("Server 1"); createServer("Server 2");
This is the "Server" class's constructor:Java Code:public static void createServer(String sName) { int p = port++; new Server(sName, p); serverMap.put(sName, p); }
Why is the 2nd server's name read as "null"?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()); } }
- 02-24-2012, 01:22 PM #14
Re: Multiple ServerSocket problem
The variable sName has not been given a value when you print it!!!Why is the 2nd server's name read as "null"?
Is sName static?Last edited by Norm; 02-24-2012 at 01:25 PM.
- 02-24-2012, 01:57 PM #15
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
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.
- 02-24-2012, 02:03 PM #16
Re: Multiple ServerSocket problem
Are there any static variables?
- 02-24-2012, 02:08 PM #17
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
Re: Multiple ServerSocket problem
Yes. In the main class there are the followings:
In the "Server" class which is instantiated these are the ones relevant to the connections:Java Code:private static int port = 7778; private static Map<String, Integer> serverMap = new HashMap<String, Integer>();
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;
- 02-24-2012, 02:13 PM #18
Re: Multiple ServerSocket problem
private static String sName;
- 02-24-2012, 02:17 PM #19
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
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?
- 02-24-2012, 02:37 PM #20
Similar Threads
-
Serversocket for external IP
By Knetic in forum NetworkingReplies: 2Last Post: 02-16-2011, 02:32 PM -
Having trouble with ServerSocket
By providence in forum New To JavaReplies: 1Last Post: 01-29-2011, 03:05 AM -
HTTP ServerSocket
By Dennis in forum Advanced JavaReplies: 0Last Post: 12-01-2010, 02:27 PM -
ServerSocket.accept() using 100%+ CPU
By pagod in forum NetworkingReplies: 2Last Post: 05-28-2010, 09:42 AM -
problem with ServerSocket on Linux
By gabriel in forum New To JavaReplies: 1Last Post: 08-07-2007, 04:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks