This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

server response

 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i,am new to socket programming . What i,am right now doing is client to server interaction by using the serverssocket class in which the client sends data to the server and the server reads that data . Now if server needs o reaply back to the client , how can i do that? Similarly , if i try to conect to this server via web browser , how can i read the response from the server to this web browser(if the server actually send som response)?
please help
Thanks in advance
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The moment your ServerSocket has given you a Socket, you actually have a Socket at both sides. There isn't really a "client" and "server" anymore, just two programs sitting at either end of a socket connection. Both can get an InputStream and/or an OutputStream from their Socket and read and write at will.
Does this answer your question? I'm afraid I do not have a good feel for where you're stuck.
- Peter
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter
i,am doing exactly wht you've said .The moment my serversocket recieves any data from the client ,it displays it on the console and returns the same data back to the client socket (from where it actually came). Now at the client socket i,am using an inputstream to read the incomming data but fail to do so as my client is not able to read the data . here are the programs:-
client1 :

server1 :

Thanks in advance
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, but your problem has nothing to do with sockets. Your problem is that you're setting up two-way communication between processes in a way that inevitably ends up in deadlock.
Whether you know it or not, in the code above you have set up a protocol (albeit a very simple one). In any request/response type protocol, the two parties must agree with each other whose turn it is to talk (write) and whose turn it is to listen (read). If they do not agree, then you'll end up in a situation where either both are listening waiting for the other to talk, or both are trying to talk but the other end is not listening.
In this particular case, the problem is that the client writes and then listens in byte-sized chunks, whereas the server listens and then writes in line-sized chunks. After writing the first byte, the client expects the server to echo that byte, but the server will not do this because it hasn't seen a full line's worth of data yet.
There are three ways you can go about fixing this.
  • Make the client work on a line-by-line basis as well. Remember to call flush() on your output streams!
  • Make the server work on a byte-by-byte basis, using read() rather than readLine(). Remember to call flush() on your output streams! Generally, this is not recommended because sending each byte in a separate packet wastes a LOT of network bandwidth.
  • Use separate read and write threads so the client does not wait for the server to echo each byte.
  • Hope this helps
    - Peter
    [ August 29, 2002: Message edited by: Peter den Haan ]
     
    Raghav Mathur
    Ranch Hand
    Posts: 641
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for your reply and sorry for responding so late . I've done as you said . Created 2 threads , 1 for inputstream and the second for outputstream. Still i,am not 100% clear about why we use separate threads for streams .
    thanks in advance
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic