Home > Software engineering >  Socket programming to realize ping, recv has timeout (10060), to solve!
Socket programming to realize ping, recv has timeout (10060), to solve!

Time:10-13

This several days in the socket network programming, and the socket write a ping practice,
That he had been there will be 10060 written after the operation of the recv (receive timeout) error, but with the ping CMD and someone else's code can be online (measured 127.0.0.1)

Below is the code, the have to copy down and the success of the code is almost online, is really not recruit, please correct me!
 
# include "iostream"
#include
#include

using namespace std;
# pragma comment (lib, "Ws2_32. Lib") loading Ws2_32.//lib

Typedef struct icmp_header
{
Unsigned char icmp_type;//message type
Unsigned char icmp_code;//code
Unsigned short icmp_checksum;//the checksum
Unsigned short icmp_id;//used to mark the request ID number, usually set to process ID
Unsigned short icmp_sequence;//serial number
Unsigned long icmp_timestamp;//timestamp
} icmp_header, * PICMPHDR;

# define ICMP_HEADER_SIZE sizeof (icmp_header)
# define ICMP_ECHO_REQUEST 0 x08
# define ICMP_ECHO_REPLY 0 x00

//calculating checksum
Unsigned short chsum (struct icmp_header * picmp, int len)
{
Long sum=0;
Unsigned short * pusicmp=(unsigned short *) picmp;

While (len & lt; 1)
{
The sum +=* (pusicmp++);
If (sum & amp; 0 x80000000)
{
Sum=(sum & amp; 0 XFFFF) + (sum & gt;> 16);
}
Len -=2;
}
If (len)
{
The sum +=(unsigned short) * (unsigned char *) pusicmp;
}
While (sum & gt;> 16)
{
Sum=(sum & amp; 0 XFFFF) + (sum & gt;> 16);
}
Return (unsigned short) ~ sum;
}

//the SOCK error handler
Void CheckSockError (int nErrorCode, char * pErrorMsg)
{
//printf (" I am here \ n ");
Printf (" nErrorCode % d \ n ", nErrorCode);
If (nErrorCode==SOCKET_ERROR) {
Printf (" \ n "s Error: d, pErrorMsg, GetLastError ());
//closesocket (nSockRaw);
//ExitProcess (1);
}
}

Bool MyPing (char * szDestIp)
{

Bool bRet=TRUE;

Char icmp_data [32]={0};
WSADATA WSADATA;
WSAStartup (MAKEWORD (2, 2), & amp; Wsadata);

//set the destination address
Sockaddr_in dest_addr;
ZeroMemory (& amp; Dest_addr, sizeof (sockaddr_in));
Dest_addr. Sin_family=AF_INET;
Dest_addr) sin_addr) S_un. S_addr=inet_addr (szDestIp);
Dest_addr. Sin_port=htons (0);
//dest_addr) sin_addr) s_addr=szDestIp;

//create a raw socket
//SOCKET s=WSASocket (AF_INET SOCK_RAW, IPPROTO_ICMP, NULL, 0, WSA_FLAG_OVERLAPPED);
The SOCKET s=SOCKET (AF_INET, SOCK_RAW IPPROTO_ICMP);
CheckSockError (s, "the setsockopt ()");
DWORD dwError1=WSAGetLastError ();
Printf (" ErrorCode % d \ n ", dwError1);

Int nErrorCode;
Int nTimeOut=1000;

NErrorCode=setsockopt (s, SOL_SOCKET, SO_SNDTIMEO, (char const *) & amp; NTimeOut, sizeof (int));//set to send timeout
If (SOCKET_ERROR==nErrorCode)
{
cout <"The setsockopt erro" & lt; Closesocket (s);
WSACleanup ();
return -1;
}

//set to receive a timeout
NErrorCode=setsockopt (s, SOL_SOCKET, SO_RCVTIMEO, (char const *) & amp; NTimeOut, sizeof (int));
CheckSockError (nErrorCode, "setsockopt ()");
DWORD dwError2=WSAGetLastError ();
Printf (" ErrorCode % d \ n ", dwError2);

Char szBuff [ICMP_HEADER_SIZE + 32]={0};
Memset (szBuff, 0, sizeof (szBuff));
SzBuff icmp_header * pIcmp=(icmp_header *);

//structure ICMP packet
PIcmp - & gt; Icmp_type=ICMP_ECHO_REQUEST;
PIcmp - & gt; Icmp_code=0;
PIcmp - & gt; Icmp_checksum=0;
PIcmp - & gt; Icmp_id=(USHORT) : : GetCurrentProcessId ();
PIcmp - & gt; Icmp_sequence=0;
PIcmp - & gt; Icmp_timestamp=0;

Unsigned short sequence=0;//serial number
Int count=4;

Char szRecvBuff [1024].
Sockaddr_in from_addr;
Int nLen=sizeof (SOCKADDR);

//copy data
Memcpy ((+ ICMP_HEADER_SIZE szBuff), "abcdefghijklmnopqrstuvwabcdefghi", 32).

//memset (szRecvBuff, 0, sizeof (szRecvBuff));

While (count) {
PIcmp - & gt; Icmp_checksum=0;
PIcmp - & gt; Icmp_timestamp=: : GetTickCount ();
PIcmp - & gt; Icmp_sequence=sequence++;
//calculating checksum
PIcmp - & gt; Icmp_checksum=chsum ((struct icmp_header *) szBuff, sizeof (szBuff));

int result;

Result=sendto (s, szBuff, sizeof (szBuff), 0, (SOCKADDR *) & amp; Dest_addr, sizeof (dest_addr));
CheckSockError (result, "sendto ()");
DWORD dwError3=WSAGetLastError ();
Printf (" ErrorCode % d \ n ", dwError3);

Result=recvfrom (s, szRecvBuff MAXBYTE, 0, (SOCKADDR *) & amp; From_addr, & amp; NLen);
CheckSockError (result, "recv ()");
DWORD dwError4=WSAGetLastError ();
Printf (" ErrorCode % d \ n ", dwError4);

Printf (" recv=% s ", szBuff);
Int length=strlen (szBuff);
If (length & gt; 0)
{
cout <"This IP is online
"<"Received bytes" & lt; }
The else
{
cout <"This IP is not online" & lt; }

cout cout

//address to determine whether the received his request
If (LSTRCMP (inet_ntoa (from_addr sin_addr), szDestIp))
{
BRet=FALSE;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related