`
haoningabc
  • 浏览: 1475143 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

badserver socket

c 
阅读更多
代码是《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


分享到:
评论

相关推荐

    mailserver开源项目

    *) Fix: If remote host shut down socket(Socket.ShutDown), the empty command buffer considered as bad empty command (not disposing session before socket close reach). (04.10.2011) *) New: Added ...

    linux下安装mysql及mysql.sock问题

    最近在linux上装Aphace... can't connect to local MySQL server through socket 'var/run/mysqld/mysqld.sock'  这个把我弄得几近崩溃,我在网上查找了很多的解决办法,大概有几种解决方案:  1.权限问题  2.修改/

    Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: ….. this is incompatible with sq

    3、Can’t connect to local MySQL server through socket ‘/Data/mydata/mysql.sock’ socket文件目录不对应导致的问题 4、今天要说的就是 没有打开only_full_group_by Cause:...

    UPnP_Programming_Guide

    - **4.1.20** `UPNP_E_SOCKET_CONNECT [-204]`: Indicates a connection error on a socket. - **4.1.21** `UPNP_E_OUTOF_SOCKET [-205]`: Signals that there are no more available sockets. - **4.1.22** `UPNP_E...

    linux 下 结合ftp 和部分telnet 功能的服务器源码

    case E_BAD_SOCK_FD : default : unknown error! 6 bugs both side couldn't send control signal to each other, that is you can only send message that after a line break. so there are ...

    Python实现的检测web服务器健康状况的小程序

    ”server is ok”:”server is bad”}END{print health}’ server is ok 说白了这些方式都是封装了curl库的,另外还有一些关于http的模块,例如perl的LWP,python的httplib,urllib或者urllib2 也可以去实现,今天就...

    实现并发的httpd简单服务器

    - **400 Bad Request**:客户端请求语法错误。 - **404 Not Found**:请求的资源未找到。 - **500 Internal Server Error**:服务器遇到了无法处理的情况。 - **501 Method Not Implemented**:服务器不支持请求中...

    java练习题

    BadStudent badStudent = new BadStudent("Bob", 22, "Male"); badStudent.eat(); badStudent.smoke(); badStudent.drink(); } } ``` #### 5. 多态及向下转型 **知识点**: 多态性,父类引用指向子类对象,以及...

    Samba服务器配置文件[整理].pdf

    - `socket address`:指明Samba监听的IP地址。 - `security`:设置共享安全级别,包括`share`(无验证)、`user`(用户级验证)、`server`(服务器级验证)和`domain`(域级验证)。 - `password server`:当使用...

    loadrunner常用报错整理

    2. Error -10057: "Failed to transmit data to network: socket is not connected." 这个错误可能是由于网络连接问题。如果在多台机器上执行相同测试,只有部分机器出现错误,可能是特定机器的网络状况问题。检查...

    cvs for linux安装文档1.2.1

    cvs [pserver aborted]: bad auth protocol start: hi Connection closed by foreign host. ``` 这表明CVS服务已经正确启动并且响应客户端请求。 #### 四、配置客户端 ##### 4.1 Wincvs (WinCvs2_0_8) 对于...

    ssd8 exam1

    2. **代理服务器(Proxy Server)**:一种服务器(或服务程序),作为客户端和服务端之间的中间层,可以过滤、缓存或修改请求和响应数据。 3. **Java网络编程**:利用Java语言进行网络应用开发的技术。 4. **HTTP...

    php.ini-development

    interesting side-effects depending on your application and web server. ; You may be able to send headers and cookies after you've already sent output ; through print or echo. You also may see ...

    BIND-LINUX64位包

    issue with potential bad data in a database used by DLZ or SDB. [RT #34993] Fix build issue on newer FreeBSD needing -lhx509 for GSSAPI build. [RT #35001] Address read after free in server side...

    centos安装mysql.docx

    如果在安装过程中遇到错误提示“/usr/bin/perl: bad interpreter: No such file or directory”,可以通过`yum -y install perl perl-devel`来安装Perl及其开发库。如果遇到“error while loading shared libraries:...

    UBOOT 使用说明

    - 注意:首次启动可能会出现关于`bad crc`的警告信息。这通常是由于环境变量尚未保存导致的。一旦保存了环境变量设置,这种警告就会消失。 #### 三、配置TFTP服务器 为了便于从主机向开发板传输文件,需要配置...

    [转载验证]Ubuntu 9.10 samba 服务器配置说明

    1. **[global]**:全局配置段,包含服务器的一般设置,如工作组名(workgroup = MYGROUP),服务器的角色(server string = %h server (Samba, Ubuntu)),以及安全级别(security = user)。 2. **[homes]**:此...

    Sakemail

    Minor bugs fixed (some variants of boundary)14/10/971.1.0- Warning: WSockets1.2 have some bugs that result in bad attachments. So I decided to use the sockets of Delphi 3 founded in D3 c/s D3.01 pro ...

    ws

    local http_server = require 'socket.http.server' local function handle(ws) while true do local msg, err = ws:receive() if not msg then break end print('Received:', msg) ws:send(msg) -- echo back...

Global site tag (gtag.js) - Google Analytics