a little socket program,connect ok,but recv error

Post new topic   Reply to topic    DD-WRT Forum Index -> Generic Questions
Author Message
omencathay
DD-WRT Novice


Joined: 31 Mar 2010
Posts: 11

PostPosted: Thu Apr 01, 2010 8:57    Post subject: a little socket program,connect ok,but recv error Reply with quote
belkin F5D7231-4
DD-WRT v24-sp2
toolchain gcc.3.4.4-broadcom-mipsel.tar download from www.dd-wrt.com

I write a little test program to post data to a webserver.

Code:

//test.c
int main(){

    int sockfd;
    struct sockaddr_in _addr;
    struct hostent *he;

    if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){
       printf("create socket error  %s\n",strerror(errno));
       return -1;
    }


    if((he = gethostbyname("192.168.1.149"))==NULL){
        printf("get host error\n");
        return -2;
    }
    _addr.sin_family = AF_INET;
    _addr.sin_port = htons(8000);
    _addr.sin_addr = *((struct in_addr *)he->h_addr);

    int conn;

    if(conn=connect(sockfd, (struct sockaddr *)&_addr,sizeof(struct sockaddr))<0){
        printf("connect socket error  %s\n",strerror(errno));
        return -1;
    }
    printf("conn %d\n",conn);
    char buf[1024];
    sprintf(buf,"%s","POST /detect/ HTTP/1.0\r\nHOST: 192.168.1.149:8000\r\nUser-Agent: test Http 0.1\r\nCache-Control: no-cache\r\nAccept: */*\r\nContent-Length: 102\r\n\r\n0001web100110008AKILISNB00740004006600170005Linux00120000001300000014000000150000001600000018000510001");
    char res[1024];
    if(send(sockfd,buf,strlen(buf),0)<0){
        printf("send socket error  %s\n",strerror(errno));
        return -1;
    }
    if(read(sockfd, res,sizeof(res))<0){
         printf("recv socket error  %s\n",strerror(errno));
        return -1;
    }

    printf("the result is : %s\n",res);

    return 0;
}



then
mipsel-linux-gcc -o test test.c
ok
and upload the compiled "test" program to dd-wrt
Code:

$scp test root@192.168.1.1:/tmp/root
root@ddwrt:~#./test

the result shows that:
socket connect() ok
socket send() ok
but when read(),there is an error:connection refused,errno=146

I test the wget command in busyBox V1.13.4 to get that url ok.

who can help me? sorry for my poor english.
Sponsor
omencathay
DD-WRT Novice


Joined: 31 Mar 2010
Posts: 11

PostPosted: Thu Apr 01, 2010 10:03    Post subject: Reply with quote
I know the reanson,I used the wrong toolchain, althought it can compile the test.c program.
I use another version toolchain gcc-4.1,the problem is resolved.
chriselviss
DD-WRT Novice


Joined: 12 Oct 2015
Posts: 1

PostPosted: Mon Oct 12, 2015 6:28    Post subject: Socket Programming Reply with quote
Check this one.....Socket Programming example

Chris
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> Generic Questions All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum