Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Proper way of sending object data between server and client? (ie. what's wrong with my code?)  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a small project to improve my understanding of Java sockets and threads and I just ran into a wall here with the communication between the server and the client. The server seems to just stall when creating a new instance of the ObjectOutputStream class.

Client snippet:


Server Snippet:
 
Sheriff
Posts: 24089
54
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Mikhael!

Mikhael Borris wrote:The server seems to just stall when creating a new instance of the ObjectOutputStream class.



Are you sure that's the exact line where the server stalls? Have you run the server code in a debugger to determine that?

By the way if you're going to be communicating over a socket connection, you should always flush the socket's output stream after you are finished writing a message. Otherwise output data may be left sitting in a buffer, which could cause the recipient to wait indefinitely for the data which is sitting in that buffer.
 
Mikhael Borris
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Welcome to the Ranch, Mikhael!

Thanks!

Mikhael Borris wrote:The server seems to just stall when creating a new instance of the ObjectOutputStream class.



Are you sure that's the exact line where the server stalls? Have you run the server code in a debugger to determine that?

By the way if you're going to be communicating over a socket connection, you should always flush the socket's output stream after you are finished writing a message. Otherwise output data may be left sitting in a buffer, which could cause the recipient to wait indefinitely for the data which is sitting in that buffer.




I am not familiar with running a debug,;so far my programs have been small and any solution always turned out eventually so before I look into that I should restate my question a little. I managed to solve my problem but quickly ran into a new one. The problem was 1: missing .newLine() statements and
2: creating an ObjectOutputStream instance while the output stream is still in use. Turns out it hogs the output stream and messes up values going to the client.

Now the problem is that,

try(ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());){
Guest g = (Guest) ois.readObject();
}catch(ClassNotFoundException e) {
System.out.println(e.getMessage());
}

returns the catched exception message: "server.Guest".


Server:



Client:

 
Bartender
Posts: 20353
111
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead ofConsiderIt gives you more information. Although in this case, it's pretty obvious that class server.Guest was not defined in the classpath of the client.

When you serialize java objects, the serialized data does not include the class itself, only the class data. So if the client doesn't understand the class, it can't instantiate a copy of it to load up with the incoming class data.
 
This is my favorite show. And this is my favorite tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!