• socket 编程 简单的TCP


    客户端向服务器发出连接请求后,服务器端写入数据发送给客户端,在客户端显示。

    server:

     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <errno.h>
     4 #include <string.h>
     5 #include <netdb.h>
     6 #include <sys/types.h>
     7 #include <netinet/in.h>
     8 #include <sys/socket.h>
     9 #include <unistd.h>
    10 
    11 #define MAXSIZE 1024
    12 
    13 int main(int argc, char* argv[])
    14 {
    15     int sockfd, new_fd;
    16     struct sockaddr_in server_addr;
    17     struct sockaddr_in client_addr;
    18     int sin_size, portnumber;
    19     char buf[MAXSIZE];
    20     if(argc != 2)
    21     {
    22         fprintf(stderr,"Usage: %s portnumbera
    ",argv[0]);
    23         exit(1);
    24     }
    25     if((portnumber = atoi(argv[1]))<0)
    26     {
    27         /*获得命令行的第二个参数端口号,atoi()把字符串转换成整形数*/
    28         fprintf(stderr,"usage: %s portnumbera
    ",argv[0]);
    29         exit(1);
    30     }
    31     /*服务器开始建立socket描述符*/
    32     sockfd = socket(AF_INET,SOCK_STREAM,0);
    33     
    34     /*服务器端填充sockaddr结构*/
    35     bzero(&server_addr,sizeof(struct sockaddr_in));
    36     server_addr.sin_family=AF_INET;
    37     server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    38     server_addr.sin_port = htons(portnumber);
    39     
    40     /*调用bind函数绑定端口*/
    41     bind(sockfd,(struct sockaddr*)(&server_addr),sizeof(struct sockaddr));
    42     
    43     /*监听sockfd描述符,同时处理的最大链接请求数为5*/
    44     listen(sockfd,5);
    45 
    46     /*服务器阻塞,等待接收链接请求,直到客户端发送链接请求*/
    47     while(1)
    48     {
    49         sin_size = sizeof(struct sockaddr_in);
    50         /*调用accept接受一个连接请求*/
    51         if((new_fd = accept(sockfd,(struct sockaddr*)(&client_addr),&sin_size))==-1)
    52         {
    53             printf("accept error!
    ");
    54             exit(1);
    55         }
    56         /*TCP链接已建立,打印申请链接的客户机的IP地址*/
    57         printf("Server get connection from %d
    ",inet_ntoa(client_addr.sin_addr));
    58         /*提示用户输入要发送的数据,长度小于缓冲区的长度*/
    59         printf("Connected successful, please input the message[less than 1024 bytes]:
    ");
    60         
    61         /*从终端输入要发送的数据,放到buf缓冲区*/
    62         fgets(buf,sizeof(buf),stdin);
    63         
    64         /*调用write发送数据*/
    65         if(write(new_fd,buf,strlen(buf))==-1)
    66         {
    67             printf("write error!
    ");
    68             exit(1);
    69         }
    70         close(new_fd);
    71     }
    72     close(sockfd);
    73     return 0;
    74 }        

    client:

     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <errno.h>
     4 #include <string.h>
     5 #include <netdb.h>
     6 #include <sys/types.h>
     7 #include <netinet/in.h>
     8 #include <sys/socket.h>
     9 
    10 int main(int argc, char* argv[])
    11 {
    12     int sockfd;
    13     char buffer[1024];
    14     /*定义服务器端套接口数据结构*/
    15     struct sockaddr_in server_addr;
    16     struct hostent *host;
    17     int portnumber, nbytes;
    18     if(argc!=3)
    19     {
    20         printf("Usage: %s hostname portnumbera
    ",argv[0]);
    21         exit(1);
    22     }
    23     /*获得命令行的第二个参数--主机名*/
    24     if((host=gethostbyname(argv[1]))==NULL)
    25     {
    26         printf("get host name error
    ");
    27         exit(1);
    28     }
    29     /*获得第三个参数,端口号*/
    30     if((portnumber = atoi(argv[2]))<0)
    31     {
    32         printf("port number error
    ");
    33         exit(1);
    34     }
    35     /*客户程序开始建立sockfd描述符*/
    36     if((sockfd = socket(AF_INET,SOCK_STREAM,0))==01)
    37     {
    38         printf("socket error
    ");
    39         exit(1);
    40     }
    41     /*客户程序填充服务端资料*/
    42     bzero(&server_addr,sizeof(server_addr));
    43     server_addr.sin_family = AF_INET;
    44     server_addr.sin_port = htons(portnumber);
    45     server_addr.sin_addr = *((struct in_addr*)host->h_addr);
    46     /*客户程序发起连接请求*/
    47     connect(sockfd,(struct sockaddr*)(&server_addr),sizeof(struct sockaddr));
    48     
    49     /*调用read读取服务器发送来的数据*/
    50     if((nbytes = read(sockfd,buffer,1024))==-1)
    51     {
    52         printf("read error
    ");
    53         exit(1);
    54     }
    55     buffer[nbytes] = '';
    56     printf("I have received:%s
    ",buffer);
    57     close(sockfd);
    58     return 0;
    59 }
    60     
  • 相关阅读:
    em,pt和px之间的换算
    css中 中文字体(fontfamily)的标准英文名称
    HTML css面试题
    css实现的透明三角形
    JavaScript经典面试题系列
    C++ template 学习笔记(第二章)
    C++ template 学习笔记 (第五章)
    20120906
    C++ template 学习笔记(第十六章) 16.1 命名模版参数
    C++ template 学习笔记(第三,四章)
  • 原文地址:https://www.cnblogs.com/yongjiuzhizhen/p/3440115.html
Copyright © 2020-2023  润新知