Winsock Connect失败,错误为10049
本文关键字:10049 错误 Connect 失败 Winsock | 更新日期: 2023-09-27 18:21:47
我有一个c#gui,它在单击按钮时调用引用的c++dll。在c++dll中,我有以下代码
SOCKADDR_IN server;
server.sin_port=htons (54321);
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
// Connect to server.
int iResult = connect(Socket, (SOCKADDR *) & server, sizeof (server));
if (iResult == SOCKET_ERROR) {
long iError = WSAGetLastError();
if (iError == WSAEWOULDBLOCK)
printf("recv failed with error: WSAEWOULDBLOCK'n");
else
printf("recv failed with error: %ld'n", iError);
wprintf(L"connect function failed with error: %ld'n", WSAGetLastError());
iResult = closesocket(Socket);
if (iResult == SOCKET_ERROR)
wprintf(L"closesocket function failed with error: %ld'n", WSAGetLastError());
WSACleanup();
return 1;
}
在调用dll之前的c#调用程序和dll中winsock2进行的连接中,我有
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
TcpListener serverSocket = new TcpListener(localAddr,54321);
int requestCount = 0;
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
然后在调用dll后,我进行
//调用dll以运行连接代码
clientSocket = serverSocket.AcceptTcpClient();//hangs up here because dll can't Connect.
在c++dll中,当我进入连接时,我得到错误10049,这是wseaddrnotavail。
msdn-winsock错误代码
我在Connect通话中做错了什么?我根据msdn示例选择了54321作为端口。谢谢
据我所见,除非您没有在这里提供代码,否则您没有正确设置SOCKADDR_IN
结构的sin_addr
字段。它需要一个按网络字节顺序排列的IP地址。
你需要做一些类似的事情:
inet_pton(AF_INET, "127.0.0.1", &server.sin_addr);
来源:
http://msdn.microsoft.com/en-us/library/windows/desktop/cc805844(v=vs.85).aspx