头文件:
#include <sys/socket.h>
#include <netdb.h>
//创建一个socket协议。比如:icmp,tcp等
struct protoent* protocolInformation;
const int krecieveSocketBufferSize = 50 * 1024; //here we want 50K for size of receive buffer
struct timeval pingTimeout;
int error = 0;
int SocketToReturn;
//netdb.h
//struct protoent *getprotobyname(const char *);
//struct protoent *getprotobynumber(int);
//struct protoent *getprotoent(void);
protocolInformation = getprotobyname("icmp");
if (protocolInformation == NULL)
{
return(EPFNOSUPPORT); //return protocol not supported UNIX error
}
SocketToReturn = socket(AF_INET, SOCK_DGRAM, protocolInformation->p_proto); //IPPROTO_TCP
if (*SocketToReturn < 0)
{
return(EPERM);
}
//设置一个socket操作
(void) setsockopt(*SocketToReturn, SOL_SOCKET, SO_RCVBUF,
&krecieveSocketBufferSize, sizeof(krecieveSocketBufferSize));
//close:
close(*SocketToReturn);