Linux_C socket server.c clinet.c功能分开写
socklib.h
1 int make_server_socket(int ); 2 int connect_to_server(char* , int ); 3 int process_request(int ); 4 int talk_with_server(int );
socklib.c
1 /* socklib.c 2 * This file contains functions used lots when writing internet 3 * client/ server programs. The two main functions here are: 4 * 5 * int make_server_socket(portnum) returns a server socket 6 * or -1 if error 7 * int make_server_socket_q(portnum, backlog) 8 * 9 * int connect_to_server(char* hostname, int portnum) return a connected socket 10 * or -1 if error 11 */ 12 #include <stdio.h> 13 #include <unistd.h> 14 #include <sys/socket.h> 15 #include <sys/types.h> 16 #include <netinet/in.h> 17 #include <netdb.h> 18 #include <time.h> 19 #include <strings.h> 20 21 #define HOSTLEN 256 22 #define BACKLOG 1 23 #define oops(msg) {perror(msg); exit(1);} 24 25 int make_server_socket_q(int , int); 26 27 int make_server_socket(int portnum) { 28 return make_server_socket_q(portnum, BACKLOG); 29 } 30 31 int make_server_socket_q(int portnum, int backlog) { 32 struct sockaddr_in saddr; 33 struct hostent *hp; 34 char hostname[HOSTLEN]; 35 int sock_id; 36 sock_id=socket(PF_INET, SOCK_STREAM, 0); 37 if(sock_id == -1) 38 return -1; 39 /*bulid socket bind*/ 40 bzero((void*)&saddr, sizeof(saddr)); 41 gethostname(hostname, HOSTLEN); 42 hp=gethostbyname(hostname); 43 44 bcopy((void*)hp->h_addr, (void*)&saddr.sin_addr, hp->h_length); 45 saddr.sin_port=htons(portnum); 46 saddr.sin_family=AF_INET; 47 if(bind(sock_id, (struct sockaddr*)&saddr, sizeof(saddr))!=0) 48 return -1; 49 50 /** arrange for incoming calls **/ 51 if(listen(sock_id, backlog)!=0) 52 return -1; 53 return sock_id; 54 } 55 56 int connect_to_server(char *host, int portnum) { 57 int sock; 58 struct sockaddr_in servadd; 59 struct hostent *hp; 60 /** Step1: get a socket**/ 61 sock=socket(AF_INET, SOCK_STREAM, 0); 62 if(sock==-1) 63 return -1; 64 /**Step2: connect to server**/ 65 bzero((void*)&servadd, sizeof(servadd)); 66 hp=gethostbyname(host); 67 if(hp==NULL) 68 return -1; 69 bcopy(hp->h_addr, (struct sockaddr*)&servadd.sin_addr, hp->h_length); 70 servadd.sin_port=htons(portnum); 71 servadd.sin_family=AF_INET; 72 if(connect(sock, (struct sockaddr* )&servadd, sizeof(servadd))!=0) 73 return -1; 74 return sock; 75 } 76 77 int talk_with_server(int fd) { 78 char buf[BUFSIZ]; 79 int len; 80 len = read(fd, buf, BUFSIZ); 81 if(len==-1) 82 return -1; 83 if(write(1, buf, len)!=len) 84 return -1; 85 return 0; 86 } 87 /* 88 int process_request(int fd) { 89 time_t thetime; 90 char *cp; 91 time(&thetime); 92 cp=ctime(&thetime); 93 if(write(fd, cp, strlen(cp)) != strlen(cp)) 94 return -1; 95 return 0; 96 } 97 */ 98 int process_request(int fd) { 99 int pid = fork(); 100 switch(pid) { 101 case -1: return -1; 102 case 0: dup2(fd, 1); 103 close(fd); 104 execl("/bin/date", "date", NULL); 105 oops("execl"); 106 default:wait(NULL); 107 } 108 }
/* timeclnt_new.c - a new client for use the socklib.c */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <time.h> #include <strings.h> #include "socklib.h" #define oops(msg) {perror(msg); exit(1);} int main(int argc, char* argv[]){ int sock_id; sock_id=connect_to_server(argv[1],atoi(argv[2])); if(sock_id==-1) oops("connect_to_server"); if(talk_with_server(sock_id) == -1) oops("talk_to_server"); close(sock_id); return 0; }
1 #include <stdio.h> 2 #include <unistd.h> 3 #include <sys/types.h> 4 #include <sys/socket.h> 5 #include <netinet/in.h> 6 #include <netdb.h> 7 #include <time.h> 8 #include <strings.h> 9 #include "socklib.h" 10 11 #define oops(msg){ perror(msg); exit(1);} 12 #define PORTNUM 13000 13 int main(void ) { 14 int sockid, fd; 15 sockid=make_server_socket(PORTNUM); 16 if(sockid==-1) 17 oops("make_server_socket"); 18 while(1){ 19 fd=accept(sockid, NULL, NULL); 20 if(fd==-1) 21 break; 22 process_request(fd); 23 close(fd); 24 } 25 return 0; 26 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。