- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 recvfrom()
函数上实现超时
。
为此,我使用 select()
函数。我从伟大的互联网上获取了代码,但不知道为什么,当我使用它时,我的程序崩溃了。如果有帮助的话,我会在线程中启动此服务器。这是我尝试做的:
...
// Setup timeval variable
timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
// Setup fd_set structure
fd_set fds;
FD_ZERO(&fds);
FD_SET(id_de_la_socket, &fds);
// Return value:
// -1: error occurred
// 0: timed out
// > 0: data ready to be read
int retval = select(id_de_la_socket+1, &fds, NULL, NULL, &timeout);
if(retval == -1){
printf("Error");
return NULL;
}
else if(retval == 0){
printf("Timeout");
return NULL;
}
else{
nombre_de_caractere=recvfrom(id_de_la_socket,buffer,1515,/*MSG_PARTIAL*/0,(struct sockaddr*)&information_sur_la_source,&tempo);
...
在尝试实现超时
之前,一切正常,我的recvfrom()
处于 block 模式。所以我认为问题出在我添加的这段代码上。也许是我不太理解的函数参数。
感谢您的帮助。
编辑:完整代码:
#include "serveur.h"
#include <unistd.h>
#include <fcntl.h>
serveur::serveur()
{
}
StructureSupervision::T_StructureSupervision* serveur::receiveDataUDP(){
WSADATA initialisation_win32;
int erreur;
int tempo;
int nombre_de_caractere;
char buffer[65535];
SOCKET id_de_la_socket;
SOCKADDR_IN information_sur_la_source;
erreur=WSAStartup(MAKEWORD(2,2),&initialisation_win32);
if (erreur!=0)
printf("\nDesole, je ne peux pas initialiser Winsock du a l'erreur : %d %d",erreur,WSAGetLastError());
else
printf("\nWSAStartup : OK");
id_de_la_socket=socket(AF_INET,SOCK_DGRAM,0);
if (id_de_la_socket==INVALID_SOCKET)
printf("\nDesole, je ne peux pas creer la socket du a l'erreur : %d",WSAGetLastError());
else
printf("\nsocket : OK");
information_sur_la_source.sin_family=AF_INET;
information_sur_la_source.sin_addr.s_addr = inet_addr("10.100.13.129"); // Ecoute sur toutes les IP locales
information_sur_la_source.sin_port=htons(4000); // Ecoute sur le port 4000
erreur=bind(id_de_la_socket,(struct sockaddr*)&information_sur_la_source,sizeof(information_sur_la_source));
if (erreur!=0)
printf("\nDesole, je ne peux pas ecouter ce port : %d %d",erreur,WSAGetLastError());
else
printf("\nbind : OK \n");
tempo=sizeof(information_sur_la_source); // Passe par une variable afin d'utiliser un pointeur
// Setup timeval variable
timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
// Setup fd_set structure
fd_set fds;
FD_ZERO(&fds);
FD_SET(id_de_la_socket, &fds);
// Return value:
// -1: error occurred
// 0: timed out
// > 0: data ready to be read
ULONG NonBlock = 1; ioctlsocket(id_de_la_socket, FIONBIO, &NonBlock);
int retval = select(id_de_la_socket+1, &fds, NULL, NULL, &timeout);
if(retval == -1){
printf("Error");
return NULL;
}
else if(retval == 0){
printf("Timeout");
return NULL;
}
else{
nombre_de_caractere=recvfrom(id_de_la_socket,buffer,1515,/*MSG_PARTIAL*/0,(struct sockaddr*)&information_sur_la_source,&tempo);
buffer[nombre_de_caractere]=0; // Permet de fermer le tableau apr�s le contenu des data, car la fonction recvfrom ne le fait pas
//printf("\nVoici les donnees : %s",buffer);
StructureSupervision::T_StructureSupervision *structureReception = (StructureSupervision::T_StructureSupervision *) buffer;
std::cout << "Voici le numero de Statut Ground Flight : " << structureReception->SystemData._statutGroundFlight;
erreur=closesocket(id_de_la_socket);
if (erreur!=0)
printf("\nDesole, je ne peux pas liberer la socket du a l'erreur : %d %d",erreur,WSAGetLastError());
else
printf("\nclosesocket : OK");
// ********************************************************
// Quitte proprement le winsock ouvert avec la commande WSAStartup
// ********************************************************
erreur=WSACleanup(); // A appeler autant de fois qu'il a �t� ouvert.
if (erreur!=0)
printf("\nDesole, je ne peux pas liberer winsock du a l'erreur : %d %d",erreur,WSAGetLastError());
else
printf("\nWSACleanup : OK");
return structureReception;
}
}
最佳答案
感谢您的帮助,问题是:该程序在循环中运行,因此我在错误的时间打开和关闭套接字。
关于c++ - recvfrom 超时(winsock2 和 udp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24506975/
伙计们, 这个问题困扰我好久了,求大神指点!我在一台笔记本电脑上有服务器,它正在将数据发送到另一台笔记本电脑上的客户端,同一个局域网。我可以在服务器端和客户端的 wireshark 上看到 udp 数
这是我的 udp 服务器端代码 #include"headers.h" int main(int argc,char **argv) { //------------udp server so
给定len参数设置为10240,我必须反复调用该函数以获取所有数据。但这最终导致阻塞。如何获得所有数据并以独立于平台的方式安全返回? 顺便说一句,我在发送方使用netcat: cat ocr_pi.p
我正在使用 recvfrom 从网络接收多播消息。 系统上有两个网络接口(interface),recvfrom 只从第一个接口(interface)接收。 当第 2 个接口(interface)正常
我想处理 SIGUSR1 信号,所以我做了它需要的一切并且它工作了。但是在我的程序中,我在 recvfrom 等待,当我处理信号然后返回到 recvfrom 时,我从这个函数中得到了 -1。是否可以返
这是一个多客户端套接字程序。 当我尝试运行我的程序时,最后几个recvfrom() 不返回任何数据。但之前的recvfrom有多余的数据。有人可以帮我解决这个问题吗? 客户: while (1) {
我有一个 UDP 服务器实现,我在 recvfrom 调用中遇到段错误。 #define SIZEOF sizeof #define PKTSIZE 65535 char tmp_bu
在基于客户端-服务器架构的设置中,我的客户端通过大小为 116 字节的 UDP 套接字发送数据。但在服务器端,程序只接收24个字节。如有任何帮助,我们将不胜感激。 我尝试使用wireshark捕获数据
我正在编写一个简单的 udp 服务器应用程序,当我尝试获取远程连接用户的地址时遇到错误。 while(run == 1) { struct sockaddr_in client_addr;
我最近用 C 语言编写了一个 netflow 接收器,它通过 UDP 监听 netflow。一旦处理完流数据报,它就会将其写入系统日志。我的问题是,一旦我收到带有标准 recvfrom 函数的数据报包
我有一个简单的代码来发送和接收 ICMP 数据包,例如 ping,一切正常:我的数据包已发送并且收到了结果。我的recvfrom结果有问题,缓冲区中的ip dest/src不是服务器dest ip。
我有开源 cod(这是它的链接) 访问https://github.com/jtRIPper/dns-tcp-socks-proxy/blob/master/dns_proxy.c在鳕鱼的这一部分
在准备第一次编码 UDP 时,我正在尝试一些从 here 复制并稍微修改过的示例客户端和服务器代码。 。一切似乎都正常,除了由 recvfrom() 返回的值始终是缓冲区的大小而不是读取的字节数(如果
我正在用 C 语言编写一个简单的多线程客户端-服务器 UDP 程序,其中我一次向服务器发送多条消息,并在接收线程中等待回复。我这样做是为了测试发送和接收数据包的时间。 我已经用我的服务器在本地测试了
我需要实现以下行为:当服务器启动时,它应该使用广播检查现有服务器。然后它等待回答。 但是,如何设置等待超时? int optval = 1; char buff[BUFF_SIZE]; SOCKADD
您好,我是 Socket 编程的新手,我尝试创建一个客户端服务器应用程序,其中我的服务器是相机,我的 C++ 应用程序中是客户端。当我看到计算机和相机之间的数据包传输时,它显示相机在停止后发送了超过
我在使用 recvfrom() 时遇到一些问题,无法在 C++ 中获取所有数据包。 (然后阻塞不返回) 我发送了 1 个查询数据包,然后发送了一个响应。 它被分成多个805字节的数据包,然后以一个~2
我不明白 recvfrom 在以下情况下的行为:我有一个发送和接收 udp 数据包并休眠的循环;根据该位接收 (recvfrom) 延迟会有所不同。如果我睡了 22 毫秒或更长时间,则延迟约为 170
我在编写小型 DNS 服务器并将其 strip 化到最低限度时偶然发现了一个奇怪的行为。该程序应在 127.0.0.1:1337 上监听 DNS 查询并以拒绝回复。我通过发出 dig @localho
我正在尝试通过 udp 实现距离 vector 路由。我有以下结构: struct cost_info { uint8_t id; uint8_t pad; uint16_t
我是一名优秀的程序员,十分优秀!