作业:
1、流式域套接字敲一遍。
/*******************************************************/ //=> File Name: local_tcp.c //=> Author: Water //=> Mail: 1249496568@qq.com //=> Created Time: 2024年12月30日 星期一 16时46分28秒 /*******************************************************/#include<myhead.h> int main(int argc, const char *argv[]) {//1、创建本地域套接字文件int oldfd = socket(AF_UNIX,SOCK_STREAM,0);if(oldfd == -1){perror("socket");return -1;}//2、绑定本地套接字文件(不存在的文件)if(access("./server",F_OK) == 0){if(unlink("./server") == -1){perror("unlink");return -1;}}struct sockaddr_un server;server.sun_family = AF_UNIX;strcpy(server.sun_path,"server");if(bind(oldfd,(struct sockaddr *)&server,sizeof(server)) == -1){perror("bind");return -1;}//3、监听if(listen(oldfd,10) == -1){perror("listen");return -1;}//4、接收客户端请求struct sockaddr_un client;int client_len = sizeof(client);int newfd = accept(oldfd,(struct sockaddr *)&client,&client_len);if(newfd == -1){perror("accept");return -1;}printf("%s发来连接请求\n",client.sun_path);char buff[1024];while(1){recv(newfd,buff,sizeof(buff),0);printf("%s\n",buff);strcat(buff,"客户端已收到\n");send(newfd,buff,sizeof(buff),0);}return 0; }
/*******************************************************/ //=> File Name: send_client.c //=> Author: Water //=> Mail: 1249496568@qq.com //=> Created Time: 2024年12月30日 星期一 17时17分22秒 /*******************************************************/#include<myhead.h> int main(int argc, const char *argv[]) {//1、创建本地域套接字文件int oldfd = socket(AF_UNIX,SOCK_STREAM,0);if(oldfd == -1){perror("socket");return -1;}//2、绑定本地套接字文件(不存在的文件)if(access("./client",F_OK) == 0){if(unlink("./client") == -1){perror("unlink");return -1;}}struct sockaddr_un client;client.sun_family = AF_UNIX;strcpy(client.sun_path,"./client");if(bind(oldfd,(struct sockaddr *)&client,sizeof(client)) == -1){perror("bind");return -1;}//3、连接服务器struct sockaddr_un server;server.sun_family = AF_UNIX;strcpy(server.sun_path,"server");if(connect(oldfd,(struct sockaddr *)&server,sizeof(server)) == -1){perror("connect");return -1;}//4、接收数据char buff[1024];while(1){fgets(buff,sizeof(buff),stdin);send(oldfd,buff,sizeof(buff),0);printf("发送成功\n");recv(oldfd,buff,sizeof(buff),0);printf("接收服务器消息:%s\n",buff);bzero(buff,sizeof(buff));}close(oldfd);return 0; }
2.、UDP的客户端服务器实现一遍。
/*******************************************************/ //=> File Name: send.c //=> Author: Water //=> Mail: 1249496568@qq.com //=> Created Time: 2024年12月30日 星期一 09时38分59秒 /*******************************************************/#include<myhead.h> #define PORT 6666 #define IP "192.168.124.255" int main(int argc, const char *argv[]) {//1、创建套接字int oldfd = socket(AF_INET,SOCK_DGRAM,0);if(oldfd == -1){perror("socket");return -1;}//2、设置允许广播int k = 1;if(setsockopt(oldfd,SOL_SOCKET,SO_BROADCAST,&k,sizeof(k)) == -1){perror("setsockopt");return -1;}printf("允许广播设置成功\n");//3、填充广播地址信息结构体struct sockaddr_in broadcast = {.sin_family = AF_INET,.sin_port = htons(PORT),.sin_addr.s_addr = inet_addr(IP)};char buff[1024];while(1){fgets(buff,sizeof(buff),stdin);sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&broadcast,sizeof(broadcast));if(strcmp(buff,"quit") == 0){printf("发送端退出\n");break;}}close(oldfd);return 0; }
/*******************************************************/ //=> File Name: recv.c //=> Author: Water //=> Mail: 1249496568@qq.com //=> Created Time: 2024年12月30日 星期一 10时09分33秒 /*******************************************************/#include<myhead.h> #define IP "192.168.124.255" #define PORT 6666 int main(int argc, const char *argv[]) {//1、创建套接字int oldfd = socket(AF_INET,SOCK_DGRAM,0);if(oldfd == -1){perror("socket");return -1;}//2、填充广播地址信息结构体struct sockaddr_in send = {.sin_family = AF_INET,.sin_port = htons(PORT),.sin_addr.s_addr = inet_addr(IP)};if(bind(oldfd,(struct sockaddr *)&send,sizeof(send)) == -1){perror("bind");return -1;}//3、接受消息char buff[1024];while(1){recvfrom(oldfd,buff,sizeof(buff),0,NULL,NULL);printf("%s",buff);}return 0; }