文章目录
- C语言网络编程
- socket
- 主机与网络字节序转换
- inet_addr、inet_aton(ip转换)
- inet_ntoa 网络字节序转换为IP字符串
- 端口转换为网络字节序
- 网络字节序转换为端口
- atoi (字符串转换为整数)
- UDP通信流程
- UDP多进程并发服务器
- 服务端
- 客户端
- TCP通信流程
- 客户端
- 服务端
- TCP粘包
- 并发服务器
C语言网络编程
socket
Linux提供的socket
socket套接字类型
1、流式套接字(TCP)
2、数据报套接字(UDP)
3、原始套接字
网络数据流在cpu中以不同的方式存储,有小端和大端两种方式(小端与人的读写同向,大端与人的读逆向)
网络传输时先判断是否为小端,若为小端则进行转换。可以用共用体判断是大端还是小端存储。
主机与网络字节序转换
inet_addr、inet_aton(ip转换)
inet_ntoa 网络字节序转换为IP字符串
端口转换为网络字节序
网络字节序转换为端口
atoi (字符串转换为整数)
UDP通信流程
发送流程
1、创建socket函数
int socket(int domain, int type, int protocol);
udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
2、sendto,发送函数
发送端代码
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
void send_data(int sockfd,struct sockaddr_in *addr,int len){int n=0;char buf[1024]={0};while(1){putchar('>');memset(buf,0,sizeof(buf));fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1] = '\0'; // ’\n‘ ==>'\0'n = sendto(sockfd,buf,strlen(buf),0,(struct sockaddr *)addr,len);//发送写buf实际长度,收时写buf最大长度if(n < 0){perror("[ERROR] sendto():");exit(EXIT_FAILURE);}if(strncmp(buf,"quit",4) == 0){printf("END SEND\n");break;}printf("\033[43msend message:\033[0m %s \n",buf);}return ;
}int main(int argc,char *argv[]){int sockfd;struct sockaddr_in peer_addr;int len = sizeof(peer_addr);if(argc != 3){fprintf(stderr,"Usage : %s ip port !\n",argv[0]);exit(EXIT_FAILURE);}//1、通过socket创建文件描述符sockfd = socket(AF_INET,SOCK_DGRAM,0);if(sockfd == -1){perror("[ERROR] socket():");exit(EXIT_FAILURE);}//2、填充服务器ip和端口号memset(&peer_addr,0,sizeof(peer_addr)); //初始化结构体peer_addr.sin_family=AF_INET;peer_addr.sin_port=htons(atoi(argv[2]));peer_addr.sin_addr.s_addr = inet_addr(argv[1]);//3、发送数据send_data(sockfd,&peer_addr,len);//4、关闭文件描述符close(sockfd);return 0;
}
接收流程
1、创建socket
2、绑定ip和端口到socket bind
3、接收数据 recvfrom
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
void recv_data(int sockfd){//接收数据函数int n = 0;char buf[1024] = {0};struct sockaddr_in client_addr;int len = sizeof(client_addr);while(1){memset(buf,0,sizeof(buf));n = recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&client_addr,&len); //接收发送方IP port 以及数据if(n < 0){perror("[error] recvfrom():");exit(EXIT_FAILURE);}printf("===================================================\n");printf("\033[34m Recv from IP = %s \033[0m\n",inet_ntoa(client_addr.sin_addr));printf("\033[34m Recv from PORT = %d \033[0m\n",ntohs(client_addr.sin_port));printf("Recv %d bytes:\033[35m %s\033[0m\n",n,buf);if(strncmp(buf,"quit",4) == 0){printf("\033[31mEND RECV\033[0m\n");break;}}}int main(int argc,char *argv[]){//参数判断if(argc != 3){printf("\033[31m %s 参数错误: ip port\033[0m\n");exit(EXIT_FAILURE);}int sockfd;struct sockaddr_in my_addr;int len = sizeof(my_addr);//1、创建socketsockfd = socket(AF_INET,SOCK_DGRAM,0);if(sockfd < 0){perror("[ERROR] socket():");exit(EXIT_FAILURE);}//2、填充ip和端口memset(&my_addr,0,sizeof(my_addr)); //初始化结构体my_addr.sin_family = AF_INET;my_addr.sin_port = htons(atoi(argv[2]));my_addr.sin_addr.s_addr = inet_addr(argv[1]);//3、绑定端口if(bind(sockfd,(struct sockaddr *)&my_addr,len)<0){perror("[ERROR] bind():");exit(EXIT_FAILURE);} printf("\033[35m wait recv from port %s \033[0m \n",argv[2]);//4、接收数据recv_data(sockfd);//关闭描述符close(sockfd);return 0;
}
UDP多进程并发服务器
服务端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>/*udp 多进程服务器端*/
/*
1、创建主进程,用与接收客户端请求
2、接收客户端请求后,分配新的socket以及端口用于和客户端连接*/void send_client(struct sockaddr_in *seradd,struct sockaddr_in *newcadd,struct sockaddr_in *newclient);int main(int argc,char *argv[]){/*变量定义*/int sfd; //主进程socketint len;//记录结构体长度int rev;//接收函数返回值struct sockaddr_in seradd;//填充服务端信息struct sockaddr_in cliadd;//接收客户端信息char buf[1024] = {0}; //接收数据缓冲区pid_t cpid; //接收子进程号/*代码*/len = sizeof(seradd);//创建主进程//1、创建socketsfd = socket(AF_INET,SOCK_DGRAM,0);if(sfd == -1){perror("error in socket():");exit(EXIT_FAILURE);}//2、填充信息到结构体memset(&seradd,0,len);seradd.sin_family = AF_INET;seradd.sin_port = htons(atoi(argv[2]));seradd.sin_addr.s_addr = inet_addr(argv[1]);//3、绑定ip和端口至socketrev = bind(sfd,(struct sockaddr *)&seradd,len);if(rev == -1){ perror("error in bind():");exit(EXIT_FAILURE);}//接收数据while(1){memset(buf,0,sizeof(buf));memset(&cliadd,0,len);rev = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cliadd,&len);if(rev == -1){perror("error in recvfrom():");exit(EXIT_FAILURE);}//输出客户端信息printf("The connect from:\033[32m %s %d\033[0m\n",inet_ntoa(cliadd.sin_addr),ntohs(cliadd.sin_port));printf("data : \033[31m %s \033[0m\n",buf);//创建子进程cpid = fork();if(cpid == -1){perror("error in fork():");exit(EXIT_FAILURE);}else if(cpid == 0){struct sockaddr_in new_seradd;//创建新的端口等struct sockaddr_in new_client;//创建新的端口等memset(&new_seradd,0,len);memset(&new_client,0,len);new_client = cliadd;send_client(&seradd,&new_seradd,&new_client);exit(EXIT_SUCCESS);}}return 0;
}/*函数*/void send_client(struct sockaddr_in *seradd,struct sockaddr_in *newcadd,struct sockaddr_in *newclient){struct sockaddr_in * new_ser; //接收目的ip,并分配新portstruct sockaddr_in * client; //接收目的ip,并分配新portint sfd;int len;int rev;char buf[1024]={0};len = sizeof(struct sockaddr_in);new_ser = newcadd;client = newclient;sfd = socket(AF_INET,SOCK_DGRAM,0);if(sfd == -1){perror("error in socket():");exit(EXIT_FAILURE);}//输出客户端信息new_ser->sin_family = seradd->sin_family;new_ser->sin_port = htons(0);new_ser->sin_addr.s_addr = seradd->sin_addr.s_addr;rev = bind(sfd,(struct sockaddr *)new_ser,len);if(rev == -1){perror("error in rev():");exit(EXIT_FAILURE);}printf("以新端口发送数据\n"); rev = sendto(sfd,"SEND NEW PORT",sizeof("SEND NEW PORT"),0,(struct sockaddr*)client,len);if(rev == -1){perror("error in sendto():");exit(EXIT_FAILURE);}printf("以新端口接收数据\n"); while(1){memset(buf,0,sizeof(buf));rev = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)new_ser,&len);if(rev == -1){perror("error in recvfrom():");exit(EXIT_FAILURE);}printf("data:\033[32m%s\n\033[0m",buf);}close(sfd);return ;}
客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>/*udp 多进程客户端*/
/*
1、创建socket,并发送一条信息
2、接收打一条信息 (struct sockaddr_in.sin_port 新更换端口)*/int main(int argc,char *argv[]){/*变量定义*/int sfd;int len;int rev;struct sockaddr_in seradd;char buf[1024] = {0};/*代码*/len = sizeof(seradd);//创建主进程//1、创建socketsfd = socket(AF_INET,SOCK_DGRAM,0);if(sfd == -1){perror("error in socket():");exit(EXIT_FAILURE);}//2、填充信息到结构体memset(&seradd,0,len);seradd.sin_family = AF_INET;seradd.sin_port = htons(atoi(argv[2]));seradd.sin_addr.s_addr = inet_addr(argv[1]);//3、发送信息获取端口号rev = sendto(sfd,"CONNECT",strlen("CONNECT"),0,(struct sockaddr *)&seradd,len);if(rev == -1){perror("error in sendto():");exit(EXIT_FAILURE);}//4、接收新的端口号 memset(buf,0,sizeof(buf));memset(&seradd,0,len);//接收新的端口号至结构体rev = recvfrom(sfd,buf,1024,0,(struct sockaddr *)&seradd,&len);if(rev == -1){perror("error in recvfrom when acquire new port:");exit(EXIT_FAILURE);}printf("接收新端口成功!!!\n");printf("请发送数据:\n");//正常接收发送数据while(1){memset(buf,0,sizeof(buf));fgets(buf,1024,0);
// scanf("%s",buf);
// printf("\n");printf("接收数据:%s\n",buf);rev = sendto(sfd,buf,sizeof(buf),0,(struct sockaddr *)&seradd,len);if(rev == -1){perror("error in sendto():");exit(EXIT_FAILURE);}rev = strncmp("exit",buf,4);printf("rev = %d\n",rev);if(rev == 0){printf("退出!!!!\n");break;}
/* rev = recvfrom(sfd,buf,1024,0,(struct sockaddr *)&seradd),&len);if(rev == -1){perror("error in recvfrom when acquire new port:");exit(EXIT_FAILURE);}
*/ }/*资源关闭*/close(sfd);return 0;
}
TCP通信流程
客户端
流程:
1、创建socket
2、填充结构体 (bzeor() 清零函数)
3、创建连接 connect
4、发送数据 send (udp 为 sendto)
5、接收数据 recv (udp recvfrom) recv返回0,表示断开连接
服务端
1、创建socket
2、绑定ip 与 端口 bind
3、设置套接字为监听状态,建立监听队列 listen
4、与客户端三次握手建立连接 accept,成功创建一个新的套接字
5、使用新的套接字 接收发送消息 send recv
TCP粘包
原因:
解决方法:
1、使用定长的数据包
2、发送时 数据长度+数据(发送不定长数据)