Home > Software engineering >  Why not make testing network connection code
Why not make testing network connection code

Time:11-02

Test network connectivity code is as follows:

 
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 do

CodePudding 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 you

CodePudding 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 look

CodePudding user response:

refer to the eighth floor zgl7903 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();
}


After using this code, if BREAK up directly, what tips didn't pop up, what ah, brother, thank you very much

CodePudding user response:

Turned out to be a MESSAGEBOX changed to TRACK, no watch,

In addition, inet_addr (" www.baidu.com "), can only use IP, can use the url,

In addition, what is PING method, how to write code to just go?

Thank you very much

CodePudding user response:

Gethostbyname can parse the domain name

CodePudding user response:

Gethostbyname how to use it, thank you for your brother

CodePudding user response:

In addition, with the DO loop will card, still have to thread, which brother give a code, create a thread, use gethostbyname to IP, and then use the above code

Thank you brothers
  • Related