?? server.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 + -