socket状态表 网络编程头文件 网络地址转换

2015-08-29 22:26:00
admin
原创 1287
摘要:socket状态表 网络编程头文件 网络地址转换

一、socket状态表

状态 描述 备注
ESTABLISHED
The socket has an established connection.

SYN_SENT
The socket is actively attempting to establish a connection.

SYN_RECV
A connection request has been received from the network.

FIN_WAIT1
The socket is closed, and the connection is shutting down.

FIN_WAIT2
Connection is closed, and the socket is waiting for a shutdown from the remote end.

TIME_WAIT
The socket is waiting after close to handle packets still in the network.

CLOSED
The socket is not being used.

CLOSE_WAIT
The remote end has shut down, waiting for the socket to close.

LAST_ACK
The remote end has shut down, and the socket is closed. Waiting for acknowledgement.

LISTEN
The socket is listening for incoming connections.

CLOSING
Both sockets are shut down but we still don’t have all our data sent.

UNKNOWN
The state of the socket is unknown.


二、网络编程头文件

说明:socket独立,arpa包含netinet;


ls /usr/include/sys | grep socket
socket.h
socketvar.h


ls /usr/include/arpa | cat
ftp.h
inet.h
nameser_compat.h
nameser.h
telnet.h
tftp.h


ls /usr/include/netinet | cat
ether.h
icmp6.h
if_ether.h
if_fddi.h
if_tr.h
igmp.h
in.h
in_systm.h
ip6.h
ip.h
ip_icmp.h
tcp.h
udp.h


三、网络地址转换

下载:ip_convert.cpp

编译:g++ -g ip_convert.cpp -o ip_convert

说明:inet_pton输出大端模式


class IPConvert
{
public:
static void getStringIP(uint32_t ip, char *strIP)
{
in_addr addr;
addr.s_addr = ip;
inet_ntop(AF_INET, (void *)&addr, strIP, INET_ADDRSTRLEN);
}
static void getStringIP(in_addr &addr, char *strIP)
{
inet_ntop(AF_INET, (void *)&addr, strIP, INET_ADDRSTRLEN);
}
static in_addr getIntIP(const char *strIP)
{
in_addr addr;
inet_pton(AF_INET, strIP, (void *)&addr);
return addr;
}
};
void testIPConvert()
{
in_addr address;
char strIP[INET_ADDRSTRLEN];
memset(strIP, 0, INET_ADDRSTRLEN);
IPConvert::getStringIP(338799626, strIP);
printf("%s\n", strIP);
address = IPConvert::getIntIP("255.255.255.255");
printf("%d\n", address.s_addr);
IPConvert::getStringIP(address, strIP);
printf("%s\n", strIP);
}


输出结果:

10.172.49.20
-1
255.255.255.255

发表评论
评论通过审核之后才会显示。