第三章实现了客户端向服务器发送一个字符A,服务器接收该字符后便将其打印出来。
这一章将实现,客户端可以多次向服务器发送一个字符串。就是你在客户端可以输入多次,你输入啥,客户端就给你发送到服务器去。然后服务器再给你打印出来。
这一章的代码在三的代码上修改非常少,第三章是发送一个字符,我们发送一个数组。数组里面存储着你想发送的字符串。
以下便是代码:
1 /*************************************************************************** 2 * * * 3 * * * Filename: client.c 4 * * * 5 * * * Description: Network Client , transfer character or string to server 6 * * * 7 * * * creater: tian 2015.9.9 8 * * * 9 * * * version: 1.0 10 * * * 11 * * * change: 12 * * * 13 * * ***************************************************************************/ 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <string.h> 17 #include <unistd.h> 18 #include <arpa/inet.h> 19 #include <netinet/in.h> 20 21 #define SERV_PORT 8000 22 #define PACK_SIZE 100 23 24 int main(int argc, char **argv) 25 { 26 int Client_socket = 0; 27 int Socket_len = 0; 28 int result = 0; 29 char pack[PACK_SIZE]; 30 struct sockaddr_in server_addr; 31 32 Client_socket = socket(AF_INET, SOCK_STREAM, 0);//create a socket 33 if(-1 == Client_socket) 34 { 35 perror("socket"); 36 exit(1); 37 } 38 39 memset(&server_addr, 0, sizeof(server_addr)); 40 41 server_addr.sin_family = AF_INET; 42 server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 43 server_addr.sin_port = htons(SERV_PORT); 44 45 result = connect(Client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));//c onnect the server 46 if(-1 == result) 47 { 48 perror("connect"); 49 exit(1); 50 } 51 52 while(1) 53 { 54 printf("Please input: "); 55 56 memset(pack, 0 , PACK_SIZE);//init the array pack 57 fgets(pack, PACK_SIZE, stdin); 58 59 result = write(Client_socket, pack, PACK_SIZE);//send out the string 60 if(-1 == result) 61 { 62 perror("write"); 63 } 64 } 65 66 close(Client_socket);//close the socket 67 68 exit(0); 69 }
1 /*************************************************************************** 2 * * * 3 * * * Filename: server.c 4 * * * 5 * * * Description: Network server, accept A from server 6 * * * 7 * * * creater: tian 2015.9.9 8 * * * 9 * * * version: 1.0 10 * * * 11 * * * change: 12 * * * 13 * * ************************************************************************/ 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <string.h> 17 #include <sys/types.h> 18 #include <sys/socket.h> 19 #include <unistd.h> 20 #include <arpa/inet.h> 21 #include <netinet/in.h> 22 23 #define SERV_PORT 8000 24 #define PACK_SIZE 100 25 26 int main(int argc, char **argv) 27 { 28 int Listen_socket = 0,New_socket = 0; 29 int result = 0; 30 int len = 0; 31 char pack[PACK_SIZE]; 32 struct sockaddr_in server_addr,Client_addr; 33 34 Listen_socket = socket(AF_INET, SOCK_STREAM, 0);//create a socket 35 if(-1 == Listen_socket) 36 { 37 perror("socket"); 38 exit(1); 39 } 40 41 memset(&server_addr, 0, sizeof(server_addr)); 42 43 server_addr.sin_family = AF_INET; 44 server_addr.sin_addr.s_addr = htonl(INADDR_ANY); 45 server_addr.sin_port = htons(SERV_PORT); 46 47 result = bind(Listen_socket, (struct sockaddr *)&server_addr, sizeof(server_addr));//Named the socket 48 if(-1 == result) 49 { 50 perror("bind"); 51 exit(1); 52 } 53 54 listen(Listen_socket,20);//momitor the connect fro client 55 56 len = sizeof(Client_addr); 57 New_socket = accept(Listen_socket, (struct sockaddr *)&Client_addr, &len);//receive a connect and create a new socket 58 if(-1 == New_socket) 59 { 60 perror("accept"); 61 exit(1); 62 } 63 64 while(1) 65 { 66 memset(pack, 0, PACK_SZIE);//init the array 67 68 result = read(New_socket, pack, PACK_SIZE); 69 if(-1 == result) 70 { 71 perror("read"); 72 exit(1); 73 } 74 75 printf("receive: "); 76 77 fputs(pack, stdout);//print the received string 78 } 79 80 close(New_socket); 81 close(Listen_socket);//close the socket 82 83 exit(0); 84 } 85
<span style="font-family: Arial, Helvetica, sans-serif;"> </span><span style="font-family: Arial, Helvetica, sans-serif;">从代码中就可以看出,我们在客户端增加了一个数组,用数组存储要发送的字符串。在服务器端也增加了一个数组,接收从客户端发送而来的字符串。</span>
<span style="font-family:Arial, Helvetica, sans-serif;"> 结果:(记得首先开启服务器)</span>
<span style="font-family:Arial, Helvetica, sans-serif;"> <img src="//img-blog.csdn.net/20150920171245961" alt="" /><img src="//img-blog.csdn.net/20150920171304782" alt="" /> </span>
版权声明:本文为博主原创文章,未经博主允许不得转载。