开发者

Pthread-ed filetransfer application crash

I am developing a file transfer application and am using pthreads on the receiver side for receiving multiple files.

The function which is passed to pthreads calls the following function and at the end of this function I get a SIGABRT error and stack-smashing error appears on the terminal. Please help me find the bugs. If you need anymore code I'd be able to post the same. Thanks in advance.

void recv_mesg(int new_sockid, char *fname)
{
    cout<<"New Thread created with "<<new_sockid<<" and "<<fname<<endl;
    char buf[MAXLINE];
    int fd;
    fd = open(fname, O_WRONLY );
    int len =0;
    while (len<1024)
    {
        int curr =  recv(new_sockid, buf, 1024-len, 0);
        //fprintf(stdout,"Message from Client:\n");
        len += curr;
        //write (fd, buf, curr);
        fputs(buf, stderr);

    }
    int file_size = 0;
    sscanf(buf,"%d",&file_size);
    if(file_size<=0)
        perror("File Size < 0");

    sprintf(buf,"Yes");
    send(new_sockid,buf,strlen(buf),0);
    len = 0;
    while (len<file_size)
    {
        int curr = recv(new_sockid, buf, min(file_size-len,MAXLINE), 0);
        len +开发者_运维问答= curr;
        write (fd, buf, curr);
        //fputs(buf, stdout);
        //fflush(stdout);
    }
    len = 0;
    close(fd);
    close(new_sockid);
}


  1. check the return code of system function, especially recv() which could return 0 or -1
  2. you forgot to increment the buffer pointer you're givin to recv(), so each time it is called, the content of the buffer is overwritten
  3. you forgot to ensure there's a NUL character at the end of buffer before calling sscanf()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜