#include<sys/socket.h>
#include<arpa/inet.h> //大小端转换
#include<netdb.h> //DNS
一、Socket套接字
为了开发网络应用,系统提供一套API函数接口,用于网络应用开发,这些接口称为套接字函数
struct sockaddr_int{
sin_family=AF_INET;
sin_port=8080;
sin_addr.s_addr=127.0.0.1;
}addr;
int sockfd=socket(AF_INET,SOCK_STREAM|SOCK_DRAM,0); //socket创建
//成功返回sockfd,失败返回-1
bind(int sockfd,struct sockaddr* addr/*使用旧的网络信息结构体,向前兼容*/,socklen_t addrlen);
//成功返回0,失败返回-1。对socket设置自定义信息,保持信息不变
listen(sockfd,backlog/*等待连接队列大小,默认128*/);//监听连接过程以及对应的链接事件(TCPServer)
//成功返回0,失败返回-1
htons(); //本机到网络16位,小端转大端端口号
htonl(); //小端转大端p
ntohs();
ntohl();
inet_ntop(); //大端序转字符串
inet_pton();
inet_addr();
connect(int sockfd,struct sockaddr* destaddr,sockelen_t addrlen); //请求连接函数(发起握手请求)
//成功返回0,失败返回-1,如果网络异常可能引发阻塞
int clientsock = accept(int serversocket,struct sockaddr* clientaddr,socklen_t* addrlen); //阻塞等待并建立连接函数(完成三次握手),连接成功后立即返回
//成功返回sock,失败返回-1,如果网络异常可能引发阻塞
send(int sockfd,char* msg,int len,MSG_NOSIGNAL/*写忽略信号*/); //向目标发送网络信息
recv(int sockfd,char* buffer,int size,MSG_DONTWAIT/*非阻塞读*/); //读取接收网络信息
TCP连接方式:keep-alive长链接,close短链接
//TcpServer.h
#include<mysock.h>
#define SHUTDOWN 1
/*支持 tcp连接 及连接反馈的模型*/
int main(){
//close 循环持续连接
int server_sock,client_sock;
struct sockaddr_in addrClient;
socklen_t addrlen;
server_sock=net_initializer();
printf("Test tcp server version 1.0\n");
char client_ip[16];
while(SHUTDOWN){
addrlen=sizeof(addrClient);
client_sock=ACCEPT(server_sock,(struct sockaddr*)&addrClient,&addrlen);
bzero(client_ip,16);
inet_ntop(AF_INET,&addrClient.sin_addr.s_addr,client_ip,16);//大端序转字符串
printf("client port %d,client ip %s\n",ntohs(addrClient.sin_port),client_ip);
first_response(client_sock,client_ip);
business(client_sock);//读取请求,处理请求,反馈响应
close(client_sock);
}
close(server_sock);
printf("server tis done\n");
return 0;
}
//TcpClient.h
#include<mysock.h>
int main(){
//创建套接字
int server_sock=SOCKET(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//服务器套接字信息
struct sockaddr_in addrServer;
bzero(&addrServer,sizeof(addrServer));
addrServer.sin_family=AF_INET;
addrServer.sin_port=htons(8080);
addrServer.sin_addr.s_addr=inet_addr("82.157.31.74");
CONNECT(server_sock,(struct sockaddr*)&addrServer,sizeof(addrServer));
int nRecvNum=0;
int nSendNum=0;
char recvBuf[1024]="";
char sendBuf[1024]="";
nRecvNum=RECV(server_sock,recvBuf,sizeof(recvBuf),0);
printf("server:%s\n",recvBuf);
fgets(sendBuf,sizeof(sendBuf),stdin);
nSendNum=SEND(server_sock,sendBuf,sizeof(sendBuf),0);
nRecvNum=RECV(server_sock,recvBuf,sizeof(recvBuf),0);
printf("server:%s\n",recvBuf);
close(server_sock);
return 0;
}
套接字函数的包裹,网络功能的包裹:在系统函数的基础上,拓展函数的功能,在函数的基础上包裹一层功能更丰富的函数
//myscok.h
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<errno.h>
#include<ctype.h>
#include<time.h>
int SOCKET(int domain,int type,int protocol);
int BIND(int sockfd,const struct sockaddr* addr,socklen_t addrlen);
int LISTEN(int sockfd,int backlog);
int CONNECT(int sockfd,const struct sockaddr* addr,socklen_t addrlen);
int ACCEPT(int sockfd,struct sockaddr* addr,socklen_t* addrlen);
ssize_t RECV(int sockfd,void* buf,size_t len,int flags);
ssize_t SEND(int sockfd,const void* buf,size_t len,int flags);
int net_initializer();
void first_response(int sock,char* cip);
//业务处理模块
void business(int client_sock);
//mysock.c
#include<mysock.h>
int SOCKET(int domain,int type,int protocol){
int sock;
if((sock=socket(domain,type,protocol))==-1){
perror("socket create failed");
return -1;
}
return sock;
}
int BIND(int sockfd,const struct sockaddr* addr,socklen_t addrlen){
if((bind(sockfd,addr,addrlen))==-1){
perror("bind call failed");
return -1;
}
return 0;
}
int LISTEN(int sockfd,int backlog){
if((listen(sockfd,backlog))==-1){
perror("listen call failed");
return -1;
}
return 0;
}
int CONNECT(int sockfd,const struct sockaddr* addr,socklen_t addrlen){
if((connect(sockfd,addr,addrlen))==-1){
perror("connect call failed");
return -1;
}
return 0;
}
int ACCEPT(int sockfd,struct sockaddr* addr,socklen_t* addrlen){
int sock;
if((sock=accept(sockfd,addr,addrlen))==-1){
perror("accept call failed");
return -1;
}
return sock;
}
ssize_t RECV(int sockfd,void* buf,size_t len,int flags){
ssize_t size;
if((size=recv(sockfd,buf,len,flags))==-1){
if(errno==EAGAIN){
printf("recv nonblock return\n");
}
else{
perror("recv call failed");
}
return -1;
}
return size;
}
ssize_t SEND(int sockfd,const void* buf,size_t len,int flags){
ssize_t size;
if((size=send(sockfd,buf,len,flags))==-1){
perror("send call failed");
return -1;
}
return size;
}
int net_initializer(){
//套接字信息
struct sockaddr_in addrServer;
bzero(&addrServer,sizeof(addrServer));
addrServer.sin_family=AF_INET;
addrServer.sin_port=htons(8080);
addrServer.sin_addr.s_addr=htonl(INADDR_ANY);
//创建套接字
int server_sock;
server_sock=SOCKET(AF_INET,SOCK_STREAM,0);
BIND(server_sock,(struct sockaddr*)&addrServer,sizeof(addrServer));
LISTEN(server_sock,128);
return server_sock;
}
void first_response(int sock,char* cip){
char response[1024];
bzero(response,sizeof(response));
sprintf(response,"hi, %s wellcome test TCP server.\n",cip);
SEND(sock,response,strlen(response),0);
}
void business(int client_sock){
//读取一次客户端请求,处理后,立刻断开
ssize_t recv_size;
char recv_buffer[1024];
bzero(recv_buffer,sizeof(recv_buffer));
recv_size=RECV(client_sock,recv_buffer,sizeof(recv_buffer),0);
if((strcmp(recv_buffer,"time\n")==0) || (strcmp(recv_buffer,"time")==0)){
//响应系统时间
time_t tp;
char tbuf[1024];
bzero(tbuf,sizeof(tbuf));
ctime_r(&tp,tbuf);
SEND(client_sock,tbuf,strlen(tbuf),0);
printf("server,response time success.\n");
close(client_sock);
}
else{
//数据处理,大小写转换 toupper()
int cnt=0;
while(cnt<recv_size){
recv_buffer[cnt]=toupper(recv_buffer[cnt]);
cnt++;
}
SEND(client_sock,recv_buffer,recv_size,0);
printf("server,response data sucess.\n");
close(client_sock);
}
}
二、业务
简易业务:例子
客户端向服务端发送time关键字,服务器接收后,向客户端返回系统时间
简单数据处理,客户端向服务端发送字符串,服务端完成大小写转换,并回复
客户端向服务端发送手机号码,服务端向手机发送短信,并附带4位验证码,后续的验证流程忽略