文章目录
- 1 TCP和UDP的区别
- 2 UDP
- API
- 流程
- 服务端流程
- 客户端流程
- 代码
- 服务端
- 客户端
- 3 本地socket通信
- 服务端
- 客户端
- 客户端代码
1 TCP和UDP的区别
2 UDP
API
流程
服务端流程
客户端流程
代码
服务端
#include<sys/socket.h>
#include<stdio.h>
#include<arpa/inet.h>
#include <unistd.h>
#include<sys/time.h>
#include<sys/types.h>
#include<sys/select.h>
#include<sys/epoll.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>int main()
{// 创建套接字int cfd = socket(AF_INET,SOCK_DGRAM,0);if(cfd<0){perror("socket error");return -1;}// 绑定struct sockaddr_in serv;struct sockaddr_in client;bzero(&serv, sizeof(serv));serv.sin_family = AF_INET;serv.sin_port = htons(8888);serv.sin_addr.s_addr = htonl(INADDR_ANY);bind(cfd,(struct sockaddr*)&serv,sizeof(serv));// while中收发数据int n;socklen_t len;char buf[1024] = "";while(1){memset(buf,0x00,sizeof(buf));len = sizeof(client);n = recvfrom(cfd, buf, sizeof(buf),0,(struct sockaddr*)&client, &len);// 将大写转换为小写for(int i=0;i<n;i++){buf[i] = toupper(buf[i]);}sendto(cfd,buf,n,0, (struct sockaddr*) &client, len);}close(cfd);return 0;
}
客户端
#include<sys/socket.h>
#include<stdio.h>
#include<arpa/inet.h>
#include <unistd.h>
#include<sys/time.h>
#include<sys/types.h>
#include<sys/select.h>
#include<sys/epoll.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>int main()
{// 创建套接字int cfd = socket(AF_INET,SOCK_DGRAM,0);if(cfd<0){perror("socket error");return -1;}// while中收发数据int n;socklen_t len;char buf[1024] = "";struct sockaddr_in serv;serv.sin_family = AF_INET;serv.sin_port = htons(8888);inet_pton(AF_INET,"127.0.0.1",&serv.sin_addr.s_addr);while(1){// 读标准输入数据memset(buf, 0x00, sizeof(buf));n = read(STDIN_FILENO,buf,sizeof(buf));// 发送数据sendto(cfd,buf,n,0, (struct sockaddr*) &serv, sizeof(serv));memset(buf,0x00,sizeof(buf));// 接受数据n = recvfrom(cfd, buf, sizeof(buf),0,NULL, NULL);printf("n = [%d], buf = [%s]\n",n,buf);}close(cfd);return 0;
}
3 本地socket通信
创建成功后,会在内核中创建缓冲区
流程
服务端
#include<sys/socket.h>
#include<stdio.h>
#include<arpa/inet.h>
#include <unistd.h>
#include<sys/types.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include <sys/un.h>int main()
{// 创建socketint lfd = socket(AF_UNIX, SOCK_STREAM, 0);if(lfd<0){perror("socket error");return 1;}// 删除 serv.sock文件unlink("serv.sock");// 绑定struct sockaddr_un serv;serv.sun_family = AF_UNIX;strcpy(serv.sun_path, "./serv.sock");int ret = bind(lfd, (struct sockaddr*)&serv,sizeof(serv));if(ret < 0){perror("bind");return 1;}// 监听listen(lfd,128);// 接受新的客户端连接struct sockaddr_un client;socklen_t len = sizeof(client);int cfd = accept(lfd, (struct sockaddr*)&client, &len);if(cfd < 0){perror("accpet error");return -1;}int n;int i;char buf[1024];while(1){memset(buf, 0x00, sizeof(buf));// 读数据n = read(cfd, buf, sizeof(buf));if(n<=0){printf("read error or client clsoe");break;}// 发送数据for(i = 0;i<n;i++){buf[i] = toupper(buf[i]);}write(cfd,buf,n);}close(cfd);close(lfd);return 0;}
客户端
客户端代码
#include<sys/socket.h>
#include<stdio.h>
#include<arpa/inet.h>
#include <unistd.h>
#include<sys/types.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include <sys/un.h>int main()
{// 创建socketint cfd = socket(AF_UNIX, SOCK_STREAM, 0);if(cfd<0){perror("socket error");return 1;}// 连接服务端struct sockaddr_un serv;serv.sun_family = AF_UNIX;strcpy(serv.sun_path, "./serv.sock");connect(cfd, (struct sockaddr*)&serv, sizeof(serv));int n;int i;char buf[1024];while(1){memset(buf, 0x00, sizeof(buf));n = read(STDIN_FILENO, buf, sizeof(buf));// 发送数据write(cfd,buf,n);// 读数据memset(buf, 0x00, sizeof(buf));n = read(cfd, buf, sizeof(buf));if(n<=0){printf("read error or client clsoe");break;}}close(cfd);return 0;}