1
我想连接到具有此客户端代码的特定服务器使用winsock。 但创建套接字是无效,以便我得到“无效套接字”。winsock客户端套接字无效
//Creating a socket for connecting to server
SOCKET hSocket;
hSocket = socket(AF_INET, SOCK_STREAM,0);
if (hSocket == INVALID_SOCKET) //this is true!
{
cout << "INVALID SOCKET" << endl;
}
/* This code assumes a socket has been created and its handle
is stored in a variable called hSocket */
sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(54123);
sockAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
// Connect to the server
if (connect(hSocket, (sockaddr*)(&sockAddr), sizeof(sockAddr)) != 0)
{
cout << "ERROR while connecting" << endl;
}