Linux平臺下基於TCP/IP協議的C++網絡編程初步

原博文地址:http://blog.csdn.net/nuptboyzhb/article/details/7998066


Linux平臺下基於TCP/IP協議的C++網絡編程初步
實驗平臺:Ubuntu(Linux)
開發工具:Eclipse+CDT

作者:@鄭海波  http://blog.csdn.net/nuptboyzhb/
特點:
1.基於Linux平臺的網絡編程,最重要的特點是要調用Linux的API函數。這是與windows平臺最大的不同。
2.我們將發送的消息封裝成結構體,增添了其傳遞消息的複雜性。

3.我們用的是socket編程,沒有用ACE網絡編程。
服務器:
[c++ code]


 

  1. /* 
  2.  * MyMain.cpp 
  3.  * 
  4.  *  Created on: 2012-9-19 
  5.  *      Author: zhb 
  6.  */  
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <errno.h>  
  10. #include <string.h>  
  11. #include <sys/types.h>  
  12. #include <netinet/in.h>  
  13. #include <sys/socket.h>  
  14. #include <sys/wait.h>  
  15. #include <arpa/inet.h>  
  16. #include <unistd.h>  
  17. #define SERVPORT 3333  
  18. #define BACKLOG 10  
  19. #define MAXSIZE 1024  
  20. /* 
  21.  *自定義信息 
  22.  */  
  23. typedef struct MyMessage{  
  24.     int ID;  
  25.     char info[256];  
  26. }MyMessage,*pMyMessage;  
  27. int main() {  
  28.     int sockfd, client_fd;  
  29.     struct sockaddr_in my_addr;  
  30.     struct sockaddr_in remote_addr;  
  31.     //創建套接字  
  32.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {  
  33.         perror("socket create failed!");  
  34.         exit(1);  
  35.     }  
  36.     //綁定端口地址  
  37.     my_addr.sin_family = AF_INET;  
  38.     my_addr.sin_port = htons(SERVPORT);  
  39.     my_addr.sin_addr.s_addr = INADDR_ANY;  
  40.     bzero(&(my_addr.sin_zero), 8);  
  41.     if (bind(sockfd, (struct sockaddr*) &my_addr, sizeof(struct sockaddr))== -1) {  
  42.         perror("bind error!");  
  43.         exit(1);  
  44.     }  
  45.     //監聽端口  
  46.     if (listen(sockfd, BACKLOG) == -1) {  
  47.         perror("listen error");  
  48.         exit(1);  
  49.     }  
  50.   
  51.     while (1) {  
  52.         //int sin_size = sizeof(struct sockaddr_in);  
  53.         socklen_t sin_size = sizeof(struct sockaddr_in);  
  54.         if ((client_fd = accept(sockfd,(struct sockaddr*) &remote_addr,&sin_size)) == -1){  
  55.             perror("accept error!");  
  56.             continue;  
  57.         }  
  58.         printf("Received a connection from %s\n", (char*)inet_ntoa(remote_addr.sin_addr));  
  59.   
  60.         //子進程段  
  61.         if (!fork()){  
  62.             //接受client發送的請示信息  
  63.             int rval;  
  64.             char buf[MAXSIZE];  
  65.             if ((rval = read(client_fd, buf, MAXSIZE)) < 0) {  
  66.                 perror("reading stream error!");  
  67.                 continue;  
  68.             }  
  69.             printf("%s\n", buf);  
  70.             //向client發送信息  
  71.             //char* msg = "Hello,Mr hqlong, you are connected!\n";  
  72.             MyMessage data;  
  73.             memset((void *)&data,0,sizeof(MyMessage));  
  74.             data.ID=123;  
  75.             strcpy(data.info,"This message come from ServSocket!");  
  76.             if(send(client_fd,(void*)&data,sizeof(MyMessage),0) == -1){  
  77.                 perror("send error!");  
  78.             }  
  79.             close(client_fd);  
  80.             exit(0);  
  81.         }  
  82.         close(client_fd);  
  83.     }  
  84.     return 0;  
  85. }  


客戶端:
[c++ code]

  1. /* 
  2.  * MyMainClient.cpp 
  3.  * 
  4.  *  Created on: 2012-9-19 
  5.  *      Author: zhb 
  6.  */  
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <errno.h>  
  10. #include <string.h>  
  11. #include <sys/types.h>  
  12. #include <netinet/in.h>  
  13. #include <sys/socket.h>  
  14. #include <sys/wait.h>  
  15. #include <arpa/inet.h>  
  16. #include <unistd.h>  
  17. #define SERVPORT 3333  
  18. #define MAXDATASIZE 100  
  19. #define SERVER_IP "127.0.0.1"  
  20. #define DATA  "this is a client message"  
  21. /* 
  22.  *自定義信息 
  23.  */  
  24. typedef struct MyMessage{  
  25.     int ID;  
  26.     char info[256];  
  27. }MyMessage,*pMyMessage;  
  28. int main(int argc, char* argv[]) {  
  29.     int sockfd, recvbytes;  
  30.     //char buf[MAXDATASIZE];  
  31.     MyMessage recvData;  
  32.     struct sockaddr_in serv_addr;  
  33.   
  34.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {  
  35.         perror("socket error!");  
  36.         exit(1);  
  37.     }  
  38.     bzero(&serv_addr, sizeof(serv_addr));  
  39.     serv_addr.sin_family = AF_INET;  
  40.     serv_addr.sin_port = htons(SERVPORT);  
  41.     serv_addr.sin_addr.s_addr = inet_addr(SERVER_IP);  
  42.   
  43.     if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr))== -1) {  
  44.         perror("connect error!");  
  45.         exit(1);  
  46.     }  
  47.     write(sockfd, DATA, sizeof(DATA));  
  48.     memset((void *)&recvData,0,sizeof(MyMessage));  
  49.     if ((recvbytes = recv(sockfd, (void *)&recvData,sizeof(MyMessage), 0)) == -1) {  
  50.         perror("recv error!");  
  51.         exit(1);  
  52.     }  
  53.     //buf[recvbytes] = '\0';  
  54.     printf("Received:ID=%d,Info= %s",recvData.ID,recvData.info);  
  55.     close(sockfd);  
  56.     return 0;  
  57. }  


 

不足:服務器沒有進行多線程處理!當多個Client接入時,應考慮多線程編程。以後繼續深入...

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