疑问,send函数出现illegal seek错误,但是发送接收完全正确。(有代码)

sherlock_lai 2009-04-20 09:27:27
这是一段简单的用TCP发送文件的程序。客户端接收到的数据完全正确,通过抓包也没什么问题,但是在发送端send函数会出现illegal seek错误,检查了几遍也没检查出原因,请大家帮忙看看。
int main(char argc,char **argv)
{
struct stat fileStat1;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
int sock_tcp;
int sock_accept;
int fd_file1;
int sock_flag;
int len;
int count;
int send_len;
int size;
int i,j;
char buff1[20000];
// char file_buff1[100000];
unsigned char *file_buff1;

memset(&fileStat1,0,sizeof(fileStat1));
memset(&my_addr,0,sizeof(my_addr));
memset(&remote_addr,0,sizeof(remote_addr));
memset(buff1,0,20000);

my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(10000);
inet_aton("192.168.0.171",&my_addr.sin_addr);

remote_addr.sin_family=AF_INET;
// remote_addr.sin_port=htons(10000);
// inet_aton("192.168.0.56",&remote_addr.sin_addr);

if((sock_tcp=socket(AF_INET,SOCK_STREAM,0))<0)
perror("socket tcp");

sock_flag=1;
if(setsockopt(sock_tcp,SOL_SOCKET,SO_REUSEADDR,&sock_flag,sizeof(sock_flag))<0)
perror("setsockopt");
if(bind(sock_tcp,(struct sockaddr *)&my_addr,sizeof(my_addr))<0)
perror("bind");

if(listen(sock_tcp,10)<0)
perror("listen");

if((sock_accept=accept(sock_tcp,(struct sockaddr *)&remote_addr,&len))<0)
perror("accept");

printf("Server: a connection build\n");

if(stat("ap1",&fileStat1)<0)
perror("stat");

file_buff1=malloc(fileStat1.st_size);
if((fd_file1=open("ap1",O_RDWR))<0)
perror("open");

read(fd_file1,file_buff1,fileStat1.st_size);
close(fd_file1);

len=fileStat1.st_size;
printf("the length of file=%d\n",len);

send_len=0;

count=0;
while(len>1460)
{
if(send(sock_accept,&file_buff1[send_len],MAX_TCP_BYTES,0))
{
printf("%d\n",count);
perror("send");
}
len-=1460;
send_len+=1460;
count++;
}

if(send(sock_accept,&buff1[send_len],len,0)) //每次send都要出现 illegal seek的错误
perror("send");

close(sock_accept);
close(sock_tcp);
free(file_buff1);
return 0;
}
...全文
444 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
rapide 2012-03-14
  • 打赏
  • 举报
回复
if(send(sock_accept,&buff1[send_len],len,0)) //每次send都要出现 illegal seek的错误
perror("send");


send调用成功发送数据后返回值大于0,所以条件语句为真,所以perror被调用;
但perror调用使用的errno只有当上一次调用出错后才有意义,所以。。。

运行一下例程就会明白的。


例程:

void main()
{
while(1)
perror();
}


goodstudy_linux 2011-10-27
  • 打赏
  • 举报
回复
我也遇到同样的问题
holtwang 2011-05-26
  • 打赏
  • 举报
回复
多年以后又遇到了相同的问题
sherlock_lai 2009-04-21
  • 打赏
  • 举报
回复
结贴了,可能这根本不是问题
sherlock_lai 2009-04-21
  • 打赏
  • 举报
回复
ap1 不大的,一个简单的hello world程序,传到客户端去执行,客户端接收数据并执行也没问题。
sherlock_lai 2009-04-20
  • 打赏
  • 举报
回复
第一次send返回success,之后的send无论发送什么都返回illegal seek,但是接收方接收到的数据是完全正确的
sherlock_lai 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 once_and_again 的回复:]
send_len=0 ; ? 初始化的问题?
[/Quote]
send_len是表示发送buff的起始位置,第一次发送时,send_len=0
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sherlock_lai 的回复:]
第一次send返回success,之后的send无论发送什么都返回illegal seek,但是接收方接收到的数据是完全正确的
[/Quote]

我也遇到过,是客户端断开重连,此后向客户端每发一次数据都会包类似的一个警告.应该是某些状态位清零的问题吧.但是数据一直都正常,别太在意.
once_and_again 2009-04-20
  • 打赏
  • 举报
回复
send_len=0 ; ? 初始化的问题?

sherlock_lai 2009-04-20
  • 打赏
  • 举报
回复
如果按流式操作,也就是只send一次,又不会出现错误...太奇怪了
morris88 2009-04-20
  • 打赏
  • 举报
回复
文件 ap1 有多大...
sherlock_lai 2009-04-20
  • 打赏
  • 举报
回复
以前没关注过send的返回值,难道返回illegal seek根本就不是错误?

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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