Home > Software engineering >  Shows failed to create a socket, consult bosses?
Shows failed to create a socket, consult bosses?

Time:09-20

 # include "winsock2. H" 
# include "ws2tcpip. H"
#include
#include
#include
# pragma comment (lib, "ws2_32")
using namespace std;
Typedef struct _IP_HEADER
{
The union
{
BYTE Version;
BYTE HdrLen;
};
BYTE ServiceType.
WORD TotalLen;
The WORD ID;
The union
{
WORD Flags;
WORD Fragoff;
};
BYTE TimeToLive;
BYTE Protocol;
WORD HdrChksum;
DWORD SrcAddr;
DWORD DstAddr;
BYTE Options;

} IP_HEADER;

Int main ()
{
The SOCKET sock;
WSADATA wsData;
Ofstream ofs (" IP. The log ", the ios: : app);
If (WSAStartup (MAKEWORD (2, 2), & amp; WsData)!=0)
{
Printf (" WSAStartup failed! \n");
return -1;
}
If ((the sock=socket (AF_INET SOCK_RAW, IPPROTO_IP))==INVALID_SOCKET)
{
Printf (" create socket failed \ n ");
return -1;
}

BOOL flag=true;
If (setsockopt (sock, IPPROTO_IP IP_HDRINCL, (char *) & amp; Flag, sizeof (flag))==SOCKET_ERROR)
{
Printf (" setsockopt failed! \n");
return -1;
}
Char hostName [128].
If (gethostname (hostName, 100)==SOCKET_ERROR)
{
Printf (" gethostname failed! \n");
return -1;
}
Hostent * pHostIP;
If ((pHostIP=gethostbyname (hostName))==NULL)
{
Printf (" gethostbyname failed \ n ");
return -1;
}
Sockaddr_in addr_in;
Addr_in. Sin_addr=* (in_addr *) pHostIP - & gt; H_addr_list [0].
Addr_in. Sin_family=AF_INET;
Addr_in. Sin_port=htons (6000);
If (bind the sock, (PSOCKADDR) & amp; Addr_in, sizeof (addr_in))==SOCKET_ERROR)
{
Printf (" bind failed \ n ");
return -1;
}
# define IO_RCVALL _WSAIOW (IOC_VENDOR, 1)
DWORD dwBufferLen [10].
DWORD dwBufferInLen=1;
DWORD dwBytesReturned=0;
Char buffer [100].
If (WSAIoctl (sock, IO_RCVALL, & amp; DwBufferInLen, sizeof (dwBufferInLen),
& DwBufferLen, sizeof (dwBufferLen), & amp; DwBytesReturned, NULL, NULL)==SOCKET_ERROR)
{
Printf (" ioctlsocket faild \ n ");
return -1;
}

Printf (" Analysis of IP Packet! \n\n");
String s;
Char * p;
Char buffer1 [100].
While (true)
{
Int size=recv (sock, buffer, sizeof (buffer), 0).
IP_HEADER IP=* (IP_HEADER *) buffer;
Cout & lt; <"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --" & lt; S +="\ n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n";
Cout & lt; <"Version:" & lt; <(IP Version & gt;> 4) & lt; _itoa_s (IP Version & gt;> 4, buffer1, 10);
S +="Version:";
S +=buffer1;
S +="\ n";
Cout & lt; <"IHL:" & lt; <((IP) HdrLen & amp; 0 x0f) * 4) & lt; S +="IHL:";
_itoa_s ((IP) HdrLen & amp; 0 x0f) * 4, buffer1, 10);
S +=buffer1;
S +="\ n";
Cout & lt; <"Type of service: the Priority" & lt; <(IP) ServiceType & gt;> 5) & lt;
"Service" & lt; <((IP) ServiceType & gt;> 1) & amp; 0 x0f) & lt; S +="Type of service: the Priority".
_itoa_s (IP) ServiceType & gt;> 5, buffer1, 10);
S +=buffer1;
S +="Service";
_itoa_s ((IP) ServiceType & gt;> 1) & amp; 0 x0f, buffer1, 10);
S +=buffer1;
S +="\ n";

Cout & lt; <"The TOtal Length:" & lt; S +="TOtal Length:";
_itoa_s (IP) TotalLen, buffer1, 10);
S +=buffer1;
S +="\ n";
Cout & lt; <"Identifcation:" & lt; S +="Identifcation:";
_itoa_s (IP) ID, buffer1, 10);
S +=buffer1;
S +="\ n";
Cout & lt; <"The sign:" & lt; <((IP) Flags & gt;> 15) & amp; 0 x01) & lt; <", DF="& lt;
((IP) Flags & gt;> 14) & amp; 0 x01) & lt; <", Mf="& lt; <((IP) Flags & gt;> 13) & amp; 0 x01) & lt; S +="sign:";
_itoa_s (IP) Flags & gt;> 15) & amp; 0 x01, buffer1, 10);
S +=buffer1;
S +=", DF=";
_itoa_s (IP) Flags & gt;> 14) & amp; 0 x01, buffer1, 10);
S +=buffer1;
S +=", Mf=";
_itoa_s (IP) Flags & gt;> 13) & amp; 0 x01, buffer1, 10);
S +=buffer1;
S +="\ n";
Cout & lt; <"Fragment offset:" & lt; <(IP) Fragoff & amp; 0 x1fff) & lt; S +="Fragment offset:";
_itoa_s (IP) Fragoff & amp; 0 x1fff, buffer1, 10);
S +=buffer1;
S +="\ n";
Cout & lt; <"Time to live:" & lt; <(int) IP. TimeToLive & lt; S +="Time to live:";
_itoa_s ((int) IP TimeToLive, buffer1, 10);
S +=buffer1;
S +="\ n";
Cout & lt; <"Protocol:" & lt; <. (int), IP Protocol & lt; S +="Protocol:";
_itoa_s ((int), IP Protocol, buffer1, 10);
S +=buffer1;
S +="\ n";
Cout & lt; <"The Header Checksum:" & lt; S +="Header Checksum:";
_itoa_s (IP) HdrChksum, buffer1, 10);
S +=buffer1;
S +="\ n";
Cout & lt; <"Source address:" & lt; <(in_addr inet_ntoa (* *) & amp; IP. SrcAddr) & lt; S +="Source address:";
S +=inet_ntoa (* (in_addr *) & amp; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related