English 中文(简体)
read、错误行为
原标题:socket recv in thread , wrong behavior

!

I use unpv13e library in linux developed as socket server , and listen a port which will accept 3 socket clients (at most) , each client would has its own thread ....

While these 3 clients send very very quick , the recv function of socket server will receive said half of string came from client1 , half of another came from client2 , it is odd because I think these 3 socket clients would be run in different thread , also different socket id , so I am curious why it happened ? let me explain in the following codes :

listenfd = Tcp_listen(ipaddr,portno,&addrlen);
cliaddr = Malloc(addrlen);
Signal(SIGINT, sig_int);
for ( ; ; ) {
    clilen = addrlen;
    connfd = Accept(listenfd, cliaddr, &clilen);
    err_msg("id [%05d] conned from %s",connfd,Sock_ntop(cliaddr, clilen));
    Pthread_create(&tid, NULL, &doit, (void *) connfd);
}


void * doit(void *arg)
{
    void web_child(int);
    Pthread_detach(pthread_self());
    web_child((int) arg);
    Close((int) arg);
    printf("thread [%05d] dead...
",(int) arg);
    return(NULL);
}


void web_child(int sockfd)
{
    int        connfd;
    ssize_t    nread;
    char       line[1024];

    for ( ; ; )
    {
        line[0]=0x00 ;
        if ( (nread = Readline(sockfd, line, 1024)) == 0)
            break;         /* connection closed by other end */
        line[nread-2]=0x00 ;
        if(strncmp(line,"101",3)==0)
            Do101(line) ;
        if(strncmp(line,"201",3)==0)
            Do201(line) ;
        if(strncmp(line,"301",3)==0)
            Do301(line) ;
    }
}

Readline function in unpv13e library call recv , and check one char at a time until it is a and return , the nread usually 315 bytes or so , and I do not have send in this socket server !! In my opinion , web_child function is run as thread , with different socketfd , and line is local var , so it is no way that 3 different client will effect each other, socket client1 will always send string start with "101" , and client2 always send "201", client3 send "301" ....

But somestimes I ll see a string in Do101(line) , which the firat half of it is "101" , and second half of it came from client2 , they just effect each other ... if these 3 clients send very very frequently , it might happened ... not very often , but it just happened !!

What kind of bug I ve in the code ? recv from different socketid in different thread will effect each other ?

任何建议都值得欢迎! 感谢!

最佳回答
问题回答

如果<代码>Readline()只收到1份,则该行即告失败:

line[nread-2]=0x00 ;

You might try changing it into:

line[nread-1]=0x00 ;




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签