简体   繁体   English

Unix IPC套接字发送/接收同步

[英]Unix IPC sockets send/recv sync

I'm using local a Unix socket to communicate between two different processes. 我正在使用本地Unix套接字在两个不同进程之间进行通信。 Thing is, some parts of the code on bth ends take different time to run, and I need recv and send to be synced across both processes. 问题是,bth端的某些代码部分需要花费不同的时间来运行,因此我需要recvsend才能在两个进程之间进行同步。 Is there a way to force send and recv to wait for the next corresponding line on the opposite process? 有没有办法强制sendrecv等待相反过程中的下一条对应行?

You must implement a protocol. 您必须实现协议。 After all, you can not be sure that the sockets are in sync. 毕竟,您不能确保套接字同步。 For example you could send one package with 100 bytes and then receive two ore even more packages adding it up. 例如,您可以发送一个100字节的数据包,然后再接收两个或更多的数据包将其加起来。

By default, recv() will block (wait) until there are data to read, while send() will block until there is space in the buffer to write to. 默认情况下, recv()将阻塞(等待),直到有数据可读取为止,而send()将阻塞直到缓冲区中的空间可写入。 For most applications, this is enough synchronisation (if you design your protocol sanely). 对于大多数应用程序,这足够同步(如果您合理地设计协议)。

So I recommend you just think about the details of how your communication will work, and try it out. 因此,我建议您仅考虑一下如何进行通信的细节,然后尝试一下。 Then if there is still a problem, come back with a question that is as specific as possible. 然后,如果仍然存在问题,请返回一个尽可能具体的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM