Linux下的socket编程实践(二)socket编程基本API简介
2015-10-02 22:30
861 查看
Socket是什么
socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模式来操作。Socket就是该模式的一个实现, socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭)。 说白了Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口。在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用户来说,一组简单的接口就是全部,让Socket去组织数据,以符合指定的协议。
Socket可以看成是用户进程与内核网络协议栈的接口(编程接口, 如下图所示), 其不仅可以用于本机进程间通信,可以用于网络上不同主机的进程间通信, 甚至还可以用于异构系统之间的通信。
如上图TCP/IP协议栈已经属于内核的一部分了,被实现好了,路由器工作于网络层(Router),Application是需要我们去实现的。Socket可以看作是用户进程和内核网络协议栈的编程接口,可以把socket看作进程间通信的一种方式,和管道不同,他是全双工的,可用于本机和不同主机之间的进程间通信,异构通信也是可以的。(例如手机和电脑,分别是ARM和X86架构)
Pv4套接口地址结构
IPv4套接口地址结构通常也称为“网际套接字地址结构”,它以“sockaddr_in”命名,定义在头文件<netinet/in.h>中
struct sockaddr_in
{
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port; //2字节
struct in_addr sin_addr; //4字节
char sin_zero[8]; //8字节
};
成员说明:
sin_len:整个sockaddr_in结构体的长度,在4.3BSD-Reno版本之前的第一个成员是sin_family.
sin_family:指定该地址家族,对于IPv4来说必须设为AF_INET(Socket不仅可以用于TCP/IP还可以用于UNIX域协议)
sin_port:端口
sin_addr:IPv4的地址;
sin_zero:暂不使用,一般将其设置为0
通用地址结构
用来指定与套接字关联的地址(可以支持其他协议).
说明:
sin_len:整个sockaddr结构体的长度
sin_family:指定该地址家族
sa_data:由sin_family决定它的形式。
注意:使用的时候通常把IPv4的地址结构强制转换成通用地址结构,就像上面的sockaddr_in 转换为sockaddr
网络字节序
大端字节序和小端字节序 的出现是为了异构系统之间的使用
1.大端字节序(Big Endian)
最高有效位(MSB:Most Significant Bit)存储于最低内存地址处,最低有效位(LSB:Lowest Significant Bit)存储于最高内存地址处。
2.小端字节序(Little Endian)
最高有效位(MSB:Most Significant Bit)存储于最高内存地址处,最低有效位(LSB:Lowest Significant Bit)存储于最低内存地址处。
3.主机字节序
不同的主机有不同的字节序,如x86为小端字节序,Motorola 6800为大端字节序,ARM字节序是可配置的。
4.网络字节序
网络字节序规定为大端字节序
判断自己主机的字节序是哪一种?
字节序转换函数(常用于端口转换)
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
/**说明:
h代表(local)host;n代表network;
s代表short;l代表long;
*/
#include <netinet/in.h>
#include <arpa/inet.h>
int inet_aton(const char *cp, struct in_addr *inp);
in_addr_t inet_addr(const char *cp);
char *inet_ntoa(struct in_addr in);
//in_addr定义如下:
typedef uint32_t in_addr_t;
struct in_addr
{
in_addr_t s_addr;
};
套接字类型
1)流式套接字(SOCK_STREAM)
提供面向连接的、可靠的数据传输服务,数据无差错,无重复的发送,且按发送顺序接收, 对应TCP协议。
2)数据报式套接字(SOCK_DGRAM)
提供无连接服务。不提供无错保证,数据可能丢失或重复,并且接收顺序混乱, 对应UDP协议。
3)原始套接字(SOCK_RAW)
使我们可以跨越传输层直接对IP层进行封装传输。(应用层直接和IP层)
socket函数
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
创建一个套接字用于通信
参数:
domain:指定通信协议族(protocol family),常用取值AF_INET(IPv4)
type:指定socket类型, 流式套接字SOCK_STREAM,数据报套接字SOCK_DGRAM,原始套接字SOCK_RAW
protocol:协议类型,常用取值0, 使用默认协议
返回值:
成功: 返回非负整数,套接字;
失败: 返回-1
bind函数
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
绑定一个本地地址到套接字
参数:
sockfd:socket函数返回的套接字
addr:要绑定的地址
//sockaddr_in结构, bind时需要强制转换成为struct sockaddr*类型
struct sockaddr_in
{
sa_family_t sin_family; /* address family: AF_INET */
in_port_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
/* Internet address. */
struct in_addr
{
uint32_t s_addr; /* address in network byte order */
};
/**示例:INADDR_ANY的使用, 绑定本机任意地址**/
int main()
{
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1)
err_exit("socket error");
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8001);
//绑定本机的任意一个IP地址, 作用同下面两行语句
addr.sin_addr.s_addr = htonl(INADDR_ANY);
//inet_aton("127.0.0.1", &addr.sin_addr);
//addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (bind(listenfd, (const struct sockaddr *)&addr, sizeof(addr)) == -1)
err_exit("bind error");
else
cout << "bind success" << endl;
}
listen函数
int listen(int sockfd, int backlog);
listen函数应该用在调用socket和bind函数之后, 并且用在调用accept之前, 用于将一个套接字从一个主动套接字转变成为被动套接字。
backlog说明:
对于给定的监听套接口,内核要维护两个队列:
1、已由客户发出并到达服务器,服务器正在等待完成相应的TCP三路握手过程(SYN_RCVD状态)
2、已完成连接的队列(ESTABLISHED状态)
但是两个队列长度之和不能超过backlog
backlog推荐使用SOMAXCONN(3.13.0-44-generic中该值为128), 使用等待队列的最大值;
bind之后变成了被动套接字,接受连接;主动套接字是用来发起连接,例如使用connect。
accept函数
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
从已完成连接队列返回第一个连接(the first connection request on the queue of pending connections for the listening
socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket. The newly created socket is not in the listening state),如果已完成连接队列为空,则阻塞。The original socket sockfd is unaffected by this call.
参数:
sockfd:服务器套接字
addr:将返回对等方的套接字地址, 不关心的话, 可以设置为NULL
addrlen:返回对等方的套接字地址长度, 不关心的话可以设置成为NULL, 否则一定要初始化
返回值: On success, these system calls return a nonnegative integer that is a descriptor for the accepted socket. On error, -1 is returned, and errno is set appropriately.
connect函数
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); <span style="background-color: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif;"> </span>
建立一个连接至addr所指定的套接字
参数:
sockfd:未连接套接字
addr:要连接的套接字地址
addrlen:第二个参数addr长度
示例:echo server/client实现
//server端代码
int main()
{
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1)
err_exit("socket error");
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8001);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(listenfd, (const struct sockaddr *)&addr, sizeof(addr)) == -1)
err_exit("bind error");
if (listen(listenfd, SOMAXCONN) == -1)
err_exit("listen error");
char buf[512];
int readBytes;
struct sockaddr_in clientAddr;
//谨记: 此处一定要初始化
socklen_t addrLen = sizeof(clientAddr);
while (true)
{
int clientfd = accept(listenfd, (struct sockaddr *)&clientAddr, &addrLen);
if (clientfd == -1)
err_exit("accept error");
//打印客户IP地址与端口号
cout << "Client information: " << inet_ntoa(clientAddr.sin_addr)
<< ", " << ntohs(clientAddr.sin_port) << endl;
memset(buf, 0, sizeof(buf));
while ((readBytes = read(clientfd, buf, sizeof(buf))) > 0)
{
cout << buf;
if (write(clientfd, buf, readBytes) == -1)
err_exit("write socket error");
memset(buf, 0, sizeof(buf));
}
if (readBytes == 0)
{
cerr << "client connect closed..." << endl;
close(clientfd);
}
else if (readBytes == -1)
err_exit("read socket error");
}
close(listenfd);
}
http://blog.csdn.net/nk_test/article/details/47733307 http://blog.csdn.net/nk_test/article/details/47756381
socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模式来操作。Socket就是该模式的一个实现, socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭)。 说白了Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口。在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用户来说,一组简单的接口就是全部,让Socket去组织数据,以符合指定的协议。
Socket可以看成是用户进程与内核网络协议栈的接口(编程接口, 如下图所示), 其不仅可以用于本机进程间通信,可以用于网络上不同主机的进程间通信, 甚至还可以用于异构系统之间的通信。
如上图TCP/IP协议栈已经属于内核的一部分了,被实现好了,路由器工作于网络层(Router),Application是需要我们去实现的。Socket可以看作是用户进程和内核网络协议栈的编程接口,可以把socket看作进程间通信的一种方式,和管道不同,他是全双工的,可用于本机和不同主机之间的进程间通信,异构通信也是可以的。(例如手机和电脑,分别是ARM和X86架构)
Pv4套接口地址结构
IPv4套接口地址结构通常也称为“网际套接字地址结构”,它以“sockaddr_in”命名,定义在头文件<netinet/in.h>中
struct sockaddr_in
{
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port; //2字节
struct in_addr sin_addr; //4字节
char sin_zero[8]; //8字节
};
成员说明:
sin_len:整个sockaddr_in结构体的长度,在4.3BSD-Reno版本之前的第一个成员是sin_family.
sin_family:指定该地址家族,对于IPv4来说必须设为AF_INET(Socket不仅可以用于TCP/IP还可以用于UNIX域协议)
sin_port:端口
sin_addr:IPv4的地址;
sin_zero:暂不使用,一般将其设置为0
通用地址结构
用来指定与套接字关联的地址(可以支持其他协议).
struct sockaddr { uint8_t sin_len; sa_family_t sin_family; char sa_data[14]; //14字节 };
说明:
sin_len:整个sockaddr结构体的长度
sin_family:指定该地址家族
sa_data:由sin_family决定它的形式。
注意:使用的时候通常把IPv4的地址结构强制转换成通用地址结构,就像上面的sockaddr_in 转换为sockaddr
网络字节序
大端字节序和小端字节序 的出现是为了异构系统之间的使用
1.大端字节序(Big Endian)
最高有效位(MSB:Most Significant Bit)存储于最低内存地址处,最低有效位(LSB:Lowest Significant Bit)存储于最高内存地址处。
2.小端字节序(Little Endian)
最高有效位(MSB:Most Significant Bit)存储于最高内存地址处,最低有效位(LSB:Lowest Significant Bit)存储于最低内存地址处。
3.主机字节序
不同的主机有不同的字节序,如x86为小端字节序,Motorola 6800为大端字节序,ARM字节序是可配置的。
4.网络字节序
网络字节序规定为大端字节序
判断自己主机的字节序是哪一种?
//测试当前系统是否为小端模式 int main() { int data = 0x12345678; //int = 4字节(32位) //每4个二进制位代表1位十六进制位, //则8位十六进制位代表4*8=32位二进制位 char *p = (char *)&data; printf("%x, %x, %x, %x\n",p[0],p[1],p[2],p[3]); //0x78属于低位,如果其放在了p[0](低地址)处,则说明是小端模式 if (p[0] == 0x78) { cout << "当前系统为小端模式" << endl; //x86平台为小端模式 } else if (p[0] == 0x12) { cout << "当前系统为大端模式" << endl; //IBM为大端模式 } }如果是小端模式,那么原串输出是 78 56 34 12
字节序转换函数(常用于端口转换)
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
/**说明:
h代表(local)host;n代表network;
s代表short;l代表long;
*/
//测试转换结果 int main() { int localeData = 0x12345678; char *p = (char *)&localeData; printf("Begin: %0x %0x %0x %0x\n", p[0], p[1], p[2], p[3]); //将本地字节转换成网络字节 int inetData = htonl(localeData); p = (char *)&inetData; printf("After: %0x %0x %0x %0x\n", p[0], p[1], p[2], p[3]); if (p[0] == 0x12) cout << "网络系统为大端模式" << endl; else cout << "网络系统为小端模式" << endl; printf("host:%x, inet:%x\n", localeData, inetData); }地址转换函数(用于IP地址转换)
#include <netinet/in.h>
#include <arpa/inet.h>
int inet_aton(const char *cp, struct in_addr *inp);
in_addr_t inet_addr(const char *cp);
char *inet_ntoa(struct in_addr in);
//in_addr定义如下:
typedef uint32_t in_addr_t;
struct in_addr
{
in_addr_t s_addr;
};
//实践 int main() { //将点分十进制转换成十进制数 cout << inet_addr("192.168.139.137") << endl; //将十进制数转换成点分十进制形式 struct in_addr address; address.s_addr = inet_addr("192.168.139.137"); cout << inet_ntoa(address) << endl; memset(&address,0,sizeof(address)); inet_aton("127.0.0.1", &address); cout << address.s_addr << endl; cout << inet_ntoa(address) << endl; return 0; }
套接字类型
1)流式套接字(SOCK_STREAM)
提供面向连接的、可靠的数据传输服务,数据无差错,无重复的发送,且按发送顺序接收, 对应TCP协议。
2)数据报式套接字(SOCK_DGRAM)
提供无连接服务。不提供无错保证,数据可能丢失或重复,并且接收顺序混乱, 对应UDP协议。
3)原始套接字(SOCK_RAW)
使我们可以跨越传输层直接对IP层进行封装传输。(应用层直接和IP层)
socket函数
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
创建一个套接字用于通信
参数:
domain:指定通信协议族(protocol family),常用取值AF_INET(IPv4)
type:指定socket类型, 流式套接字SOCK_STREAM,数据报套接字SOCK_DGRAM,原始套接字SOCK_RAW
protocol:协议类型,常用取值0, 使用默认协议
返回值:
成功: 返回非负整数,套接字;
失败: 返回-1
bind函数
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
绑定一个本地地址到套接字
参数:
sockfd:socket函数返回的套接字
addr:要绑定的地址
//sockaddr_in结构, bind时需要强制转换成为struct sockaddr*类型
struct sockaddr_in
{
sa_family_t sin_family; /* address family: AF_INET */
in_port_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
/* Internet address. */
struct in_addr
{
uint32_t s_addr; /* address in network byte order */
};
/**示例:INADDR_ANY的使用, 绑定本机任意地址**/
int main()
{
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1)
err_exit("socket error");
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8001);
//绑定本机的任意一个IP地址, 作用同下面两行语句
addr.sin_addr.s_addr = htonl(INADDR_ANY);
//inet_aton("127.0.0.1", &addr.sin_addr);
//addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (bind(listenfd, (const struct sockaddr *)&addr, sizeof(addr)) == -1)
err_exit("bind error");
else
cout << "bind success" << endl;
}
listen函数
int listen(int sockfd, int backlog);
listen函数应该用在调用socket和bind函数之后, 并且用在调用accept之前, 用于将一个套接字从一个主动套接字转变成为被动套接字。
backlog说明:
对于给定的监听套接口,内核要维护两个队列:
1、已由客户发出并到达服务器,服务器正在等待完成相应的TCP三路握手过程(SYN_RCVD状态)
2、已完成连接的队列(ESTABLISHED状态)
但是两个队列长度之和不能超过backlog
backlog推荐使用SOMAXCONN(3.13.0-44-generic中该值为128), 使用等待队列的最大值;
bind之后变成了被动套接字,接受连接;主动套接字是用来发起连接,例如使用connect。
accept函数
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
从已完成连接队列返回第一个连接(the first connection request on the queue of pending connections for the listening
socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket. The newly created socket is not in the listening state),如果已完成连接队列为空,则阻塞。The original socket sockfd is unaffected by this call.
参数:
sockfd:服务器套接字
addr:将返回对等方的套接字地址, 不关心的话, 可以设置为NULL
addrlen:返回对等方的套接字地址长度, 不关心的话可以设置成为NULL, 否则一定要初始化
返回值: On success, these system calls return a nonnegative integer that is a descriptor for the accepted socket. On error, -1 is returned, and errno is set appropriately.
connect函数
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); <span style="background-color: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif;"> </span>
建立一个连接至addr所指定的套接字
参数:
sockfd:未连接套接字
addr:要连接的套接字地址
addrlen:第二个参数addr长度
示例:echo server/client实现
//server端代码
int main()
{
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1)
err_exit("socket error");
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8001);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(listenfd, (const struct sockaddr *)&addr, sizeof(addr)) == -1)
err_exit("bind error");
if (listen(listenfd, SOMAXCONN) == -1)
err_exit("listen error");
char buf[512];
int readBytes;
struct sockaddr_in clientAddr;
//谨记: 此处一定要初始化
socklen_t addrLen = sizeof(clientAddr);
while (true)
{
int clientfd = accept(listenfd, (struct sockaddr *)&clientAddr, &addrLen);
if (clientfd == -1)
err_exit("accept error");
//打印客户IP地址与端口号
cout << "Client information: " << inet_ntoa(clientAddr.sin_addr)
<< ", " << ntohs(clientAddr.sin_port) << endl;
memset(buf, 0, sizeof(buf));
while ((readBytes = read(clientfd, buf, sizeof(buf))) > 0)
{
cout << buf;
if (write(clientfd, buf, readBytes) == -1)
err_exit("write socket error");
memset(buf, 0, sizeof(buf));
}
if (readBytes == 0)
{
cerr << "client connect closed..." << endl;
close(clientfd);
}
else if (readBytes == -1)
err_exit("read socket error");
}
close(listenfd);
}
//client端代码 int main() { int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) err_exit("socket error"); //填写服务器端口号与IP地址 struct sockaddr_in serverAddr; serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(8001); serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); if (connect(sockfd, (const struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) err_exit("connect error"); char buf[512]; while (fgets(buf, sizeof(buf), stdin) != NULL) { if (write(sockfd, buf, strlen(buf)) == -1) err_exit("write socket error"); memset(buf, 0, sizeof(buf)); int readBytes = read(sockfd, buf, sizeof(buf)); if (readBytes == 0) { cerr << "server connect closed... \nexiting..." << endl; break; } else if (readBytes == -1) err_exit("read socket error"); cout << buf; memset(buf, 0, sizeof(buf)); } close(sockfd); }另外,这里再附上windows下实现简单C/S服务器的代码,大同小异,可供参考。
http://blog.csdn.net/nk_test/article/details/47733307 http://blog.csdn.net/nk_test/article/details/47756381
相关文章推荐
- CentOS7架设vsftpd(重点叙述)
- 关于uefi的机器win8下安装CentOS双系统
- CentOS7架设vsftpd(重点叙述)
- Linux系统的头文件和库文件搜索路径
- linux后台运行和关闭、查看后台任务
- linux rcu 理解
- Archlinux编译安装mysql5.6
- Linux系统调用和库函数调用的区别
- CentOS 7更改网卡名称
- linux --scp(secure copy)命令 文件传输
- linux下的硬连接和符号链接(软连接)
- Linux | sed
- linux后台运行和关闭、查看后台任务
- CentOS构造SNMP
- linux服务器登录邮件告警python程序
- Linux(Fedora21)安装google chrome浏览器
- Nios II uCLinux/Linux启动分析
- Linux下开启和关闭Telnet服务
- linux文件处理命令——文件处理命令
- Linux - 扩展