WSASendTo WSARecvFrom在msdn上的代码的错误
来源:互联网 发布:网络写手如何取名 编辑:程序博客网 时间:2024/06/07 15:02
这个错误让我深深的迷惑了好长时间
我们看一下源代码
#include <stdio.h>#include "winsock2.h"void main() { //--------------------------------------------- // Declare and initialize variables WSADATA wsaData; WSABUF DataBuf; WSAOVERLAPPED Overlapped; SOCKET SendSocket; sockaddr_in RecvAddr; sockaddr_in LocalAddr; int RecvAddrSize = sizeof(RecvAddr); int LocalAddrSize = sizeof(LocalAddr); int Port = 27015; char *ip; char SendBuf[1024] = "Data buffer to send"; int BufLen = 1024; DWORD BytesSent = 0, Flags = 0; //--------------------------------------------- // Initialize Winsock WSAStartup(MAKEWORD(2,2), &wsaData); //--------------------------------------------- // Create a socket for sending data SendSocket = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, WSA_FLAG_OVERLAPPED); //--------------------------------------------- // Set up the RecvAddr structure with the IP address of // the receiver (in this example case "123.123.123.1") // and the specified port number. RecvAddr.sin_family = AF_INET; RecvAddr.sin_port = htons(Port); RecvAddr.sin_addr.s_addr = inet_addr("123.123.123.1"); //--------------------------------------------- // Set up the LocalAddr structure with the local IP address // and the specified port number. hostent* localHost; localHost = gethostbyname(""); ip = inet_ntoa (*(struct in_addr *)*localHost->h_addr_list); LocalAddr.sin_family = AF_INET; LocalAddr.sin_addr.s_addr = inet_addr(ip); LocalAddr.sin_port = htons(Port); //--------------------------------------------- // Bind the sending socket to the LocalAddr structure // that has the internet address family, local IP address // and specified port number. bind(SendSocket, (sockaddr*) &LocalAddr, LocalAddrSize); //--------------------------------------------- // Send a datagram to the receiver printf("Sending a datagram...\n"); DataBuf.len = BufLen; DataBuf.buf = SendBuf; WSASendTo(SendSocket, &DataBuf, 1, &BytesSent, Flags, (SOCKADDR*) &RecvAddr, RecvAddrSize, &Overlapped, NULL); //--------------------------------------------- // When the application is finished sending, close the socket. printf("Finished sending. Closing socket.\n"); closesocket(SendSocket); printf("Exiting.\n"); //--------------------------------------------- // Clean up and quit. WSACleanup(); return;}
这是微软的源代码 我直接粘贴编译 返回永远是socketerror并且getlasterror之后,永远都是6(句柄无效)
需要对Overlapped.m_hevent进行赋值,然而默认值是无效的Overlapped.m_hevent = CreateEvent(NULL,TRUE,FALSE,NULL) ;使之变成有效值
0 0
- WSASendTo WSARecvFrom在msdn上的代码的错误
- sendto、WSASendto,recvfrom、WSARecvfrom 区别?
- sendto、WSASendto,recvfrom、WSARecvfrom 区别?
- IOCP12 WSASendTo不存在的目标
- 解压cab的代码 msdn上的
- WSASendTo()与WSARecvfrom()函数发送wsabuf结构体数组
- MSDN上的緩存設計網址
- MSDN上的内容
- MSDN上的strtok
- MSDN上的数据类型
- IOCP 中WSARecvFrom返回10045的问题
- 在visual c++上出现编译错误时而又无助无解的时候。。还是得看MSDN
- 在MSDN里创建胖客户端程序时MSDN的一个错误
- MSDN上的WCF入门教程
- msdn上cin的解释
- WSARecvFrom()
- 微软一站式示例代码库在MSDN代码库的新家
- 微软一站式示例代码库在MSDN代码库的新家
- 在visual studio下防止代码污染
- Ogre手动编译
- tlb、tlh和tli文件的关系
- 自己动手实现图的BFS和DFS(附完整源码)转
- 模式匹配——从BF算法到KMP算法(附完整源码)转
- WSASendTo WSARecvFrom在msdn上的代码的错误
- asp.net-css控制table构造棋盘
- AcAxDocLock
- 引用 typedef和typename关键字
- AcDbDateBase
- visual studio 正则表达式查找替换
- 转(ObjectARX杂碎--(学习指南及书籍))
- 转(ARX编程各种技术处理)
- 设置代码高亮