linux中的socket阻塞问题

nanyun2010 2011-06-01 02:52:17
在网上找了一个ftp的客户端,长时间去文件列表,发生阻塞,有时提示这个错误read: Connection reset by peer
Control socket read failed: Invalid argument。请大家帮忙解决一下,急用。谢谢了。
...全文
480 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nanyun2010 2011-06-03
  • 打赏
  • 举报
回复
我想提示连接超时,程序能继续运行。怎么处理这种错误啊?
nanyun2010 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ly1213 的回复:]
不过我刚解决了 他在拿没有的文件时的处理有问题 未释放句柄 你看下吧
[/Quote]
能具体说一下吗?谢谢了。
nanyun2010 2011-06-03
  • 打赏
  • 举报
回复
在连接的时候加上

if(setsockopt(sControl,SOL_SOCKET,SO_SNDTIMEO,&timeout,sizeof(timeout))== -1)
{
perror("setsockopt SO_SNDTIMEO");
net_close(sControl);
return 0;
}

if(setsockopt(sControl,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout))==-1)
{
perror("setsockopt SO_RCVTIMEO");
net_close(sControl);
return 0;
}

出现如下错误,且程序退出。
read: Resource temporarily unavailable
Control socket read failed: Invalid argument
read: Resource temporarily unavailable
Control socket read failed: Resource temporarily unavailable
read: Resource temporarily unavailable
Control socket read failed: Resource temporarily unavailable
read: Resource temporarily unavailable
.
.
.
.
.
Control socket read failed: Resource temporarily unavailable
read: Resource temporarily unavailable
Control socket read failed: Resource temporarily unavailable
read: Resource temporarily unavailable
Control socket read failed: Resource temporarily unavailable
read: Resource temporarily unavailable
Control socket read failed: Resource temporarily unavailable
write: Resource temporarily unavailable
write: Resource temporarily unavailable
write: Resource temporarily unavailable
write: Resource temporarily unavailable
write: Resource temporarily unavailable
write: Resource temporarily unavailable
.
.
.
.
write: Resource temporarily unavailable
write: Resource temporarily unavailable
write: Connection timed out


movie0125 2011-06-03
  • 打赏
  • 举报
回复
你的socket创建是用的阻塞还是非阻塞(默认是阻塞)
ly1213 2011-06-03
  • 打赏
  • 举报
回复
不过我刚解决了 他在拿没有的文件时的处理有问题 未释放句柄 你看下吧
ly1213 2011-06-03
  • 打赏
  • 举报
回复
我也遇到了同样的问题!
nanyun2010 2011-06-02
  • 打赏
  • 举报
回复
是read()的阻塞问题,不知道怎么解决,因为是别人写的函数不想做大的改动,有没有什么简单易行的方法,请各位大虾指教。
nanyun2010 2011-06-02
  • 打赏
  • 举报
回复
求答案,谢谢了。
龙行天下之Sky 2011-06-01
  • 打赏
  • 举报
回复
顶一下,有空再看
nanyun2010 2011-06-01
  • 打赏
  • 举报
回复 1
int ftplib::FtpXfer(const char *localfile, const char *path, ftphandle *nControl, accesstype type, transfermode mode)
{
int l,c;
char *dbuf;
FILE *local = NULL;
ftphandle *nData;
int rv=1; // 3.1-1

if (localfile != NULL)
{
//printf("localfile: -%s-\n", localfile);

//local = fopen(localfile, (typ == ftplib::filewrite) ? "r" : "w");
char ac[3] = " ";
if ((type == ftplib::dir) || (type == ftplib::dirverbose)) { ac[0] = 'w'; ac[1] = '\0'; }
if (type == ftplib::fileread) { ac[0] = 'w'; ac[1] = '\0'; }
if (type == ftplib::filewriteappend) { ac[0] = 'r'; ac[1] = '\0'; }
if (type == ftplib::filereadappend) { ac[0] = 'a'; ac[1] = '\0'; }
if (type == ftplib::filewrite) { ac[0] = 'r'; ac[1] = '\0'; }
if (mode == ftplib::image) ac[1] = 'b';

#ifndef NOLFS
local = fopen64(localfile, ac);
if (type == ftplib::filewriteappend) fseeko64(local,mp_ftphandle->offset,SEEK_SET);
#else
local = fopen(localfile, ac);
if (type == ftplib::filewriteappend) fseek(local,mp_ftphandle->offset,SEEK_SET);
#endif
if (local == NULL)
{
strncpy(nControl->response, strerror(errno), sizeof(nControl->response));
return 0;
}
}
if (local == NULL) local = ((type == ftplib::filewrite)
|| (type == ftplib::filewriteappend)) ? stdin : stdout;
if (!FtpAccess(path, type, mode, nControl, &nData))
{
if (localfile != NULL)
fclose(local);
return 0;
}

dbuf = static_cast<char*>(malloc(FTPLIB_BUFSIZ));
if ((type == ftplib::filewrite) || (type == ftplib::filewriteappend))
{
while ((l = fread(dbuf, 1, FTPLIB_BUFSIZ, local)) > 0)
{
if ((c = FtpWrite(dbuf, l, nData)) < l)
{
printf("short write: passed %d, wrote %d\n", l, c);
rv = 0;
break;
}
}
}
else
{
while ((l = FtpRead(dbuf, FTPLIB_BUFSIZ, nData)) > 0)
{
if (fwrite(dbuf, 1, l, local) <= 0)
{
perror("localfile write");
break;
}
}
}
free(dbuf);
fflush(local);
if (localfile != NULL)
fclose(local);
return FtpClose(nData);
return rv;
}

69,372

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧