Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Client/Server App Problem

Status
Not open for further replies.

ronnyjljr

IS-IT--Management
Nov 23, 2003
249
US
Hi Guys,

I am just learning how to do this so bare with me. I am writing a client/server app that is multithreaded. However, when a single client logs in it works fine. When another client logs in, they then can do whatever they want, but the telnet prompt for the first client is frozen but still remains open. If the second prompt then closes, the first is still frozen. How do I give control to both?

Code:
[blue]
	myClientSocket = myServer.getServerSocket().accept();
			ClientCount++;
			TalkToClient(myClientSocket,"Enter a name: ");
			String mClientName = new String();
			try
			{
				mClientName = ListenToClient(myClientSocket);
			}
			catch (IOException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			catch (InterruptedException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			myClient = new Client(myClientSocket,myServer.getStartDate(),mClientName,myServer);
			myClient.setName(mClientName);
			System.out.println("Added Client to Server: "+myClient.getName());
			myClient.start();

[/blue]


The Client object is threaded so here is the run:


Code:
[blue]
	public void run()
	{
		TalkToClient(ServerInfo(),false);
		TalkToClient("Logged in...",false);
	
	
			try
			{
				ListenToClient();
			}
			catch (IOException e)
			{
			}
			catch (InterruptedException e)
			{
			}
	}

[/blue]

and this is the ListenToClient code... I suspect it has something to do with this while loop...

Code:
[blue]
	public static void ListenToClient() throws IOException, InterruptedException
	{
		String Input = new String();
			   
		while((Input = in.readLine())!=null)
		{
			   TalkToClient(Input,true);
			   
			if (Input.equals("exit"))
			{
				clientSocket.close();
				return;
			} 
                } //END WHILE
}//END LISTEN TO CLIENT

[/blue]

and for good measure, the TalkToClient Function:

Code:
[blue]
	public static void TalkToClient(String mText, boolean mECHO)
	{
		StringBuffer myBuffer = new StringBuffer();
		if (mECHO) myBuffer.append("Command: "+mText);
		else myBuffer.append(SERVER+" - " +VERSION +": "+ mText);
		out.println(myBuffer);
	}

[/blue]



Is it the while loop or is something I am doing with the threads?


Thanks,
Ron






typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
I have not looked through your code completely, but your code is not, what I would say, is "well written".

The general form for server apps is to accept() and then pass to a threaded class for handling.
The accept() call is blocking (but considering you have a C/C++ typedef as a signature, I guess you know this) - so you should thread off ASAP.

Something like this :

Code:
// attach socket
ServerSocket ss = new ServerSocket(5555);
System.err.println("attached to port:5555");

// listen forever
while (true) {
   // SocketThreadHandler is your class that extends
   // Thread and does the handling of the client socket
   // so MT off, and go back to accept() ASAP
   new SocketThreadHandler(ss.accept()).start();
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I will try that.

Thanks a bunch!

~Ron

typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
You'll have to excuse the messy code too.
My style is much neater but I am just having
a little fun with this and chugging out code trying
to get things to work.

I promise I'll clean it up later :p

-Ron

typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top