套接字编程最佳实践

套接字编程最佳实践

本文介绍了套接字编程最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个文件同步应用程序(如 DropBox).客户端在端口 443 上与服务器保持持久的安全 (SSL) TCP 套接字.每当在客户端上创建/更改/删除文件时,包含相关数据的数据包通过套接字发送到服务器,服务器将其处理为更新服务器上的文件.同样,当服务器发生变化时,它会将相关数据发送给客户端,然后客户端更新本地副本.

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.

当服务器位于本地计算机或本地 LAN 上时,这绝对正常.我担心的是客户端在不可靠的网络上.所以我的问题是在设计此类应用程序时需要考虑哪些最佳实践和问题?

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?

推荐答案

TCP 抽象出许多网络问题:数据包总是按顺序到达,如果服务器不确认已收到数据包,则会重新发送.不可靠的网络会导致流量变慢,因为必须重新发送数据包.

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.

如果连接丢失,您的 read() 和 write() 调用将返回错误返回值,因此您必须处理.

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

这篇关于套接字编程最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:32