Socket編程獲取服務器時間

客戶端向服務器端發送請求,服務器收到請求做相應的處理,將處理結果傳回客戶端。下面採用TCP協議實現服務器和客戶端之間的連接。

1.      客戶端

約定雙方的傳輸協議(UDP或者TCP),根據傳輸協議創建socket;

服務器的IP地址和端口號;

連接服務器;

獲取服務器傳遞回來的數據。

  1. <span style="font-size:18px;"><strong>#include<string.h>     
  2. #include <sys/types.h>     
  3. #include <sys/socket.h>     
  4. #include <sys/time.h>     
  5. #include <time.h>     
  6. #include <fcntl.h>     
  7. #include<netinet/in.h>     
  8. #include<arpa/inet.h>     
  9. #include <sys/errno.h>     
  10. #include<iostream>     
  11. #include<stdlib.h>     
  12. #include<stdio.h>     
  13. using namespace std;    
  14. const int MAXLINE=1024;    
  15. int main(int argc,char** argv)    
  16. {    
  17.     int sockfd,n;    
  18.     char recvline[MAXLINE+1];    
  19.     struct sockaddr_in servaddr;    
  20.     if(argc!=2)    
  21.     {    
  22.         cout<<"usage: a.out<IPaddress"<<endl;    
  23.         exit(0);    
  24.     }    
  25.     sockfd=socket(AF_INET,SOCK_STREAM,0);    
  26.     if(sockfd<0)    
  27.     {    
  28.         cout<<"socket error"<<endl;    
  29.         exit(0);    
  30.     }    
  31.     memset(&servaddr,0, sizeof(servaddr));    
  32.     servaddr.sin_family=AF_INET;    
  33.     servaddr.sin_port=htons(8080);//將無符號短整型數值轉換爲網絡字節序,即將數值的高位字節存放到內存中的低位字節0X1234變爲0X3412     
  34.     if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<=0)//將ip地址在“點分十進制”和整數之間轉換     
  35.     {    
  36.         cout<<"inet_ptons error"<<endl;    
  37.         exit(0);    
  38.     }    
  39.     if(connect(sockfd,(sockaddr*)&servaddr,sizeof(servaddr))<0)    
  40.     {    
  41.         cout<<"connect error"<<endl;    
  42.         exit(0);    
  43.     }    
  44.     while((n=read(sockfd,recvline,MAXLINE))>0)    
  45.     {    
  46.         recvline[n]=0;    
  47.         if(fputs(recvline,stdout)==EOF)    
  48.         {    
  49.             cout<<"fputs error"<<endl;    
  50.             exit(0);    
  51.         }    
  52.     }    
  53.     if(n<0)    
  54.     {    
  55.       cout<<"read error"<<endl;    
  56.       exit(0);    
  57.     }    
  58.     exit(0);    
  59. }  </strong></span>  

2.      服務器

約定雙方的傳輸協議(UDP或者TCP),根據傳輸協議創建socket;

將地址和端口綁定到socket;

對端口進行偵聽,直到偵聽到有連接信息;

接收連接,然後將數據寫回連接中。

將地址和端口綁定到socket  


  1. <span style="font-size:18px;"><strong>#include<string.h>    
  2. #include <sys/types.h>    
  3. #include <sys/socket.h>    
  4. #include <sys/time.h>    
  5. #include <time.h>    
  6. #include <fcntl.h>    
  7. #include<netinet/in.h>    
  8. #include<arpa/inet.h>    
  9. #include <sys/errno.h>    
  10. #include<iostream>    
  11. #include<stdlib.h>    
  12. #include<stdio.h>    
  13. #include<errno.h>    
  14. using namespace std;    
  15.     
  16. const int MAXLINE=1024;    
  17. int main(int argc,char **argv)    
  18. {    
  19.     int listenfd,connfd;    
  20.     struct sockaddr_in servaddr;    
  21.     char buff[MAXLINE];    
  22.     time_t ticks;    
  23.     listenfd=socket(AF_INET,SOCK_STREAM,0);//建立socket    
  24.     if(listenfd<0)    
  25.     {    
  26.         cout<<"socket error "<<strerror(errno)<<endl;    
  27.         exit(0);    
  28.     }    
  29.     memset(&servaddr,0, sizeof(servaddr));    
  30.     servaddr.sin_family=AF_INET;    
  31.     servaddr.sin_port=htons(8080);    
  32.     servaddr.sin_addr.s_addr=htonl(INADDR_ANY);    
  33.     int bindfd=bind(listenfd,(sockaddr*)&servaddr,sizeof(servaddr));//將地址和端口綁定到socket    
  34.     if(bindfd<0)    
  35.     {    
  36.         cout<<"bind error"<<bindfd<<endl;    
  37.         exit(0);    
  38.     }    
  39.     listen(listenfd,MAXLINE);//監聽連接,一直阻塞,直到有連接出現    
  40.     while(1)    
  41.     {    
  42.         connfd=accept(listenfd,(sockaddr*)NULL,NULL);//接收連接傳遞的數據    
  43.         ticks=time(NULL);    
  44.         snprintf(buff,sizeof(buff),"%.24s\r\n",ctime(&ticks));    
  45.         cout<<strlen(buff)<<endl;    
  46.         write(connfd,buff,strlen(buff));//將傳遞的數據寫回到socket中    
  47.         close(connfd);    
  48.     }    
  49.     return 0;    
  50. }  </strong></span>  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章