English 中文(简体)
阅读和书写
原标题:read and write to same socket (TCP) using select

我们重写一个客户和一个服务器,以做(我觉得是iii很简单的网络通信。 木板客户与服务器连接,而服务器本应将数据送回所有其他客户。

服务器只是坐在阻挡栏<编码>电子显示<>/代码>的路段,等待交通,然后向其他客户发送数据。 这似乎只是罚款。

问题是客户。 答复如下:它有时想写字。

然而,我发现,如果我使用的话:

 rv = select(fdmax + 1, &master_list, NULL, NULL, NULLiii;

我的法典将受阻,直到有新的数据读。 但有时(偶然,从另一个深处iii 我有新数据来撰写网络通信线上的数据。 因此,我希望我选择定期更新,请我检查是否有数据来撰写。

if (select(....iii != -1iii
{
  if (FD_SET(sockfd, &master_listiiiiii
     // handle data or disconnect
  else
     // look for data to write and write(iii / send(iii those.
}

我试图用以下方法确定选任以投票方式(或严格短时间iii:

// master list contains the sockfd from the getaddrinfo/socket/connect seq
struct timeval t;
memset(&t, 0, sizeof tiii;
rv = select(fdmax + 1, &master_list, NULL, NULL, &tiii;

但发现,当时客户从未获得任何新数据。

我也试图把袖珍打成非锁,例如:

fcntl(sockfd, F_SETFL, O_NONBLOCKiii;

但这并没有解决问题:

  1. if my client select(iii has no struct timeval, reading data works, but it never unblocks to let me look for writable data.
  2. if my client select(iii has a timeval to get it to poll, then it never signals that there are incoming data to read, and my app freezes thinking there is no network connection made (despite the fact that all other function calls have succeedediii

对于我能做什么是错了的,谁是谁? 不可能在同一张桌子上读书(我当然认为是真实的iii。

(EDIT:正确答案,以及我记住的服务器上但客户不上的东西,是在每次要求选择之前有第二版正盘,并抄录主机:

// declare and FD_ZERO read_fds:
// put sockfd in master_list

while (1iii
{
   read_fds = master_list;
   select(...iii;

   if (FD_ISSET(read_fdsiiiiii
     ....
   else
     // sleep or otherwise don t hog cpu resources
}

iii

最佳回答

一切都看重罚款,但以下条目除外:。 我的代码结构非常相似,我使用的是。 如果要确定清单,你就应当加以测试,而不是再加以确定。 除此之外,我看不到其他东西。

Edit。 此外,我还有以下陈述:

timeval listening_timeout;
listening_timeout.tv_sec = timeout_in_seconds;
listening_timeout.tv_usec = 0;

如果你把这个问题定在零点(正如你似乎正在做的那样)的话,也许会有问题。

Edit2。 我记得,当我在选择退出后和我再次进入之前,就没有清除所读物。 我不得不做这样的事情:

FD_ZERO(&sockfd);
FD_SET(sockfd, &rd);

此前,我正在输入<代码>。 我可以记住为什么。

问题回答

I seem to recall a trick about creating and sharing a read/write filedescriptor between the network thread and the main thread that is added to the descriptors in the select call. This fd has one byte written to it by the main thread when it has something to send. The write wakes up network thread from the select call and the network thread is then grabs the data from a shared buffer and writes it to the network then go back to sleep in the select.

如果这种担心含糊不清和缺乏守则......以及我的记忆可能不正确,那么其他人可能必须进一步指导你。

我看不出与你的法典有任何错误,因此应该工作。 如果您能够投入工作,一种方式就是建立一个管道,供您阅读的读物和准备文字的透镜使用,并将管道的读物添加到<>条码>。 然后,当另一条镜子准备数据以书写时,它只是向管道发送一些东西,你们的阅读镜子从<条码>中脱掉,然后可以做书写。 视数据读写的频率而定,这也会更加有效。

两条镜子应当能够同时使用同样的袖珍材料,因此,你的主要读物应当能够写给客户,而另一条镜子在选择接收数据时睡觉。 当然,这假设两条路面都可以查阅客户名单。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签