Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Does this method block or is it becaus of the thread? Posted by super newb on 24 Mar 2005 at 6:12 AM
I am writing a fairly simple chat server. When I take in a string from the client, I use DataInputStream class.

for(;;)
{
			
	new Logger("Server ready to accept connections",ERRORFILE );
try
{
	client = server.accept();
	new Logger("New client accepted at: " + client.getRemoteSocketAddress().toString(),ERRORFILE);	
	out = new DataOutputStream(client.getOutputStream());
	in = new DataInputStream(client.getInputStream());
				
	if(Count.getCount() >= MAXUSERS)
	{
	
	   new Logger("Chatroom full, connect request refused.", LOGPATH);
	   out.write(CHATFULL);
	   client.close();
	   continue;
	}
				
	out.write(CONNECT);
	recv = in.readUTF();
	valid = validateName(recv);
	
	if(valid == NOTFOUND)
	{
		new Logger("invalid name entered: " + recv + ", client disconnected", LOGPATH);
		out.write(NAMENOTFOUND);
		client.close();
		continue;
	}
	if(valid == ALREADYLOGGED)
	{
		tmp = getSocket(recv);
		new Logger(recv + " already logged in at " + tmp.getRemoteSocketAddress().toString(),LOGPATH);
		out.write(ALREADYLOGGED);
		client.close();
		continue;
	}
		name = recv;
				
		out.write(VALIDNAME);
		valid =0;
	while(valid != VALID )
	{
			recv = in.readUTF();
			valid = validatePass(recv);
					
		if(valid != VALID)
		{
			new Logger("invalid password entered",LOGPATH);
			out.write(INVALIDPASS);
		}
	}
				
	out.write(VALIDPASS);
				
	Count.addElement(Count.getCount(), new UserLogged(client, name));
				
	new Logger(name + " at " + client.getRemoteSocketAddress().toString() + " accepted, configuring chatroom thread", LOGPATH);
				
	Runnable cru = new ChatRoomUser(in, name);
	new Thread(cru).start();
	out.write(READY);
				
				
	in=null;
	out=null;
	client=null;
				
}
catch(IOException e)
{
	new Logger("Client unexpectedly closed connection",LOGPATH);
	new ErrorLogger("Client unexpectedly closed connection",LOGPATH);
}


This runs no problem. The calls to in.readUTF() blocks until data is recieved from the client. It will sit happily forever waiting for data. The problem lies in the class that implements Runnable.

public class ChatRoomUser implements Runnable
{
	private UserLogged user;
	private DataInputStream in;
	private final String ERRORFILE = "../logs/chat/";
	private final String GETUSER = "reTrIEve";

	public ChatRoomUser(DataInputStream in, String handle)
	{
		this.in=in;
		user = Count.findElement(handle);
		if(user == null)//should never happen
			new ErrorLogger("Could not find UserLogged object in ChatRoomUser for " + handle, ERRORFILE);
		
	}
	
	public synchronized void run()
	{
		String str;
		
		for(;;)
		{
			try
			{
				
				str = in.readUTF();
				if(str.equals(GETUSER) == true)
				{
					retrieveUser();
				}
				else
					broadcast(str);//this just retrieves each Socket that is logged in and creates a DataOutputStream and send the string to each client in a loop
				
			}
			catch(IOException e)
			{
				System.out.println("Error recieving message from " + user.getName() );
				new ErrorLogger("Error recieving message from " + user.getName() ,ERRORFILE);
				str=null;
				
			}
			
			
			
			
		}	
		
	}


Now all of a sudden in.readUTF() will block until the first data is sent to it, it will send it back to the client like it should, but then when it gets back to readUTF(), an IOException is thrown, and then the next call to readUTF, throws it again, and it goes like this forever.

I tried using interrupt(), sleep(), wait(), and even putting in.available in a if block, with readUTF inside it and it doesn't work.

I am still struggling with threads, and threads seem to be the problem. What do I need to do to fix this? Also, should I make use of ThreadLocal for variables that will be different in each thread?

Thanks in advance.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.