Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket Programming Best Practices?

Tags:

tcp

sockets

I am designing a file syncing application (like DropBox). The client keeps a persistent Secure (SSL) TCP socket with the server on port 443. Whenever a file is created/changed/deleted on the client, a packet containing the relevant data is sent over the socket to the server, which processes it to update the file on the server. Similarly when something changes on the server, it sends the relevant data to the client, which then updates the local copy.

This is working absolutely fine when the server is on the local computer, or on the local LAN. What I am worried about is when the client is on an unreliable network. So my question is what are the best practices, issues to consider while designing such an App?

For example, say when a file is created on the Client, should the client just send the data to the server and forget about it, or should it wait for an acknowledgment from the server within a certain time period, failing which send the data again? And what kind of acknowledgement?

like image 959
Tarandeep Gill Avatar asked Feb 01 '12 12:02

Tarandeep Gill


3 Answers

TCP abstracts away many network problems: packets always arrive in-order and are resent if the server does not acknowledge that is has received a packet. An unreliable network will cause traffic to become slower, as packets have to be resent.

If the connection is lost anyway, your read() and write() calls will return error return values, so you have to handle that.

like image 78
Sjoerd Avatar answered Oct 07 '22 14:10

Sjoerd


The client keeps a persistent Secure (SSL) TCP socket with the server

[..]

should the client just send the data to the server and forget about it, or should it wait for an acknowledgment from the server within a certain time period, failing which send the data again?

If you were using UDP, you'd have to do that. But since you're using TCP, that's all done for you already.

http://en.wikipedia.org/wiki/Transmission_Control_Protocol

You should be aware that TCP is a stream orientated protocol, so your data might not arrive the same way you sent it (i.e. it might arrive one byte at a time, or all at once).

like image 39
James McLaughlin Avatar answered Oct 07 '22 14:10

James McLaughlin


You should use acknowledgment even when the server is localhost. Imagine some kind of connection problem occurs when sending information. You need some way to know the result of the operation (using an acknowledgment system, for instance).

I would use something more complex than a simple ACK/NACK reply. For instance, if you change some files in the client, after sending information from the client to the server, the server should reply the number (or a list with the name) of files affected by the update operation. This way, the client can verify everything went OK, or act in consequence.

like image 25
Josepanaero Avatar answered Oct 07 '22 13:10

Josepanaero