zoukankan      html  css  js  c++  java
  • Socket编程:之TCP案例

    转载请加上博文引用:http://i.cnblogs.com/EditPosts.aspx?postid=5733248&update=1

    服务端:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <errno.h>
     4 #include <string.h>
     5 #include <netdb.h>
     6 #include <time.h>
     7 #include <unistd.h>
     8 #include <sys/types.h>
     9 #include <sys/socket.h>
    10 #include <arpa/inet.h>
    11 
    12 int sockfd;
    13 
    14 void do_service(int fd)
    15 {
    16     long t = time(0);
    17     char *s = ctime(&t);
    18     write(fd,s,strlen(s));
    19 }
    20 void out_client(struct sockaddr_in clientaddr)
    21 {
    22     char buffer[16];
    23     inet_ntop(AF_INET,&clientaddr.sin_addr.s_addr,buffer,sizeof(clientaddr));
    24     unsigned short port = ntohs(clientaddr.sin_port);
    25     printf("client ip:%s (%d)
    ",buffer,port);
    26 }
    27 
    28 
    29 int main(int argc,char *argv[])
    30 {
    31     if(argc < 2)
    32     {
    33         fprintf(stderr,"usage: %s port
    ",argv[0]);
    34         exit(1);
    35     }
    36 
    37     //创建套接字
    38     sockfd = socket(AF_INET,SOCK_STREAM,0);
    39     if(sockfd < 0)
    40     {
    41         fprintf(stderr,"socket: %s
    ",strerror(errno));
    42         exit(1);
    43     }
    44 
    45     //设置IP和端口
    46     struct sockaddr_in addr;
    47     memset(&addr,0,sizeof(addr));
    48     addr.sin_family = AF_INET;
    49     addr.sin_port = htons(atoi(argv[1]));
    50     addr.sin_addr.s_addr = INADDR_ANY; 
    51 
    52     //绑定IP和端口
    53     int len = sizeof(addr);
    54     if(bind(sockfd,(struct sockaddr*)&addr,len) < 0)
    55     {
    56         fprintf(stderr,"bind: %s
    ",strerror(errno));
    57         exit(1);
    58     }
    59 
    60     //开始听
    61     if(listen(sockfd,10) < 0)
    62     {
    63         fprintf(stderr,"listen: %s
    ",strerror(errno));
    64         exit(1);
    65     }
    66 
    67     while(1)
    68     {
    69         struct sockaddr_in clientaddr;
    70         int c_len = sizeof(clientaddr);
    71         int fd = accept(sockfd,(struct sockaddr*)&clientaddr,&c_len);
    72         if(fd < 0)
    73         {
    74             fprintf(stderr,"accept: %s
    ",strerror(errno));
    75             continue;
    76         }
    77         out_client(clientaddr);
    78         do_service(fd);
    79         close(fd);
    80     }
    81     return 0;
    82 }

    编译: 

      gcc -o server server.c

     运行:

      ./server 6666

    客户端:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <errno.h>
     4 #include <string.h>
     5 #include <netdb.h>
     6 #include <sys/socket.h>
     7 #include <netinet/in.h>
     8 #include <arpa/inet.h>
     9 #include <unistd.h>
    10 
    11 int sockfd;
    12 
    13 int main(int argc,char *argv[])
    14 {
    15     if(argc < 3)
    16     {
    17         fprintf(stderr,"usage: %s port
    ",argv[0]);
    18         exit(1);
    19     }
    20 
    21     sockfd = socket(AF_INET,SOCK_STREAM,0);
    22     if(sockfd < 0)
    23     {
    24         fprintf(stderr,"socket:%s
    ",strerror(errno));
    25         exit(1);
    26     }
    27 
    28     struct sockaddr_in sockaddr;
    29     memset(&sockaddr,0,sizeof(sockaddr));
    30     sockaddr.sin_family = AF_INET;
    31     sockaddr.sin_port = htons(atoi(argv[2]));
    32     inet_pton(AF_INET,argv[1],&sockaddr.sin_addr.s_addr);
    33     socklen_t len = sizeof(sockaddr);
    34 
    35     if(connect(sockfd,(struct sockaddr*)&sockaddr,len) < 0)
    36     {
    37         fprintf(stderr,"connect: %s
    ",strerror(errno));
    38         exit(1);
    39     }
    40     char buffer[1024];
    41     memset(buffer,0,sizeof(buffer));
    42     ssize_t n;
    43     if((n = read(sockfd,buffer,1024)) < 0)
    44     {
    45         fprintf(stderr,"read: %s
    ",strerror(errno));
    46         exit(1);
    47     }
    48     else
    49     {
    50         printf("%s
    ",buffer);
    51     }
    52 
    53     return 0;
    54 }

    编译: 

      gcc -o client client.c

     运行:

      ./client 192.168.0.30 6666 

  • 相关阅读:
    打开网页总结
    学期总结
    总结
    Sprint3
    Sprint2团队贡献分
    6.14
    典型用户与场景
    5种创建型模式
    JAVA 将接口的引用指向实现类的对象
    Java里的接口
  • 原文地址:https://www.cnblogs.com/jikexianfeng/p/5733248.html
Copyright © 2011-2022 走看看