⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.c~

📁 这个资料非常不错
💻 C~
字号:
#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>void process(int fd){char buff[10000];int received;int help,read_bytes;received = 5000;memset ( buff, '.', received );read_bytes = read(fd, buff, received);if (read_bytes < 0) {perror("read");exit(1);}printf("%d bytes have received on socket %d\n", read_bytes, fd);printf("buff=\n%s\n", buff);for(help=0; help<received; help++)if(buff[help] != '0'+help%10) {printf("Error on position %d\n", help); break;}}int main(void){int sockfd ,newsockfd;struct sockaddr_in myaddr, peer;int addrlen1,addrlen2;if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {perror("socket");exit(1);}addrlen1 = sizeof(myaddr);myaddr.sin_family = AF_INET;myaddr.sin_port = htons(10000);myaddr.sin_addr.s_addr = INADDR_ANY;if (bind(sockfd, &myaddr , addrlen1) < 0 ) {perror("bind");exit(1);}if (listen(sockfd, 5 )) {perror("listen");exit(1);}for (;;){addrlen2 = sizeof(peer);newsockfd = accept(sockfd, &peer , &addrlen2);if ( newsockfd < 0) {perror("accept");exit(1);}if (fork() == 0) {close(sockfd);/* process request */printf("connection on socket %d from %s\n", newsockfd, inet_ntoa(peer.sin_addr.s_addr));process(newsockfd);close(newsockfd);exit(0);}close(newsockfd);}} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -