ReadFile()vs recv / recvfrom [英] ReadFile() vs recv/recvfrom

查看:95
本文介绍了ReadFile()vs recv / recvfrom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到我们可以使用ReadFile()或recv()(tcp)/ recvfrom()(udp)调用从套接字读取数据。我对此有一些疑问。

I see that we can read data from a socket using ReadFile() or recv()(tcp)/recvfrom()(udp) calls. I have some questions regarding this.

1。它们基本相同吗?

1. Are they basically the same?

2。可以在同一个UDP套接字上同时使用ReadFile()和recvfrom()吗?

2. Can is it OK to use both ReadFile() and recvfrom() on the same UDP socket?

3。我的情况是我的应用程序在UDP套接字上调用重叠的ReadFile()。在调用ReadFile()时,预期数据尚未到达,因此ReadFile()返回ERROR_IO_PENDING。后来我使用网络监控工具验证了
预期数据是否已经到达我的PC。但即使在此之后,ReadFile()仍未完成。可能是什么原因?

3. I have a situation where my application calls an overlapped ReadFile() on a UDP socket. At the time ReadFile() is called, the expected data has not arrived, so ReadFile() returns ERROR_IO_PENDING. Later I verify using network monitoring tool that the expected data has arrived on my PC . But the ReadFile() does not get finished even after this. What could be the reason?

以下是我初始化套接字的方法。

Here is how I initialize the socket.

struct sockaddr_in server, si_other;

int createsocket(char * host,char * port)
{
int sockfd;
WSADATA wsa;

int slen = sizeof(si_other);

if(WSAStartup(MAKEWORD(2,2),& wsa)!= 0)
{

return -1;
}

if((sockfd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))== SOCKET_ERROR)
{

return -1;
}
memset((char *)& si_other,0,sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons((unsigned short)strtoul(port,NULL,0));
si_other.sin_addr.S_un.S_addr = inet_addr((const char *)host);
}

int createsocket ( char *host, char *port ) { int sockfd; WSADATA wsa; int slen = sizeof ( si_other ); if ( WSAStartup ( MAKEWORD ( 2, 2 ), &wsa ) != 0 ) { return -1; } if ( (sockfd = socket ( AF_INET, SOCK_DGRAM, IPPROTO_UDP )) == SOCKET_ERROR ) { return -1; } memset ( ( char * ) &si_other, 0, sizeof ( si_other ) ); si_other.sin_family = AF_INET; si_other.sin_port = htons( ( unsigned short ) strtoul ( port, NULL, 0 )); si_other.sin_addr.S_un.S_addr = inet_addr ( (const char*)host ); }


我正在等待重叠事件等待的WaitForXXXX()函数超时。当我将INFINITE指定为timeperiod时,ReadFile()不会返回静止。

I am getting a timeout for WaitForXXXX() function on which the overlapped event waits. In When I specify INFINITE as timeperiod, ReadFile() doesn't return still.

推荐答案

由于历史原因,通常面向文件的读写系统调用也适用于套接字。现在最好使用专用API作为套接字。正如您所注意到的那样,特殊的套接字功能也可以更好地工作。

For historic reasons, the usual file-oriented read and write system calls work also for sockets. Nowadays it is better to use dedicated API for sockets. The special sockets functions also work better, as you have noticed.

- pa


这篇关于ReadFile()vs recv / recvfrom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆