伺服器: 客戶端: 參考: 《UNIX網路編程 - 捲1:套接字聯網API》 ...
伺服器:
1 /* 2 *tcp_server.c 3 */ 4 5 #include <stdio.h> 6 #include <sys/socket.h> 7 #include <netinet/in.h> 8 #include <arpa/inet.h> 9 #include <string.h> 10 #include <errno.h> 11 #include <stdlib.h> 12 #include <signal.h> 13 14 #define SER_PORT 8000 15 #define MAX_LINE 1024 16 17 void str_echo(int sockfd); 18 void sig_chld(int signo); 19 20 int main(int argc, char **argv) 21 { 22 int listenfd, connfd; 23 struct sockaddr_in servaddr, cliaddr; 24 socklen_t cliaddr_len; 25 char buff[MAX_LINE]; 26 pid_t childpid; 27 28 listenfd = socket(AF_INET, SOCK_STREAM, 0); 29 30 bzero(&servaddr, sizeof(servaddr)); 31 servaddr.sin_family = AF_INET; 32 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 33 servaddr.sin_port = htons(SER_PORT); 34 35 bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); 36 37 listen(listenfd, 20); 38 signal(SIGCHLD, sig_chld); 39 40 while(1) 41 { 42 cliaddr_len = sizeof(cliaddr); 43 connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len); 44 printf("receive from %s at port %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)), ntohs(cliaddr.sin_port)); 45 46 if ((childpid = fork()) == 0) /* child process */ 47 { 48 printf("child process pid = %d\n", (int)childpid); 49 close(listenfd); 50 str_echo(connfd); 51 printf("child process terminated\n"); 52 exit(0); 53 } 54 55 close(connfd); 56 } 57 58 return 0; 59 } 60 61 void str_echo(int sockfd) 62 { 63 ssize_t n; 64 char buf[MAX_LINE] = {0}; 65 66 AGAIN: 67 while ((n = read(sockfd, buf, MAX_LINE)) > 0) 68 { 69 printf("%s\n", buf); 70 write(sockfd, buf, n); 71 bzero(buf, sizeof(buf)); 72 } 73 74 if (n < 0 && errno == EINTR) 75 { 76 goto AGAIN; 77 } 78 else if (n < 0) 79 { 80 printf("read error\n"); 81 } 82 else 83 { 84 printf("str_echo: read EOF\n"); 85 } 86 87 } 88 89 void sig_chld(int signo) 90 { 91 pid_t pid; 92 int stat; 93 94 pid = wait(&stat); 95 printf("child %d terminated\n", pid); 96 97 return; 98 }
客戶端:
1 /* 2 *tcp_client.c 3 */ 4 5 #include <stdio.h> 6 #include <sys/socket.h> 7 #include <netinet/in.h> 8 #include <arpa/inet.h> 9 #include <errno.h> 10 #include <string.h> 11 12 #define SER_PORT 8000 13 #define MAX_LINE 1024 14 15 void str_cli(FILE *fp, int sockfd); 16 17 int main(int argc, char **argv) 18 { 19 int sockfd; 20 struct sockaddr_in servaddr; 21 22 sockfd = socket(AF_INET, SOCK_STREAM, 0); 23 24 bzero(&servaddr, sizeof(servaddr)); 25 servaddr.sin_family = AF_INET; 26 inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr); 27 servaddr.sin_port = htons(SER_PORT); 28 29 if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) 30 { 31 printf("connect error:%s\n", strerror(errno)); 32 return 0; 33 } 34 35 str_cli(stdin, sockfd); 36 37 return 0; 38 } 39 40 void str_cli(FILE *fp, int sockfd) 41 { 42 char sendline[MAX_LINE] = {0}; 43 char recvline[MAX_LINE] = {0}; 44 45 46 while (fgets(sendline, MAX_LINE, fp) != NULL) 47 { 48 write(sockfd, sendline, strlen(sendline)); 49 50 if (read(sockfd, recvline, MAX_LINE) == 0) 51 { 52 printf("server terminated\n"); 53 break; 54 } 55 fputs(recvline, stdout); 56 57 bzero(sendline, sizeof(sendline)); 58 bzero(recvline, sizeof(recvline)); 59 } 60 }
參考:
《UNIX網路編程 - 捲1:套接字聯網API》