- 论坛徽章:
- 0
|
程序老是在bind()时报10049号错误,不知道为什么,哪位高手指点一下?谢谢,代码如下:
#include "testsocket.h"
int main()
{
int ret;
WSADATA wsadata;
SOCKET sock;
char hostname[512];
struct hostent *localhost;
WSAStartup(MAKEWORD(2,2), &wsadata);
sock = WSASocket(AF_INET, SOCK_RAW, IPPROTO_RAW, NULL, 0, WSA_FLAG_OVERLAPPED);
if (sock == INVALID_SOCKET)
{
return -1;
}
bool flag = true;
ret = setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)&flag, sizeof(flag));
if (ret != 0)
{
cout << "set error!" << WSAGetLastError() << endl;
return -1;
}
memset(hostname, 0, sizeof(hostname));
gethostname(hostname, sizeof(hostname));
localhost = gethostbyname(hostname);
if (localhost == NULL)
{
return -1;
}
cout << "myip: " << inet_ntoa(*(struct in_addr *)(localhost->;h_addr_list[0])) << endl;
struct sockaddr_in myaddr;
myaddr.sin_family = AF_INET;
myaddr.sin_addr = *(struct in_addr *)(localhost->;h_addr_list[0]);
myaddr.sin_port = htons(34432);
ret = bind(sock, (PSOCKADDR)&myaddr, sizeof(myaddr));
if (ret != 0)
{
cout << "bind error: " << WSAGetLastError() << endl;
return -1;
}
DWORD dwValue = 1;
ret = ioctlsocket(sock, SIO_RCVALL, &dwValue);
if (ret != 0)
{
return -1;
}
int i = 0;
char RecvBuf[BUFFER_SIZE];
MyIp *myip;
while (i < 5)
{
// 接收原始数据包信息
int ret = recv(sock, RecvBuf, BUFFER_SIZE, 0);
if (ret >; 0)
{
myip = (struct MyIp *)RecvBuf;
cout << "IP SOURCE: "<< inet_ntoa(*(struct in_addr *)myip->;send_ip) << endl;
}
}
cout << "clear" << endl;
WSACleanup();
return 0;
} |
|