aspose file tools*
The moose likes Threads and Synchronization and the fly likes Trying to create a client/server chat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Trying to create a client/server chat " Watch "Trying to create a client/server chat " New topic
Author

Trying to create a client/server chat

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I am building my way up to creating a client/server chat application.
The stage i am at now is a server that can accept multiple clients and they can all send messages to the server, which displays the conversation. I now want what each client enters to be displayed to all other clients, as currently they only see what they type, and the conversation is just sent to the server.
The problem i have is trying to send the conversation to all clients. Below is what i have so far.
When a client is connected they are given their own serverThread which continues to run and read/output the conversation. I also store them in a linked list.



This is the client that i now want to read from the server as well as write to.


My problem is in server thread, in the while loop i want to loop through all of my clients and output the messages, I have put a for loop in to cycle through my clients but i don't know how to output the messages sent to the server, or even if this is the correct way to do it. Currently i can connect multiple users but they still only output to the server. . . .
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

Well, if I might suggest this: your client should always be ready to receive data from the server. What you have there doesn't do that -- it only receives data when it's being constructed, and then it stops listening to the server. Perhaps the client should have a separate thread which does nothing but wait for data from the server?
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Everything i have tried so far has resulted in no response getting sent to the server. I have tried to put a while(true) around the original while loop but whenever i put anything in the original while loop such as System.out.println(response) it stops sending my messages to the server. .
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

Yes. Your code won't be able to simultaneously listen for input from System.in and input from the server's socket in one thread. That's why I suggested you needed a separate thread for listening to the server.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I have made some changes to both classes, mainly to the client class though.
I still have the same problem though. I can connect multiple clients and send messages that will display on my server, but these messages are not sent back out to the clients.
If anyone could help point out my errors that would be great.
Thanks

I have added System.out.println("Sent output:> " + line);
into the while loop in my server run method and it appears i am getting the right output, it just isn't getting received by the client
Client


Server
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

Well, I would say this line of code is a problem:



Your code could be throwing exceptions, but you wouldn't know because you are ignoring them. And in this context, ignoring errors is a bad strategy. Try this instead:



And you are using a PrintWriter. Here's one thing the API documentation says about that:

Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().


So since you're working in an environment where exceptions may occur and it's important to deal with them, that's a problem. I would suggest using some other kind of Writer and dealing with the errors appropriately.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I have tried this, still the same issue though, and no errors are thrown.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

Did you fix both of the places where you were ignoring exceptions?
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Paul Clapham wrote:Did you fix both of the places where you were ignoring exceptions?


Yes i have fixed all of the exceptions. No error is being thrown, it just doesn't print back to the clients.

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

Because your PrintWriter is still swallowing the exceptions?

Or is that line of code even being called? What does your debugging tell you about that?
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168

I may have been focussing/changing the client where in fact i should have been focussing on the PrintWriter that you kept telling me in the server class.
Well now i feel a fool, anyway its working now.
Thanks for persisting ha
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Another question which i believe must have a simple answer.
I am now moving what i had working form the command line into a GUI.
The client class is pretty much identical just in a JFrame.
I have a text field that i enter the port number into and i can see from my server that the client is successfully connected. I then start the thread running as you can see below.
After the client is connected and it starts the run method i get this error message
Exception in thread "Thread-1" java.lang.NullPointerException
at SimpleChat.run(SimpleChat.java:133)
at java.lang.Thread.run(Thread.java:722)

This refers to this line:
serverIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));

I have tried placing this in various places around within the run method as well as connectToServer method but i still get similar the same error message.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

That means you're trying to use a method of a variable whose value is null. In that statement there's only one place where you try to do that. Therefore that variable must be null.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
The variable serverIn is originally set to null. But why when i say
serverIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));

Does it not change it from null? I have tried to set the variable as this instead of null
private BufferedReader serverIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));

But it wants me to throw several IOExceptions, i didn't think that was the best way
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

No, you misunderstand. You aren't calling any of serverIn's methods. NullPointerException occurs when you have a variable (which is null) and you try to call one of its methods. Or more precisely, one of the methods of the object which it fails to refer to.

So try again. In that statement you're calling a method on a variable. Find that variable.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Paul Clapham wrote:No, you misunderstand. You aren't calling any of serverIn's methods. NullPointerException occurs when you have a variable (which is null) and you try to call one of its methods. Or more precisely, one of the methods of the object which it fails to refer to.

So try again. In that statement you're calling a method on a variable. Find that variable.


I am calling getInputStream on socket. But i believed i had declared socket in ConnectToServer

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Could anyone help me with this? I have tried a variety of ways to try and get this working such as assigning the input stream in ConnectToServer and using that in my InputStreamReader, that also didnt work
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

tom davies wrote:
I am calling getInputStream on socket. But i believed i had declared socket in ConnectToServer


Don't "assume" -- what is the value of the socket variable? You can easily add a println() just before that line to actually see what the variable is set to.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
The socket does return null yes. I think i have got my head around why.
I initialise it in ConnectToServer, but when i create a new thread i am creating a new SimpleChat object which doesn't have the socket initialised so it is still set to null.
What would be the best way to solve this? i could create an inner class like i have done for my server thread?

I have added an inner class and it all works great. With the addition of some changes to the setting of the conversation, so it didn't overwrite the existing conversation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to create a client/server chat
 
Similar Threads
Multithread-call center support
Facing some problems in my client server application ? It is showing connection reset ?
ObjectOutputStream thread issue
Not getting input or output to the Stream
Issue with simple multi-threaded Echo server and client program