Linux 下socket编程客户端与服务端代码

客户端代码:

[cpp]  view plain copy
  1. /* client */  
  2. #include <stdio.h>  
  3. #include <sys/types.h>  
  4. #include <netinet/in.h>  
  5. #include <sys/socket.h>  
  6. #include <netdb.h>  
  7. #include <unistd.h>  
  8. #include <string.h>  
  9. #include <arpa/inet.h>  
  10. #include <errno.h>  
  11. #define PORT 8848  
  12. #define MAXDATASIZE 100  
  13.   
  14. int main(int argc, char *argv[])  
  15. {  
  16.    char hostname[100];  
  17.    gethostname(hostname,sizeof(hostname));  
  18.    printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%s\n",hostname);  
  19.     int sockfd, numbytes;  
  20.     char buf[MAXDATASIZE];  
  21.     struct hostent *he;  
  22.     struct sockaddr_in serv_addr;  
  23.     if (argc != 2)  
  24.     {  
  25.         fprintf(stderr, "usage: cilent hostname\n");  
  26.         return 0;  
  27.     }  
  28.     if ((he = gethostbyname(argv[1])) == NULL)  
  29.     {  
  30.         fprintf(stderr, "gethostbyname error!\n");  
  31.        return 0;;  
  32.     }  
  33.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)  
  34.     {  
  35.         fprintf(stderr, "socket error!\n");  
  36.        return 0;  
  37.     }  
  38.     serv_addr.sin_family = AF_INET;  
  39.     serv_addr.sin_port = htons(PORT);  
  40.     serv_addr.sin_addr = *((struct in_addr *)he->h_addr);  
  41.     bzero(&(serv_addr.sin_zero), 8);  
  42.     if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1)  
  43.     {  
  44.         fprintf(stderr, "connect error!\n");  
  45.         return 0;  
  46.     }  
  47.     if ((numbytes = read(sockfd, buf, MAXDATASIZE)) == -1)  
  48.     {  
  49.         fprintf(stderr, "read error!\n");  
  50.         return 0;  
  51.     }  
  52.     buf[numbytes] = '\0';  
  53.     printf("Received: %s", buf);  
  54.     close(sockfd);  
  55. }  
服务器端代码:

[cpp]  view plain copy
  1. /* server */  
  2. #include <stdio.h>  
  3. #include <sys/types.h>  
  4. #include <netinet/in.h>  
  5. #include <sys/socket.h>  
  6. #include <netdb.h>  
  7. #include <unistd.h>  
  8. #include <string.h>  
  9. #include <arpa/inet.h>  
  10. #include <sys/wait.h>  
  11. #include <errno.h>  
  12.   
  13. #define PORT 8848  
  14. #define BACKLOG 10  
  15.   
  16. int main(void)  
  17. {  
  18.  int sockfd, conn_fd;  
  19.     struct sockaddr_in my_addr;  
  20.     struct sockaddr_in their_addr;  
  21.     int sin_size;  
  22.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)  
  23.     {  
  24.         fprintf(stderr, "socket error!\n");  
  25.        return 0;  
  26.     }  
  27.     my_addr.sin_family = AF_INET;  
  28.     my_addr.sin_port = htons(PORT);  
  29.     my_addr.sin_addr.s_addr = INADDR_ANY;  
  30.     bzero(&(my_addr.sin_zero), 8);  
  31.     if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)  
  32.     {  
  33.         fprintf(stderr, "bind error!\n");  
  34.            return 0;  
  35.     }  
  36.     if (listen(sockfd, BACKLOG) == -1)  
  37.     {  
  38.         fprintf(stderr, "listen error!\n");  
  39.           return 0;  
  40.     }  
  41.     while (1)  
  42.     {  
  43.         sin_size = sizeof(struct sockaddr_in);  
  44.         if ((conn_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)  
  45.         {  
  46.             fprintf(stderr, "accept error!\n");  
  47.             continue;  
  48.         }  
  49.         printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));  
  50.         if (!fork())  
  51.         {  
  52.             if (send(conn_fd, "Hello, world!\n", 14, 0) == -1)  
  53.                 fprintf(stderr, "send error!\n");  
  54.             close(conn_fd);  
  55.             return 0;  
  56.         }  
  57.         close(conn_fd);  
  58.         while(waitpid(-1, NULL, WNOHANG) > 0);  
  59.     }  
  60. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值