Linux Socket编程实例.doc
Linux Socket编程实例(一个Hello World程序)在Linux下写了个小的socket程序,分为客户端和服务器端,服务端开一个端口(2000),做为一个daemon,等待客户的连接请求.一旦有客户连接,服务器端打印出客户端的IP地址和端口,并且向服务器端发送欢迎信息和时间.下面是服务端的代码(tcpserver.c).由于这只是个简单的程序,所以只用了单线程实现!Cpp代码 1. /* 2. * Tcp Server program, It is a simple example only. 3. * zhengsh 200520602061 2 4. * when client connect to server, send a welcome message and timestamp in server. 5. */ 6. 7. #include <stdio.h> 8. #include <sys/socket.h> 9. #include <unistd.h> 10. #include <sys/types.h> 11. #include <netinet/in.h> 12. #include <stdlib.h> 13. #include <time.h> 14. 15. #define SERVER_PORT 20000 / define the defualt connect port id 16. #define LENGTH_OF_LISTEN_QUEUE 10 /length of listen queue in server 17. #define BUFFER_SIZE 255 18. #define WELCOME_MESSAGE "welcome to connect the server. " 19. 20. 21. int main(int argc, char *argv) 22. 23. int servfd,clifd; 24. struct sockaddr_in servaddr,cliaddr; 25. 26. if (servfd = socket(AF_INET,SOCK_STREAM,0) < 0) 27. 28. printf("create socket error!n"); 29. exit(1); 30. 31. bzero(&servaddr,sizeof(servaddr); 32. servaddr.sin_family = AF_INET; 33. servaddr.sin_port = htons(SERVER_PORT); 34. servaddr.sin_addr.s_addr = htons(INADDR_ANY); 35. 36. if (bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr)<0) 37. 38. printf("bind to port %d failure!n",SERVER_PORT); 39. exit(1); 40. 41. 42. if (listen(servfd,LENGTH_OF_LISTEN_QUEUE) < 0) 43. 44. printf("call listen failure!n"); 45. exit(1); 46. 47. 48. while (1) 49. /server loop will nerver exit unless any body kill the process 50. char bufBUFFER_SIZE; 51. long timestamp; 52. socklen_t length = sizeof(cliaddr); 53. clifd = accept(servfd,(struct sockaddr*)&cliaddr,&length); 54. if (clifd < 0) 55. 56. printf("error comes when call accept!n"); 57. break; 58. 59. strcpy(buf,WELCOME_MESSAGE); 60. /inet_ntop(INET_ADDRSTRLEN,cliaddr.sin_addr,buf,BUFFER_SIZE); 61. 62. printf("from client,IP:%s,Port:%dn",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port); 63. timestamp = time(NULL); 64. strcat(buf,"timestamp in server:"); 65. strcat(buf,ctime(×tamp); 66. send(clifd,buf,BUFFER_SIZE,0); 67. close(clifd); 68. 69. /exit 70. close(servfd); 71. return 0; 72. /* * Tcp Server program, It is a simple example only. * zhengsh 200520602061 2 * when client connect to server, send a welcome message and timestamp in server. */ #include <stdio.h>#include <sys/socket.h>#include <unistd.h>#include <sys/types.h>#include <netinet/in.h>#include <stdlib.h>#include <time.h> #define SERVER_PORT 20000 / define the defualt connect port id#define LENGTH_OF_LISTEN_QUEUE 10 /length of listen queue in server#define BUFFER_SIZE 255#define WELCOME_MESSAGE "welcome to connect the server. " int main(int argc, char *argv) int servfd,clifd; struct sockaddr_in servaddr,cliaddr; if (servfd = socket(AF_INET,SOCK_STREAM,0) < 0) printf("create socket error!n"); exit(1); bzero(&servaddr,sizeof(servaddr); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(SERVER_PORT); servaddr.sin_addr.s_addr = htons(INADDR_ANY); if (bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr)<0) printf("bind to port %d failure!n",SERVER_PORT); exit(1); if (listen(servfd,LENGTH_OF_LISTEN_QUEUE) < 0) printf("call listen failure!n"); exit(1); while (1) /server loop will nerver exit unless any body kill the process char bufBUFFER_SIZE; long timestamp; socklen_t length = sizeof(cliaddr); clifd = accept(servfd,(struct sockaddr*)&cliaddr,&length); if (clifd < 0) printf("error comes when call accept!n"); break; strcpy(buf,WELCOME_MESSAGE); /inet_ntop(INET_ADDRSTRLEN,cliaddr.sin_addr,buf,BUFFER_SIZE); printf("from client,IP:%s,Port:%dn",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port); timestamp = time(NULL); strcat(buf,"timestamp in server:"); strcat(buf,ctime(×tamp); send(clifd,buf,BUFFER_SIZE,0); close(clifd); /exit close(servfd); return 0; 客户每次用一个随机的端口连接服务器,并接收来自服务器的欢迎信息,然后打印出来(tcpclient).运行的时候接受一个参数,也就是服务器的ip地址.Cpp代码 1. /* Tcp client program, It is a simple example only. 2. * zhengsh 200520602061 2 3. * connect to server, and echo a message from server. 4. */ 5. 6. 7. #include <stdio.h> 8. #include <sys/socket.h> 9. #include <unistd.h> 10. #include <sys/types.h> 11. #include <netinet/in.h> 12. #include <stdlib.h> 13. 14. #define SERVER_PORT 20000 / define the defualt connect port id 15. #define CLIENT_PORT (20001+rand()%65536) / define the defualt client port as a random port 16. 17. #define BUFFER_SIZE 255 18. #define REUQEST_MESSAGE "welcome to connect the server.n" 19. 20. void usage(char *name) 21. 22. printf("usage: %s IpAddrn",name); 23. 24. 25. int main(int argc, char *argv) 26. 27. int servfd,clifd,length = 0; 28. struct sockaddr_in servaddr,cliaddr; 29. socklen_t socklen = sizeof(servaddr); 30. char bufBUFFER_SIZE; 31. 32. if (argc < 2) 33. 34. usage(argv0); 35. exit(1); 36. 37. 38. if (clifd = socket(AF_INET,SOCK_STREAM,0) < 0) 39. 40. printf("create socket error!n"); 41. exit(1); 42. 43. srand(time(NULL);/initialize random generator 44. bzero(&cliaddr,sizeof(cliaddr); 45. cliaddr.sin_family = AF_INET; 46. cliaddr.sin_port = htons(CLIENT_PORT); 47. cliaddr.sin_addr.s_addr = htons(INADDR_ANY); 48. 49. bzero(&servaddr,sizeof(servaddr); 50. servaddr.sin_family = AF_INET; 51. inet_aton(argv1,&servaddr.sin_addr); 52. servaddr.sin_port = htons(SERVER_PORT); 53. /servaddr.sin_addr.s_addr = htons(INADDR_ANY); 54. 55. if (bind(clifd,(struct sockaddr*)&cliaddr,sizeof(cliaddr)<0) 56. 57. printf("bind to port %d failure!n",CLIENT_PORT); 58. exit(1); 59. 60. 61. if (connect(clifd,(struct sockaddr*)&servaddr, socklen) < 0) 62. 63. printf("can't connect to %s!n",argv1); 64. exit(1); 65. 66. 67. length = recv(clifd,buf,BUFFER_SIZE,0); 68. if (length < 0) 69. 70. printf("error comes when recieve data from server %s!",argv1); 71. exit(1); 72. 73. printf("from server %s :nt%s ",argv1,buf); 74. 75. close(clifd); 76. return 0; 77.