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
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
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 :
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 ]
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
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop