The SOCKET sockClient=SOCKET (AF_INET, SOCK_STREAM, 0);
//connect server socket
SOCKADDR_IN addrServer;
AddrServer. Sin_addr. S_un. S_addr=inet_addr (" www.baidu.com ");
AddrServer. Sin_family=AF_INET;
AddrServer. Sin_port=htons (80);
If (connect (sockClient, (SOCKADDR *) & amp; AddrServer, sizeof (addrServer))==SOCKET_ERROR)//connection specified address, port
{
MessageBox (NULL, _T (" client connection failure "), _T (" greetings "), 0).
}
The else
{
MessageBox (NULL, _T (" the client connection is successful "), _T (" greetings "), 0).
}
Position in www.baidu.com with the IP also not line,
But why always prompt connection fails? O brothers solutions, thanks brothers
CodePudding user response:
WSAStartup and WSACleanup is there?CodePudding user response:
These two what is useless, I didn't use ah, how to doCodePudding user response:
Before running this program you must first run:WSAStartup
CodePudding user response:
If (WSAStartup (MAKEWORD (1, 1), & amp; WsaData))return 0;
Look at the return value right???????
CodePudding user response:
The original poster is just to test network connectivity, why create a socket, finished with ping not directly?CodePudding user response:
WSAStartup will how to use ah, brothers for example is good, thank youCodePudding user response:
Inet_addr (" www.baidu.com ");This only with the IP address of the string, domain names,
CodePudding user response:
BOOL bWsaStartupEd=FALSE;
The SOCKET sockClient=INVALID_SOCKET;
Do
{
WSADATA wsa={0};
If (WSAStartup (MAKEWORD (2, 2), & amp; Wsa)!=0)
break;
BWsaStartupEd=TRUE;
SockClient=socket (AF_INET SOCK_STREAM, 0).
If (sockClient==INVALID_SOCKET)
break;
//connect server socket
SOCKADDR_IN addrServer;
AddrServer. Sin_addr. S_un. S_addr=inet_addr (" www.baidu.com ");
AddrServer. Sin_family=AF_INET;
AddrServer. Sin_port=htons (80);
If (connect (sockClient, (SOCKADDR *) & amp; AddrServer, sizeof (addrServer))==SOCKET_ERROR)//connection specified address, port
{
TRACE (_T (" client connection failure \ n "));
}
The else
{
TRACE (_T (" the client connection is successful "));
}
} while (0);
If (sockClient!=INVALID_SOCKET)
{
Closesocket (sockClient);
SockClient=INVALID_SOCKET;
}
If (bWsaStartupEd)
{
WSACleanup();
}
CodePudding user response:
Thank you upstairs, the elder brothers I'll try and have a lookCodePudding user response: