Does this method block or is it becaus of the thread? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Does this method block or is it becaus of the thread?

super newbsuper newb Posts: 5Member
I am writing a fairly simple chat server. When I take in a string from the client, I use DataInputStream class.

[code]
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);
}
[/code]

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.

[code]
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;

}




}

}
[/code]

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.
Sign In or Register to comment.