Pthread 文件传输应用程序崩溃
我正在开发一个文件传输应用程序,并在接收端使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)