zoukankan      html  css  js  c++  java
  • 编写Linux下socket协议TCP和UDP的Client Server程序

    http://wenku.baidu.com/view/0803b63610661ed9ad51f32b.html

    一、编写Linux下socket协议TCP的ClientServer程序
     
    客户端,client:
     

    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<sys/socket.h>
    #include<sys/stat.h>
    #include<arpa/inet.h>
    #define MAXBUF 256
    int main()
    {
       int ssock;
       int clen;
       struct sockaddr_in server_addr;
       char buf[MAXBUF];
       if((ssock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0){
       perror("socket error:");
       exit(1);
       }
       clen = sizeof(server_addr);
       memset(&server_addr,0,sizeof(server_addr));
       server_addr.sin_family     =AF_INET;
       server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
       server_addr.sin_port       =htons(7758);
       if(connect(ssock,(struct sockaddr *)&server_addr,clen)<0){
       perror("connect error:");
       exit(1);
       }
       memset(buf,0,MAXBUF);
       if(read(ssock,buf,MAXBUF)<=0)
       {
          perror("read error:");
          exit(1);
       }
       close(ssock);
       printf("
    read: %s
    ",buf);
       return 0;
    }
     

     
    服务端,server:
     

    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/socket.h>
    #include<sys/stat.h>
    #include<arpa/inet.h>
    #define MAXBUF 256
    int main()
    {
        int clen;
        int ssock,csock;
        struct sockaddr_in client_addr,server_addr;
        char buf[MAXBUF];
        if((ssock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0){
        perror("socket error:");
        exit(1);
        }
        clen = sizeof(client_addr);
        memset(&server_addr,0,sizeof(server_addr));
        server_addr.sin_family     =AF_INET;
        server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
     
        server_addr.sin_port=htons(7758);
        if(bind(ssock,(struct sockaddr *)&server_addr,sizeof(server_addr))<0){
        perror("bind error");
        exit(1);
        }
        if(listen(ssock,8)<0){
        perror("listen error:");
        exit(1);
        }
        while(1){
        csock=accept(ssock,(struct sockaddr *)&client_addr,&clen);
        if(write(csock,buf, MAXBUF)<=0)
        perror("write error:");
        close(csock);
        }
        return 0;
    } 


     
    二、编写Linux下的socket协议UDP Client/Server程序
    客户端,client:
     

    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/socket.h>
    #include<sys/stat.h>
    #include<arpa/inet.h>
    #define MAXBUF 256
    int main()
    {
       int ssock;
       int clen;
       struct sockaddr_in client_addr,server_addr;
       char buf[MAXBUF];
       strcpy(buf,"I miss you already!
    ");
       if((ssock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))<0){
       perror("socket error:
    ");
       exit(1);
       }
       memset(&server_addr,0,sizeof(server_addr));
       server_addr.sin_family  =AF_INET;
       server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
       server_addr.sin_port=htons(7758);
       sendto(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&server_addr,sizeof(server_addr));
       printf("Send message:%s
    ",buf);
       clen=sizeof(client_addr);
       recvfrom(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&server_addr,&clen);
       printf("Receive message:%s
    ",buf);
       close(ssock);
       return 0;
    } 


     
    服务端,server:
     

    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/socket.h>
    #include<sys/stat.h>
    #include<arpa/inet.h>
    #define MAXBUF 256
    int main()
    {
        int ssock;
        int clen;
        struct sockaddr_in client_addr,server_addr;
        char buf[MAXBUF];
        if((ssock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))<0){
        perror("socket error:");
        exit(1);
        }
        memset(&server_addr,0,sizeof(server_addr));
        server_addr.sin_family=AF_INET;
        server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
        server_addr.sin_port=htons(7758);
        if(bind(ssock,(struct sockaddr *)&server_addr,sizeof(server_addr))<0){
        perror("bind error:");
        exit(1);
        }
        clen=sizeof(client_addr);
        recvfrom(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&client_addr,&clen);
        strcpy(buf,"I miss you");
        sendto(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&client_addr ,sizeof(client_addr));
        close(ssock);
        return 0;
    } 


     

  • 相关阅读:
    Selenium WebDriver
    Xpath常用语法
    firefox插件
    Echarts图中datazoom的相关配置
    js获取当前年月日
    echarts饼图角度起始角度位置和生长方向
    jS获取table表格任意单元格值
    jquery中 cache: true和false的区别
    Ajax请求中的async:false/true的作用 、cache: true和false的区别
    Cocos Creator set get
  • 原文地址:https://www.cnblogs.com/Adrian99/p/3299076.html
Copyright © 2011-2022 走看看