September 04, 2010, 03:19:35 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Socket and ObjectStream communication  (Read 1113 times)
0 Members and 1 Guest are viewing this topic.
JavaLearner
Member
*

Reputation: 0
Offline Offline
Posts: 34
Referrals: 0

Awards
« on: July 05, 2009, 06:10:09 AM »

I am trying to do something very simple. I have a server and multiple clients. Each of those clients connect to the server on a particular port. Every client talks to the server based on the current state of the client. For eg, the client first connects to the server and provides its identity. Once the server authenticates this client, it sends out a accept response. The server now maintains the client state as authenticated. As the client requests for new messages the server responds.

Simple client server communication. Now this is what I am trying to do.
Socket socket = serverSocket.accept() ; this is a blocking call and until the client responds, the server waits.
inputStream = socket.getobjectInputStream() ; so I save the objectinputstream for that client.

Now I expect that subsequent calls to
inputStream.readObject() will result in blocking calls. And that I wont need to do a serverSocket.accept() anymore for that client. This way every request the client sends, the server does not need to identify the client. Correct ?

Thats what streams are for right ? A stream is like a connection made between the server and client. I just need to write to the outputstream on the client side and read from the inputstream on the server side.

Please correct my assumption. The java docs dont mention if the readObject on inputstream is blocking or not. I wonder why not...
Logged
Javaforums.net :: a community about Java software development.
« on: July 05, 2009, 06:10:09 AM »

 Logged
trey
Member
*

Reputation: 0
Offline Offline
Posts: 16
Referrals: 0

Awards
« Reply #1 on: December 07, 2009, 09:09:18 PM »

The non-blocking library is java.nio. Remember that since the accept method is blocking, the thread will not perform beyond that call until a Socket is accepted, which in some cases forces multi-threading (particularly bad in cases where a new thread is created for each Socket that is accepted). To answer your question directly, readObject is a blocking call, which once again almost forces multi-threading. I would recommend reading into NIO a bit, this may save you some hassle as far as the overall design.
Logged
Javaforums.net :: a community about Java software development.
   

Your Ad Here
 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here
Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.163 seconds with 33 queries.

Google visited last this page September 02, 2010, 04:23:13 AM