badserver socket

编程技术  /  houtizong 发布于 3年前   84
代码是《linux高级程序设计》第5章的内容

mqq@208_181:~/fabricehao/test/20120726> cat errtcpserver.c #include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <string.h>int main(int args,char * argv){    int i,s,fd,len;    struct sockaddr_in my_addr;    struct sockaddr_in remote_addr;    int sin_size;    char buf[BUFSIZ];    memset(&my_addr,0,sizeof(my_addr));    my_addr.sin_family=AF_INET;    my_addr.sin_addr.s_addr=INADDR_ANY;    unsigned int pp=87654;    my_addr.sin_port=htons(pp);    if((s=socket(AF_INET,SOCK_STREAM,0))<0){        perror("socket");        return 1;    }    if(bind(s,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0){        perror("bind");        return 1;    }    listen(s,5);    sin_size=sizeof(struct sockaddr_in);    if((fd=accept(s,(struct sockaddr *)&remote_addr,&sin_size))<0)    {        perror("accept");        return 1;    }    printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr));    len=send(fd,"welcome to my server\n",21,0);    for(i=0;i<5;i++){        len=recv(fd,buf,BUFSIZ,0);        buf[len]='\0';        printf("%s\n",buf);    }    close(fd);    close(s);    return 0;}

mqq@208_181:~/fabricehao/test/20120726> cat badclient.c #include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>int main(args,argv){    int i,s,fd,len;    struct sockaddr_in remote_addr;    int sin_size;    char buf[BUFSIZ];    memset(&remote_addr,0,sizeof(remote_addr));    remote_addr.sin_family=AF_INET;    remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");     unsigned int pp=87654;    remote_addr.sin_port=htons(pp);    if((s=socket(AF_INET,SOCK_STREAM,0))<0){        perror("socket");        return 1;    }    if(connect(s,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0){        perror("connect");        return 1;    }    printf("connect to server");    len=recv(s,buf,BUFSIZ,0);    buf[len]='\0';    printf("%s",buf);    len=send(s,"test1 message",13,0);    len=send(s,"test2 message",13,0);    len=send(s,"test3 message",13,0);    len=send(s,"test4 message",13,0);    len=send(s,"test5 message",13,0);    close(s);    return 0;}

betterserver.c
len=recv(fd,buf,13,0);指定了缓冲
#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <string.h>int main(int args,char * argv){    int i,s,fd,len;    struct sockaddr_in my_addr;    struct sockaddr_in remote_addr;    int sin_size;    char buf[BUFSIZ];    memset(&my_addr,0,sizeof(my_addr));    my_addr.sin_family=AF_INET;    my_addr.sin_addr.s_addr=INADDR_ANY;    unsigned int pp=87654;    my_addr.sin_port=htons(pp);    if((s=socket(AF_INET,SOCK_STREAM,0))<0){        perror("socket");        return 1;    }    if(bind(s,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0){        perror("bind");        return 1;    }    listen(s,5);    sin_size=sizeof(struct sockaddr_in);    if((fd=accept(s,(struct sockaddr *)&remote_addr,&sin_size))<0)    {        perror("accept");        return 1;    }    printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr));    len=send(fd,"welcome to my server\n",21,0);    for(i=0;i<5;i++){        len=recv(fd,buf,13,0);        buf[len]='\0';        printf("%s\n",buf);    }    close(fd);    close(s);    return 0;}



liburl使用
mqq@208_181:~/fabricehao/test/20120726> cat liburl.c #include <stdio.h>#include <curl/curl.h>int main(int args,char * argv){    CURL * curl;    CURLcode res;    curl =curl_easy_init();    if(!curl)    {        perror("curl");        return 1;    }    curl_easy_setopt(curl,CURLOPT_URL,argv[1]);    curl_easy_setopt(curl,CURLOPT_PROXY,"webproxy:8080");    res=curl_easy_perform(curl);    curl_easy_cleanup(curl);    return 0;}

yum install libcurl-devel  
这个是依赖
gcc -I /usr/local/include/ -o getweb liburl.c -lcurl


上一篇:udp socket

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客