标签:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sys/socket.h>
#include <netdb.h>
#include <fcntl.h>
static int setSocketNonBlock(const int socketFd)
{
int ret(0);
int flags = fcntl(socketFd, F_GETFL, 0);
if (-1 == flags)
{
perror("fcntl");
return -1;
}
flags |= O_NONBLOCK;
ret = fcntl(socketFd, F_SETFL, flags);
if (-1 == ret)
{
perror("fcntl");
return -1;
}
return 0;
}
static int setSocketLingerDisable(const int socketFd)
{
int ret(0);
struct linger lingerVal;
lingerVal.l_onoff = 1;
lingerVal.l_linger = 0;
ret = setsockopt(socketFd, SOL_SOCKET, SO_LINGER, &lingerVal, sizeof(struct linger));
if (0 != ret)
{
perror("setsockopt");
return -1;
}
return 0;
}
static int create_and_bind()
{
int ret(0);
struct addrinfo hints;
struct addrinfo * retAddr, * retNext;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
ret = getaddrinfo(NULL, "8090", &hints, &retAddr);
if (0 != ret)
{
perror("getaddrinfo");
return -1;
}
for (retNext = retAddr; retNext != NULL; retNext = retNext->ai_next)
{
char ipBuf[1024];
switch(retNext->ai_family)
{
case AF_INET:
inet_ntop(AF_INET, ((struct sockaddr_in *)(retNext->ai_addr))->sin_addr, ipBuf, sizeof(char[1024]))
break;
case AF_INET6:
break;
}
inet_ntop()
}
return 0;
}
int main(int argc, char **argv)
{
return 0;
}
标签:
原文地址:http://www.cnblogs.com/itpoorman/p/5180123.html