1 socket函数
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domin,int type,int protocol);
1.1参数
1.domain:
AF_INET IPv4 Internet protocols ip(7)
AF_INET6 IPv6 Internet protocols ipv6(7)
AF_UNIX, AF_LOCAL Local communication unix(7)
AF_NETLINK Kernel user interface device netlink(7)
AF_PACKET Low level packet interface packet(7)
2.type:
SOCK_STREAM: 流式套接字 唯一对应于TCP
SOCK_DGRAM: 数据报套接字,唯一对应着UDP
SOCK_RAW: 原始套接字
3.protocol: 一般填0,原始套接字编程时需填充
1.2 返回值:
RETURN VALUE
On success, a file descriptor for the new socket is returned. On
error, -1 is returned, and errno is set appropriately.
成功时返回文件描述符,出错时返回为-1
2 bind()函数
#include <sys/types.h>
#include <sys/socket.h>
int bind(int sockfd,const struct sockaddr *addr,socklen_t addrlen);
2.1参数
sockfd: 通过socket()函数拿到的fd
addr: struct sockaddr的结构体变量的地址
addrlen: 地址长度
![](https://i-blog.csdnimg.cn/blog_migrate/88166dc7db8353c1e6d4c89c8fe46eb0.png)
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
如果是IPV6的编程,要使用struct sockddr_in6结构体(详细情况请参考man 7 ipv6),通常更通用的方法可以通过struct sockaddr_storage来编程
示例代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#define SERV_PORT 5001
#define SERV_IP_ADDR 192,168,5,5
int main(int argc, char *argv[])
{
int fd = -1;
struct sockaddr_in sin;
//1创建套接字
if(fd = socket(AF_INET,SOCKET_STREAM,0)==-1){
perroe("socket");
return 0;
}
//2.绑定
//2.1填充struct sockaddr_in 变量
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SERV_PORT);
#if 0
sin.sin_addr = inet_addr(SERV_IP_ADDR);
#else
if(inet_pton(AF_INET,SERV_IP_ADDR,(void *)&sin_addr)!=1){
perror("inet_pton");
return 0;
}
#endif
//2.2绑定
if(bind(fd,(struct sockaddr *)&sin,sizeod(sin))<0){
perror("bind");
return 0;
}
return 0;
}
3 listen()函数
#include <sys/types.h>
#include <sys/socket.h>
int listen(int sockfd,int BACKLOG);
参数:
sockfd: 通过socket()函数拿到的fd
backlog: 同时允许几路客户端和服务器进行正在连接的过程(正在三次握手)
一般填5, 测试得知,ARM最大为8
内核中服务器的套接字fd会维护2个链表: 1. 正在三次握手的的客户端链表(数量=2*backlog+1) 2.已经建立好连接的客户端链表(已经完成3次握手分配好了newfd) |
返回值:
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
4 accept()函数 阻塞等待客户端连接请求
#include <sys/types.h>
#incldue <sys/socket.h>
int accept(int sockfd,struct sockaddr *addr,socklen_t *addrlen);
4.1参数:
sockfd: 经过前面socket()创建并通过bind(),listen()设置过的fd
addr和addrlen: 获取连接过来的客户的信息
4.2 返回值:
RETURN VALUE
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.
成功时返回已经建立好连接的新的newfd
代码示例
头文件 net.h
#ifndef _CHRIS_NET_H
#define _CHRIS_NET_H
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#define SERV_PORT 5001
#define SERV_IP_ADDR "192.168.28.184"
#define BACKLOG 5
#define QUIT_STR "quit"
#endif
服务端
#include "net.h"
int main(int argc, char *argv[])
{
int fd = -1;
struct sockaddr_in sin;
//1创建套接字
if((fd = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
return 0;
}
//2.绑定
//2.1填充struct sockaddr_in 变量
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SERV_PORT);//网络字节序的端口号
/*优化1:让服务端可以绑定任意ip地址*/
#if 1
sin.sin_addr.s_addr = htonl(INADDR_ANY);
#else
if(inet_pton(AF_INET,SERV_IP_ADDR,(void *)&sin.sin_addr)!=1){
perror("inet_pton");
return 0;
}
#endif
//2.2绑定
if(bind(fd,(struct sockaddr *)&sin,sizeof(sin))<0){
perror("bind");
return 0;
}
//3主动套接字变为被动套接字
if(listen(fd,5)<0){
perror("listen");
return 0;
}
//4阻塞等待客户端连接请求
int newfd = -1;
/*优化2:让服务端可以识别是哪个客户端*/
#if 1
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
newfd = accept(fd,(struct sockaddr *)&cin,&addrlen);
char ipv4_addr[16];
if(!inet_ntop(AF_INET,(void *)&cin.sin_addr,ipv4_addr,sizeof(cin))){
perror("inet_ntop");
exit(1);
}
printf("Client(%s,%d) is connect\n",ipv4_addr,ntohs(cin.sin_port));
#else
newfd = accept(fd,NULL,NULL);
if(newfd<0){
perror("accept");
exit(1);
}
#endif
//5读取数据
int ret = -1;
char buf[BUFSIZ];
while(1){
do{
ret = read(newfd,buf,BUFSIZ);
}while(ret<0&&EINTR==errno);
if(ret<0){
perror("read");
return 0;
}
if(!ret){
break;
}
printf("recive data = %s\n",buf);
if(!strncasecmp(buf,QUIT_STR,strlen(QUIT_STR))){
printf("client is exit\n");
break;
}
}
close(newfd);
close(fd);
return 0;
}
客户端
#include "net.h"
int main(int argc, char *argv[])
{
int fd = -1;
struct sockaddr_in sin;
//1创建套接字
if((fd = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
return 0;
}
//2.连接
//2.1填充struct sockaddr_in 变量
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SERV_PORT);
#if 1
sin.sin_addr.s_addr = inet_addr(SERV_IP_ADDR);
#else
if(inet_pton(AF_INET,SERV_IP_ADDR,(void *)&sin.sin_addr)!=1){
perror("inet_pton");
return 0;
}
#endif
//2.2连接
printf("ip is %s\n",SERV_IP_ADDR);
if(connect(fd,(struct sockaddr *)&sin,sizeof(sin))<0){
perror("connect");
return 0;
}
printf ("Client staring...OK!\n");
//4读写数据
int ret = -1;
char buf[BUFSIZ];
while(1){
bzero(buf,BUFSIZ);
if(fgets(buf,BUFSIZ-1,stdin)==NULL){
continue;
}
do{
write(fd,buf,strlen(buf));
}while(ret<0&&EINTR == errno);
if(!strncasecmp(buf,QUIT_STR,strlen(QUIT_STR))){
printf("client is exit\n");
break;
}
}
close(fd);
return 0;
}