Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket question  RSS feed

 
Alex Barilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, guys! I'm trying to cope with th sockets but even simple actions don't work. Please give me an idea where the mistake is.



I can't also understand how socket connection works with streams: when I create BufferedReader / BufferedWriter objects in two clases how does JVM match this stream instances in different clases to each other? Is it becase of getInputStream() and getOutputStream() mathods are invoked from the same socket? Thank you in advance.
 
Henry Wong
author
Sheriff
Posts: 22831
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Barilo wrote:Hi, guys! I'm trying to cope with th sockets but even simple actions don't work. Please give me an idea where the mistake is.


It would probably help if you give us an idea of what is that you are seeing versus what is it that you are expecting.

Alex Barilo wrote:


I can't also understand how socket connection works with streams: when I create BufferedReader / BufferedWriter objects in two clases how does JVM match this stream instances in different clases to each other? Is it becase of getInputStream() and getOutputStream() mathods are invoked from the same socket? Thank you in advance.



Some possible issues. Your server only accepts one socket, processes one data socket, and then just exits. It doesn't even bother with closing the rendezvous socket. Your client uses localhost as the address -- which means that these two programs can only communicate when they are on the same machine. Also, it may be a good idea to flush the socket before closing them -- or better yet, don't close the socket. Instead, close the buffered writer (and buffered reader), and let the writer (reader) close the socket.

Henry
 
Alex Barilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, thank you for reply! When I flush the buffer it works, but works only if client sends the message and server receives it. But if I try to send back the message to the client it doesn't work. It looks like client and server are waiting for actions from each other and happens nothing. Frankly, I attempt to establish two-way communication between client and server - a sort of simple chat. My English not good but hope I got my points across. I would appreciate a lot if you give me an idea how to do it.
 
Henry Wong
author
Sheriff
Posts: 22831
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Barilo wrote:Henry, thank you for reply! When I flush the buffer it works, but works only if client sends the message and server receives it. But if I try to send back the message to the client it doesn't work. It looks like client and server are waiting for actions from each other and happens nothing. Frankly, I attempt to establish two-way communication between client and server - a sort of simple chat. My English not good but hope I got my points across. I would appreciate a lot if you give me an idea how to do it.


Well.. don't do that. Keep in mind that a protocol is more than how a packet is assembled. It is also coordination between the applications. The TCP socket will only get the packets over the wire -- but it is the responsibility of the two sides to send only when the other side expects it (and is receiving it).

Henry
 
Goran Udosic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Something as following could work for Server part :



You need a server to stay active mostly of the time, so it can actually wait for the client to send something.
The code above will just bounce back the data received from the client.
 
Alex Barilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, thank you for help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!