LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2009, 10:49 PM   #1
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Rep: Reputation: 34
Help needed debugging a simple server supporting multiple clients (Java)


I don't have any problem when I'm communicating with one client. The problem starts when I connect another client.The server eventually ignores the older clients and responds only to the latest one.

The working mechanism is simple: Accept connections and print messages received from multiple clients.

Client 1 (telnet localhost 5000)

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
1
1
1
1
1
1
1
1
1
111
Connection closed by foreign host

Client 2 (telnet localhost 5000) Latest client. Works properly.
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
2
2
2
2
2
2
2
2
2
2
2
222
Connection closed by foreign host.


Server output (java DebugServer 5000)

Server started
Incoming connection accepted
RECEIVED: 1
RECEIVED: 1
RECEIVED: 1
RECEIVED: 1
RECEIVED: 1
RECEIVED: 1
Incoming connection accepted
RECEIVED: 2
RECEIVED: 2
RECEIVED: 2
RECEIVED: 2
RECEIVED: 2
RECEIVED: 2
RECEIVED: 2
RECEIVED: 2
RECEIVED: 1
RECEIVED: 2
RECEIVED: 2
RECEIVED: 2
RECEIVED: 222


You can see that it isn't receiving the messages from the first client eventually. I tried debugging it using Netbeans and it shows nothing happens at bfr.readLine() when Client 1 sends a message after some messages have been sent.

Code:
import java.net.*;
import java.io.*;
import java.util.*;

public class DebugServer {
	ServerSocket serverSocket;
	BufferedReader bfr;

	public static void main(String args[])
	{
		DebugServer server = new DebugServer();
		server.go(Integer.parseInt(args[0]));

	}
	
	public void go(int portNo)	
	{
		System.out.println("Server started");
		try {
		serverSocket = new ServerSocket(portNo);
		while(true) {
		Socket socket = serverSocket.accept();
		System.out.println("Incoming connection accepted");
		ClientSession c = new ClientSession(socket);
		Thread t = new Thread(c);
		t.start();
		}
		} catch(IOException e) 
		{
			System.out.println("Server IOException");
			e.printStackTrace();
		}
	}


class ClientSession implements Runnable{
	Socket socket;
	
	ClientSession(Socket socket)
	{
		this.socket = socket;
	}

	public void run() {
	try{
			InputStreamReader isr = new InputStreamReader(socket.getInputStream());
			bfr = new BufferedReader(isr);
			String s;

		for(;;) {
			while((s = bfr.readLine()) !=  null) {
				System.out.println("RECEIVED: "+s);
			}
		}
      } catch(IOException e) {
				System.out.println("Server IO Exception");
	} 
	}
	
		}
	
}

Thank you for your help.

Last edited by gregorian; 09-26-2009 at 12:20 AM.
 
Old 09-25-2009, 07:17 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
you declare "bfr" as a class-wide variable to "DebugServer". theres only one instance of this variable in the server, and for each client you overwrite the previous client's reference to this variable. "bfr" should be declared as local to the "run" method in class "ClientSession".

i suggest one ("named") class per file. if you must have more than one, then declare each one in its own block. your declaring the "ClientSession" class within the scope of "DebugServer". if you would have declared them as two separate blocks, you would have seen the compiler error, thus finding the problem immediately! of course there are other reasons besides this for structuring the classes in separate files too.

you have something like:
Code:
public class Debug Server
{
  bfr;
  //...

  class ClientSession
  {
    //...
    bfr...
  }
}
you would have seen the compiler error if you structured it this way:
Code:
public class Debug Server
{
  bfr;
  //...
}

class ClientSession
{
  //...
  bfr... // variable not defined in this scope!  better make a local one to the run method that uses it
}
 
Old 09-25-2009, 08:41 PM   #3
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
Thank you very much! Could you tell me how you found it? Did you just "spot" the error or did you arrive at it through analysis. It seems obvious on hindsight but I'd like to know how you found it. Thank you once again.
 
Old 09-25-2009, 08:45 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
this was one of those trickier ones to find. sure, its a simple solution, but when you run into something you cant solve we often look past simple things like where a variable is declared, and think theres something deeper that we are misunderstanding. so, at first no, i did not spot the error and was curious why you were seeing the behaviour (my initial guess was that somehow telnet was screwing it up, and i was going to suggest you write your own java client to talk to your server, rather than telnet). anyway, your welcome.
 
Old 09-26-2009, 12:14 AM   #5
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
Well I did write my java client originally and had the same problem. I confirmed that the problem was with the server because telnet wasn't working either. Anyway thank you! This bug had been escaping me for days and I was on the verge of giving up.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple clients for echo server msriram_linux Linux - Networking 1 12-05-2007 03:27 PM
multiple-echo-clients-server msriram_linux Linux - Networking 2 12-05-2007 12:10 PM
server which accepts multiple clients suresh_rupineni Linux - Server 1 10-19-2007 01:46 AM
Multiple JAVA VNC clients on same webpage KingPhillius Linux - Networking 2 11-16-2004 07:07 AM
java, simple program help needed.. marlor Programming 1 11-04-2004 02:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration