Linux TCP网络编程示例

Linux大全评论508 views阅读模式
  1. #include <sys/types.h>   
  2. #include <sys/socket.h>   
  3. #include <arpa/inet.h>   
  4. #include <netinet/in.h>   
  5. #include <strings.h>   
  6. #include <string.h>   
  7. #include <stdio.h>   
  8. #include <stdlib.h>   
  9. #include <errno.h>   
  10. #include <unistd.h>   
  11. int main(int argc, char *argv[])   
  12. {   
  13.     int sockfd;   
  14.     char buffer[1024];   
  15.     struct sockaddr_in server_addr;   
  16.     int portnumber, nbytes;   
  17.     if (argc != 3) {   
  18.         fprintf(stderr, "Usage:%s hostname portnumber\n\a", argv[0]);   
  19.         exit(1);   
  20.     }   
  21.     if (inet_aton(argv[1], &server_addr.sin_addr) == 0) {   
  22.         fprintf(stderr, "the hostip is not right!");   
  23.         exit(1);   
  24.     }   
  25.     if ((portnumber = atoi(argv[2])) < 0) {   
  26.         fprintf(stderr, "Usage:%s hostname portnumber\n\a", argv[0]);   
  27.         exit(1);   
  28.     }   
  29.     // 创建套接字   
  30.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {   
  31.         fprintf(stderr, "Socket Error:%s\n\a", strerror(errno));   
  32.         exit(1);   
  33.     }   
  34.     // 填充服务器的地址信息   
  35.     server_addr.sin_family = AF_INET;   
  36.     server_addr.sin_port = htons(portnumber);   
  37.     // 向服务器发起连接   
  38.     if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {   
  39.         fprintf(stderr, "Connect Error:%s\n\a", strerror(errno));   
  40.         exit(1);   
  41.     }   
  42.     // 连接成功后,从服务器接收信息   
  43.     if ((nbytes = read(sockfd, buffer, 1024)) == -1) {   
  44.         fprintf(stderr, "Read Error:%s\n", strerror(errno));   
  45.         exit(1);   
  46.     }   
  47.     buffer[nbytes] = '\0';   
  48.     printf("I have received:%s\n", buffer);   
  49.     close(sockfd);   
  50.     exit(0);   
  51. }  

 

  1. #include <sys/types.h>   
  2. #include <sys/socket.h>   
  3. #include <arpa/inet.h>   
  4. #include <netinet/in.h>   
  5. #include <strings.h>   
  6. #include <string.h>   
  7. #include <stdio.h>   
  8. #include <stdlib.h>   
  9. #include <errno.h>   
  10. #include <unistd.h>   
  11. int main(int argc, char *argv[])   
  12. {   
  13.     int sockfd, new_fd;   
  14.     struct sockaddr_in server_addr;   
  15.     struct sockaddr_in client_addr;   
  16.     int sin_size, portnumber;   
  17.     char hello[] = "Hello! Are you fine?\n";   
  18.     if (argc != 2) {   
  19.         fprintf(stderr, "Usage:%s portnumber\a\n", argv[0]);   
  20.         exit(1);   
  21.     }   
  22.     if ((portnumber = atoi(argv[1])) < 0) {   
  23.         fprintf(stderr, "Usage:%s portnumber\a\n", argv[0]);   
  24.         exit(1);   
  25.     }   
  26.     // 创建套接字   
  27.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {   
  28.         fprintf(stderr, "Socket error:%s\n\a", strerror(errno));   
  29.         exit(1);   
  30.     }   
  31.     // 填充服务器的地址结构   
  32.     bzero(&server_addr, sizeof(struct sockaddr_in));   
  33.     server_addr.sin_family = AF_INET;   
  34.     server_addr.sin_addr.s_addr = htonl(INADDR_ANY);   
  35.     server_addr.sin_port = htons(portnumber);   
  36.     // 套接字绑定地址   
  37.     if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {   
  38.         fprintf(stderr, "Bind error:%s\n\a", strerror(errno));   
  39.         exit(1);   
  40.     }   
  41.     // 进入监听状态   
  42.     if (listen(sockfd, 5) == -1) {   
  43.         fprintf(stderr, "Listen error:%s\n\a", strerror(errno));   
  44.         exit(1);   
  45.     }   
  46.     while (1) {   
  47.         // 接收客户端的连接   
  48.         sin_size = sizeof(struct sockaddr_in);   
  49.         if ((new_fd = accept(sockfd, (struct sockaddr *)&client_addr, (socklen_t *)&sin_size)) == -1) {   
  50.             fprintf(stderr, "Accept error:%s\n\a", strerror(errno));   
  51.             exit(1);   
  52.         }   
  53.         fprintf(stderr, "Server get connection from %s\n", inet_ntoa(client_addr.sin_addr));   
  54.         if (write(new_fd, hello, strlen(hello)) == -1) {   
  55.             fprintf(stderr, "Write error:%s\n", strerror(errno));   
  56.             exit(1);   
  57.         }   
  58.         close(new_fd);   
  59.     }   
  60.     close(sockfd);   
  61.     //  exit(0);   
  62.     return 0;   
  63. }  

企鹅博客
  • 本文由 发表于 2020年9月24日 16:56:14
  • 转载请务必保留本文链接:https://www.qieseo.com/152176.html

发表评论