Socket Reading and Writing data
Hi, I am new to socket programming and was wondering if anyone could tell me the best methods to employ to ensure that when data is sent through a socket that the receiving socket is ready to receive all the data. I have started to use the NIO classes and primarily interested about how this can be achieved when sending a bytebuffer over a socket. Any example code would be appreciated, thank you.
> Can you explain your requirement more clearly? What
> exactly do you mean by 'ready to receive it'? You
> can't send anything over a socket unless it is
> connected and accepted: does that answer your
> question?
OK, I understand a server socket needs to accept() a connection before any communication can be sent to it however what if communication is happening in both directions? Say for instance a client initiates connection with the server the server needs to accept the connection, however if the server is to reply to the some request the client has made how is the client made ready to receive the response or stream of data over one socket connection.
Actually I misspoke, the data can be sent before the accept() completes.
TCP/IP/socket I/O is asynchronous. There are sending and receiving socket buffers at both ends. You can write to a socket as long as there is room in the receiver's receive buffer or the sender's send buffer. Blocking I/O will block on writing if these buffers are both full. If all your written data fits into your local send buffer the write will return immediately, i.e. before the data is even put on the wire.
So you can write when the peer isn't reading, up to the limit of those buffers. You can control their size via the Socket API. On most systems they have reasonable default sizes, except for Windows where it is a pathetically inadequate 8k.
ejpa at 2007-7-15 3:57:49 >

I have a very simple program and it hangs, this is main:
1Socket soc = new Socket(InetAddress.getByName("mail.bigpond.com"), 110);
2byte[] b72 = new byte[72];
3int bytesRead = soc.getInputStream().read(b72, 0, b72.length);
4System.out.println(new String(b72, 0, bytesRead));
5soc.getOutputStream().write("USER lilymail\r\f".getBytes("ASCII"));
6soc.getOutputStream().flush();
7bytesRead = soc.getInputStream().read(b72, 0, b72.length);
8System.out.println(new String(b72, 0, bytesRead));
9soc.close();
}
It prints: +OK POP3 server ready.
And then nothing.
Why? Is the \r\f incorrect?
What else could it be? Outlook retrieves from lilymail at mail.bigpond.com.
Who knows...