《Linux Socket编程实例.doc》由会员分享,可在线阅读,更多相关《Linux Socket编程实例.doc(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Linux Socket编程实例(一个Hello World程序)在Linux下写了个小的socket程序,分为客户端和服务器端,服务端开一个端口(2000),做为一个daemon,等待客户的连接请求.一旦有客户连接,服务器端打印出客户端的IP地址和端口,并且向服务器端发送欢迎信息和时间.下面是服务端的代码(tcpserver.c).由于这只是个简单的程序,所以只用了单线程实现!Cpp代码 1. /* 2. *TcpServerprogram,Itisasimpleexampleonly. 3. *zhengsh2005206020612 4. *whenclientconnecttoserv
2、er,sendawelcomemessageandtimestampinserver. 5. */6. 7. #include 8. #include 9. #include 10. #include 11. #include 12. #include 13. #include 14. 15. #defineSERVER_PORT20000/definethedefualtconnectportid 16. #defineLENGTH_OF_LISTEN_QUEUE10/lengthoflistenqueueinserver 17. #defineBUFFER_SIZE255 18. #def
3、ineWELCOME_MESSAGEwelcometoconnecttheserver. 19. 20. 21. intmain(intargc,char*argv) 22. 23. intservfd,clifd; 24. structsockaddr_inservaddr,cliaddr; 25. 26. if(servfd=socket(AF_INET,SOCK_STREAM,0)0) 27. 28. printf(createsocketerror!n); 29. exit(1); 30. 31. bzero(&servaddr,sizeof(servaddr); 32. servad
4、dr.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,(structsockaddr*)&servaddr,sizeof(servaddr)0) 37. 38. printf(bindtoport%dfailure!n,SERVER_PORT); 39. exit(1); 40. 41. 42. if(listen(servfd,LENGTH_OF_LISTEN_QUEUE)0)
5、 43. 44. printf(calllistenfailure!n); 45. exit(1); 46. 47. 48. while(1) 49. /serverloopwillnerverexitunlessanybodykilltheprocess 50. charbufBUFFER_SIZE; 51. longtimestamp; 52. socklen_tlength=sizeof(cliaddr); 53. clifd=accept(servfd,(structsockaddr*)&cliaddr,&length); 54. if(clifd0) 55. 56. printf(e
6、rrorcomeswhencallaccept!n); 57. break; 58. 59. strcpy(buf,WELCOME_MESSAGE); 60. /inet_ntop(INET_ADDRSTRLEN,cliaddr.sin_addr,buf,BUFFER_SIZE); 61. 62. printf(fromclient,IP:%s,Port:%dn,inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port); 63. timestamp=time(NULL); 64. strcat(buf,timestampinserver:); 65
7、. strcat(buf,ctime(×tamp); 66. send(clifd,buf,BUFFER_SIZE,0); 67. close(clifd); 68. 69. /exit 70. close(servfd); 71. return0; 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. */ #i
8、nclude #include #include #include #include #include #include #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) i
9、nt 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(
10、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 timest
11、amp; 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(cliad
12、dr.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. /*Tcpclientprogr
13、am,Itisasimpleexampleonly. 2. *zhengsh2005206020612 3. *connecttoserver,andechoamessagefromserver. 4. */5. 6. 7. #include 8. #include 9. #include 10. #include 11. #include 12. #include 13. 14. #defineSERVER_PORT20000/definethedefualtconnectportid 15. #defineCLIENT_PORT(20001+rand()%65536)/definethed
14、efualtclientportasarandomport 16. 17. #defineBUFFER_SIZE255 18. #defineREUQEST_MESSAGEwelcometoconnecttheserver.n 19. 20. voidusage(char*name) 21. 22. printf(usage:%sIpAddrn,name); 23. 24. 25. intmain(intargc,char*argv) 26. 27. intservfd,clifd,length=0; 28. structsockaddr_inservaddr,cliaddr; 29. soc
15、klen_tsocklen=sizeof(servaddr); 30. charbufBUFFER_SIZE; 31. 32. if(argc2) 33. 34. usage(argv0); 35. exit(1); 36. 37. 38. if(clifd=socket(AF_INET,SOCK_STREAM,0)0) 39. 40. printf(createsocketerror!n); 41. exit(1); 42. 43. srand(time(NULL);/initializerandomgenerator 44. bzero(&cliaddr,sizeof(cliaddr);
16、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
17、.s_addr=htons(INADDR_ANY); 54. 55. if(bind(clifd,(structsockaddr*)&cliaddr,sizeof(cliaddr)0) 56. 57. printf(bindtoport%dfailure!n,CLIENT_PORT); 58. exit(1); 59. 60. 61. if(connect(clifd,(structsockaddr*)&servaddr,socklen)0) 62. 63. printf(cantconnectto%s!n,argv1); 64. exit(1); 65. 66. 67. length=recv(clifd,buf,BUFFER_SIZE,0); 68. if(length0) 69. 70. printf(errorcomeswhenrecievedatafromserver%s!,argv1); 71. exit(1); 72. 73. printf(fromserver%s:nt%s,argv1,buf); 74. 75. close(clifd); 76. return0; 77.