本文介绍了WINSOCK DGRAM套接字bind()错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我认为我在Vista winsock实施中发现了一个错误,我不确定在哪里报告,所以我将其发布在这里.

说明:
如果对数据报套接字的bind()调用返回正在使用scocket WSAEADDRINUSE(10048),则对空闲端口在同一套接字上的下一次bind()调用将返回 WSAEINVAL(10022),即套接字已绑定.

繁殖方法:
编写一个简单的程序,打开2个数据报套接字,并绑定其中一个在空闲端口上,然后将第二个套接字绑定到同一端口上,因此您将获取
WSAEADDRINUSE,然后尝试绑定第二个
在一个免费端口上插入套接字,您将获得
WSAEINVAL.

测试用例:

WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2),& wsaData);


SOCKET Socket1 =套接字(PF_INET,SOCK_DGRAM,0);
SOCKET (PF_INET,SOCK_DGRAM,0);

sockaddr_in服务;

service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1") br> service.sin_port = htons(27015);


if(bind(Socket1,(SOCKADDR *)& service,sizeof(service))< 0)
{
cout<< "bind()失败." << endl;
}
else cout<< "bind()成功!" << endl;

//将失败并显示
WSAEADDRINUSE
如果(bind(Socket2,(SOCKADDR * )& service,sizeof(service))< 0)
{
cout<< "bind()失败." << WSAGetLastError()<< endl;
}
else cout<< "bind()成功!" << endl;

service.sin_port = htons(27016);

//应该不会失败,但是会失败
WSAEINVAL
如果(bind(Socket2,(SOCKADDR *)& service,sizeof(service))< 0)
{ "bind()失败." << WSAGetLastError()<< endl;
}
else cout<< "bind()成功!" << endl;

closesocket(Socket1);
closesocket(Socket2);
WSACleanup();

Hi,

I think that I found a bug in Vista winsock implementation, I'm not sure where to report, so I'll post it here.

Description:
If bind() call on datagram socket returns that scocket is in use WSAEADDRINUSE (10048), then next call of bind() on the same socket on a free port returns WSAEINVAL (10022) i.e socket is bound already.

How to reproduce:
Write a simple program that opens 2 datagram sockets, bind one of themon a free port, then bind the second socket on the same port, so you'llget
WSAEADDRINUSE, then try to bind the second
 socket on a free port and you'll get
WSAEINVAL.

Test case:

 WSADATA wsaData;
  int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
 

 
SOCKET Socket1 = socket(PF_INET, SOCK_DGRAM, 0);
 
SOCKET Socket2 = socket(PF_INET, SOCK_DGRAM, 0);

  sockaddr_in service;

  service.sin_family            = AF_INET;
  service.sin_addr.s_addr = inet_addr("127.0.0.1");
  service.sin_port                = htons(27015);


  if (bind( Socket1, (SOCKADDR*) &service, sizeof(service)) < 0)
  {
    cout << "bind() failed." << endl;
  }
  else  cout << "bind() success!" << endl;
 
 // will fail with
WSAEADDRINUSE
  if (bind( Socket2, (SOCKADDR*) &service, sizeof(service)) < 0)
  {
    cout << "bind() failed." << WSAGetLastError() << endl;
  }
  else  cout << "bind() success!" << endl;
 
  service.sin_port = htons(27016);
 
  //shouldnot fail, but it fails with
WSAEINVAL
  if (bind( Socket2, (SOCKADDR*) &service, sizeof(service)) < 0)
  {
    cout << "bind() failed." << WSAGetLastError() << endl;
  }
  else  cout << "bind() success!" << endl;
 
  closesocket(Socket1);
  closesocket(Socket2);
  WSACleanup();

推荐答案


这篇关于WINSOCK DGRAM套接字bind()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:10