Pthread 文件传输应用程序崩溃

发布于 2024-10-16 23:51:06 字数 1105 浏览 2 评论 0原文

我正在开发一个文件传输应用程序,并在接收端使用 pthreads 来接收多个文件。
传递给 pthreads 的函数调用以下函数,在该函数结束时我收到 SIGABRT 错误,并且终端上出现堆栈粉碎错误。 请帮我找出错误。如果您需要更多代码,我可以发布相同的代码。提前致谢。

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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-10-23 23:51:06
  1. 检查系统函数的返回代码,特别是recv(),它可能返回0或-1
  2. ,你忘记增加你给recv()的缓冲区指针,所以每次调用它时,缓冲区的内容都会被
  3. 覆盖在调用 sscanf() 之前忘记确保缓冲区末尾有一个 NUL 字符
  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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文