I have a sender that TCP-connects, sends block of data, and closes the socket. I'd like to write the simplest, but reliable program that does the above. The first thing comes into mind (e.g. in .NET, although the question relevant to sockets in general):
// assuming LingerOption(false), NoDelay set to whatever
var client = new TcpClient().Connect(server, port);
var stream = client.GetStream();
stream.Write(data, 0, data.Length);
stream.Close();
client.Close();
Now to some questions basing on reading of various MSDN and other materials:
- stream.Close() calls Socket.Close(). Socket.Close() is said to close immediatelly, discarding network buffer data which was not send. This is bad. But, Socket.Write documentation says that if the socket is blocking (it is, by default), Socket.Write will block until all the data is sent. So there's no problem, right?
- In general, can there be a situation, in which the code above will result in receiver not receiving everything what was sent in Write? (assuming 100% reliable network)