Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Socket Asynchronous Chat  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a server and client chat program that each run 2 threads that run at the same time. One thread reads in messages from the other, while the other thread reads the keyboard for a message then sends it to the other. What's going wrong is that when I run the chat, I keep getting the follower error messages from my Try/Catch statements.

java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Unknown Source)
at WriteOut.run(WriteOut.java:23)
java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Unknown Source)
Client: at ReadIn.run(ReadIn.java:22)
Exception in thread "Thread-0" java.lang.NullPointerException
at ReadIn.run(ReadIn.java:29)

The troublesome lines seem to be
Line 23 WriteOut


Line 22 ReadIn


I run it on a Linux server and the server side just keeps getting an endless loop of that message printing off, which leads me to think that the Client getting this error and sending to the server repeatedly, as it is actively listening to the client.
When you go to run this and test it, you have to run the server first, then you run the client.

Here is the code

Client


ReadIn (Client read thread)



WriteOut (client write out thread)



Server


SerWrite (server write thread)



SerRead (server read thread)



I appreciate the help as this is just driving me crazy. I have been trying and trying and trying to figure out what the heck is going on with this.
 
Sheriff
Posts: 4840
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I moved this post to Java in General to see if it gets more traction.

I will try the server code on my Raspberry Pi and the client on Windows 10, but I can't guarantee I will be much help.
 
author
Sheriff
Posts: 23484
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ryan Re wrote:
java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Unknown Source)
at WriteOut.run(WriteOut.java:23)
java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Unknown Source)

The troublesome lines seem to be
Line 23 WriteOut

Line 22 ReadIn



The error being reported is that ... the code in those lines are trying to use a socket which has already been closed.

And since your main thread code, closes the socket, after passing it to the other threads, perhaps that is related to the issue?

Henry
 
Ryan Re
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh my gosh! Thank you that was it Henry! I needed to edit my main class to not move on until the threads returned that the chat was false. I got a few minor tweaks, but I am getting messages that go back and forth right now and that has me excited!
 
Ryan Re
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this may be a dumb question, but can I see if another thread has ended from inside a thread? When I do my exit command, the write from that end and the read from the other closes, but their perspective writes and reads don't. I have to enter the exit command from both sides.

Here is the updated code

Client



ReadIn



WriteOut



Server



SerWrite



SerRead

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!