从github上找了3段程序,借此来学习socket编程,从一开始看不懂,到现在能看出来个轮廓,还是有进步的。
#注意,本文所示程序除注释外均非本人所写。
1 #ifndef SERVER_H 2 #define SERVER_H // line 3 to 11 : include lib 3 #include <unistd.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <unistd.h> 7 #include <string.h> 8 #include <sys/types.h> 9 #include <sys/socket.h> 10 #include <netinet/in.h> 11 #include <netdb.h> 12 13 struct Server; //声明结构体 14 15 struct Server * new_server(char *host, char *port); //声明函数 16 17 //主机连接函数 18 int host_connect(struct Server *this); 19 //信息接收 20 char *get_message(struct Server *this); 21 //信息发送函数 22 void send_message(struct Server *this, const char *msg); 23 // 错误处理函数 24 void error(struct Server *this, const char *msg); 25 26 struct Server { 27 int port; //端口 28 char *host; //主机地址 29 30 int sockfd; //描述符 31 char buffer[256]; //应该是传送的信息 32 33 int (*host_connect)(struct Server *this);//函数指针 34 char *(*get_message)(struct Server *this); 35 void (*send_message)(struct Server *this, const char *msg); 36 void (*error)(struct Server *this, const char *msg); 37 38 struct sockaddr_in server_address;; 39 struct hostent *server; 40 }; 41 42 #endif
1 #include "server.h" 2 3 //函数名字为new_server的返回值是指向结构体Server的指针 4 struct Server * new_server(char *host, char *port) { 5 struct Server *server = malloc(sizeof(struct Server)); 6 7 // line 8~13 赋值 8 server->host_connect = &host_connect; 9 server->get_message = &get_message; 10 server->send_message = &send_message; 11 server->error = &error; 12 server->host = host; 13 server->port = atoi(port); 14 15 return server; 16 } 17 18 int host_connect(struct Server *this) { 19 // int socket(int domain,int type,int protocol); 20 // 域 类型 协议 21 //创建套接字 22 // AF_INET IPv4因特网域 23 // SOCK_STREAM 有序,可靠,双向到面向连接字节流 24 // AF_INET + SOCK_STREAM -----> tcp协议 25 this->sockfd = socket(AF_INET, SOCK_STREAM, 0); 26 if (this->sockfd < 0) { 27 return 0; 28 } 29 30 this->server = gethostbyname(this->host); 31 if (this->server == NULL) { 32 return 0; 33 } 34 35 //bzero函数功能,置字节字符串s到前n个字节为零且包括' ' 36 bzero((char *) &this->server_address, sizeof(this->server_address)); 37 this->server_address.sin_family = AF_INET; 38 39 //bcopy函数功能应该和bzero差不多 40 //extern void bcopy(const void *src,void *dest,int n); 41 //将字符串src到前n个字节复制到dest中 42 bcopy((char *)this->server->h_addr, 43 (char *)&this->server_address.sin_addr.s_addr, 44 this->server->h_length); 45 46 this->server_address.sin_port = htons(this->port); 47 48 if (connect(this->sockfd, 49 (struct sockaddr *) &this->server_address, 50 sizeof(this->server_address)) < 0) { 51 this->error(this, "Could not connect to host!"); 52 } 53 54 return 1; 55 } 56 57 char * get_message(struct Server *this) { 58 bzero(this->buffer, 256); 59 60 if(read(this->sockfd, this->buffer, 255) < 0) { 61 this->error(this, "Error reading from socket"); 62 } 63 64 return this->buffer; 65 } 66 67 void send_message(struct Server *this, const char *msg) { 68 bzero(this->buffer, 256); 69 70 if(write(this->sockfd, msg, strlen(msg)) < 0) { 71 this->error(this, "Error writing to socket"); 72 } 73 } 74 75 void error(struct Server *this, const char *msg) { 76 perror(msg); 77 close(this->sockfd); 78 exit(-1); 79 }
1 #include "server.h" 2 3 int main(int argc, char *argv[]) { 4 if (argc < 2) { 5 puts("Usage: ./chat [ip address] [port]"); 6 return -1; 7 } 8 9 char *line; 10 size_t length = 80; 11 12 struct Server *server = new_server(argv[1], argv[2]); 13 server->host_connect(server); 14 15 printf("## %s ", server->get_message(server)); 16 17 while (1) { 18 printf("> "); 19 fflush(stdout); 20 21 if (getline(&line, &length, stdin) < 0) { 22 server->error(server, "Error while getting input!"); 23 } 24 25 if (strcmp(line, "/quit ") == 0) { 26 puts("Goodbye!"); 27 break; 28 } 29 30 server->send_message(server, line); 31 printf("## %s ", server->get_message(server)); 32 } 33 34 close(server->sockfd); 35 return 0; 36 }