当前位置: 技术问答>linux和unix
socket文件传输-linux服务端,window客户端
来源: 互联网 发布时间:2016-10-02
本文导语: 小弟写了一个socket程序:服务端是linux,客户端是window。实现的功能是客户端向服务器发送文件,服务器接受文件后进行处理再发送给客户端。服务器是多线程的,即当有新的socket连接就开启一个新线程 有多个(例...
小弟写了一个socket程序:服务端是linux,客户端是window。实现的功能是客户端向服务器发送文件,服务器接受文件后进行处理再发送给客户端。服务器是多线程的,即当有新的socket连接就开启一个新线程
有多个(例如30个)客户端同时向服务器发送文件,服务器接受后文件都是正确的。可当服务器处理了这些文件后发送给客户端时,有些客户端接收到完整的文件,有些则接受失败,出现10060错误。
疑问:1.客户端在等待服务器发送文件的时候,会不会因为时间太长导致这个连接出现问题。
这个要怎么解决啊?小弟想用异步方式去解决,但具体怎么实现不知道啊?异步方式是通过函数参数去控制一下就可以实现了吗?
哪位大侠可以给个实例参考一下
2.下面是部分代码: 哪位大侠何以看一下这是阻塞方式还是非阻塞方式?
服务端发送文件部分代码
memcpy(sendfile,Attri->name,1020);
if(stat(sendfile,&statbuf)==-1)
{
fprintf(stderr,"Err:Get stat on %s Error:%sn",sendfile,strerror(errno));
printf("%d***Err:Get stat on %s Error:%sn",nums,sendfile);
goto Error2;
}
if(S_ISDIR(statbuf.st_mode))
{
printf("Err: This is directory!n");
goto Error2;
}
if(S_ISREG(statbuf.st_mode))
printf("%d %s File size:%ld bytesn",nums,sendfile,statbuf.st_size);
if(statbuf.st_size != Attri->size)
{
printf("Err: Create %s failed!n",sendfile);
goto Error2;
}
strcpy(buffer,"signed_");
strcat(buffer, sendfile);
strcpy(attri2->name,buffer);
bzero(buffer,BUFLEN);
attri2->size = statbuf.st_size;
md5_process(attri2->name,attri2->md5);
attri2->md5[16] = '';
memcpy(buffer,(char *)attri2,1024);
if(-1==send(new_server_socket,buffer,BUFLEN,0))
{
printf("send failed:n");
printf(" Error:%sn",strerror(errno));
goto Error2;
}
bzero(buffer,BUFLEN);
bytes_read=-2;
printf("Create send file %s:n",sendfile);
if((to_fd = open(sendfile,O_RDONLY))==-1)
{
printf("%d***send client:open %s failed!n",nums,sendfile);
goto Error2;
//exit(1);
}
inte = attri2->size/1024;
residual = attri2->size%1024;
if(residual>0) inte +=1;
k=0;
printf("%d Send signed file %s: size=%dn",nums,sendfile,attri2->size);
tchl=0;
for(k=0;kname);
goto Error1;
}
else if(bytes_read>0)
{
if(-1==send(new_server_socket,buffer,bytes_read,0))
{
printf("Err: send failed; k=%d Get stat on %s Error:%sn",k,sendfile,strerror(errno));
goto Error1;
}
bzero(buffer,BUFLEN);
tchl += bytes_read;
}
}
printf("%d Send signed file %s ok size:%d n",n
客户端接收文件部分代码
#ifdef RECE
//begin receive file from sign server!
printf("************ begin receive file from sign server! ************n");
//length = recv(client_socket,buffer,BUFFER_SIZE,0);
nRecv=0;
while(nRecv!=sizeof(buffer))
{
length = recv(client_socket,buffer+nRecv,1024-nRecv,0);
if(length == SOCKET_ERROR || length == 0)
{
id = WSAGetLastError();
switch (id)
{
case WSANOTINITIALISED: printf("Err:not initializedn"); break;
case WSASYSNOTREADY: printf("Err:sub sys not readyn"); break;
case WSAHOST_NOT_FOUND: printf("Err:name server not foundn"); break;
case WSATRY_AGAIN: printf("Err:server failn"); break;
case WSANO_RECOVERY: printf("Err:no recoveryn"); break;
case WSAEINPROGRESS: printf("Err:socket blocked by other progn"); break;
case WSANO_DATA: printf("Err:no data recordn"); break;
case WSAEINTR: printf("Err:blocking call canciledn"); break;
case WSAEPROCLIM: printf("Err:limit exceededn");break;
case WSAEFAULT: printf("Err:lpWSAData in startup not validn");break;
default: printf("Err:unknown error id = %dn",id); break;
};
printf("Err: Receive first info from server failed!n");
free(Attri2);
closesocket(client_socket);
WSACleanup();
return NULL;
}
nRecv +=length;
printf("Info:nRecv=%d ",nRecv);
}
memcpy((char*)Attri2,buffer,1024);
printf("%d File name: %sn",nums,Attri2->name);
printf("%d File size: %ld bytesn",nums,Attri2->size);
memset(buffer,0,BUFFER_SIZE);
printf("%d Create file %s!n",nums,Attri2->name);
if((to_fd=fopen(Attri2->name,"wb+"))==NULL)
{
fprintf(stderr,"Open %s Error:%sn",Attri2->name,strerror(errno));
printf("Err: %d Open %s Error:%sn",nums,Attri2->name);
free(Attri2);
closesocket(client_socket);
WSACleanup();
return NULL;
}
inte=0;
residual=0;
inte = Attri2->size/1024;
residual = Attri2->size%1024;
k=0;
tems =0;
if(residual>0) inte +=1;
bytes_read=0;
tems = Attri2->size;
printf("Begin to receive file from server!n");
for(k=0;k=1024)
{
while(nRecv!=sizeof(buffer))
{
length = recv(client_socket,buffer+nRecv,1024-nRecv,0);
if(length == SOCKET_ERROR || length == 0)
{
id = WSAGetLastError();
switch (id)
{
case WSANOTINITIALISED: printf("Err:not initializedn"); break;
case WSASYSNOTREADY: printf("Err:sub sys not readyn"); break;
case WSAHOST_NOT_FOUND: printf("Err:name server not foundn"); break;
case WSATRY_AGAIN: printf("Err:server failn"); break;
case WSANO_RECOVERY: printf("Err:no recoveryn"); break;
case WSAEINPROGRESS: printf("Err:socket blocked by other progn"); break;
case WSANO_DATA: printf("Err:no data recordn"); break;
case WSAEINTR: printf("Err:blocking call canciledn"); break;
case WSAEPROCLIM: printf("Err:limit exceededn");
case WSAEFAULT: printf("Err:lpWSAData in startup not validn");
default: printf("Err:unknown error id = %dn",id); break;
};
printf("Err: filename:%s 1024receive error.\n",Attri2->name);
fclose(to_fd);
free(Attri2);
closesocket(client_socket);
WSACleanup();
return NULL;
}
nRecv +=length;
}
notsame =0;
bytes_read = BUFFER_SIZE;
tems -=1024;
fwrite(buffer,bytes_read,1,to_fd);
}
else
{
while(nRecv!=residual)
{
length = recv(client_socket,buffer+nRecv,residual-nRecv,0);
if(length == SOCKET_ERROR || length == 0)
{
id = WSAGetLastError();
switch (id)
{
case WSANOTINITIALISED: printf("not initializedn"); break;
case WSASYSNOTREADY: printf("sub sys not readyn"); break;
case WSAHOST_NOT_FOUND: printf("name server not foundn"); break;
case WSATRY_AGAIN: printf("server failn"); break;
case WSANO_RECOVERY: printf("no recoveryn"); break;
case WSAEINPROGRESS: printf("socket blocked by other progn"); break;
case WSANO_DATA: printf("no data recordn"); break;
case WSAEINTR: printf("blocking call canciledn"); break;
case WSAEPROCLIM: printf("limit exceededn");
case WSAEFAULT: printf("lpWSAData in startup not validn");
default: printf("unknown error id = %dn",id); break;
};
printf("Err: file name:%s length %d rest receive error.n",Attri2->name,length);
fclose(to_fd);
free(Attri2);
closesocket(client_socket);
WSACleanup();
return NULL;
}
nRecv +=length;
}
bytes_read = residual;
int oo = fwrite(buffer,bytes_read,1,to_fd);
printf(" rec tems:%dn",tems);
}
memset(buffer,0,BUFFER_SIZE);
}
fclose(to_fd);
md5_process(Attri2->name,md5check);
md5check[16] = '';
if(strcmp(md5check,Attri2->md5)!=0)
{
printf("Err: Receive file %s failed!n",Attri2->md5);
remove(Attri2->name);
}
else
{
printf("Info: Receivefile %s successfully!n",Attri2->name);
}
有多个(例如30个)客户端同时向服务器发送文件,服务器接受后文件都是正确的。可当服务器处理了这些文件后发送给客户端时,有些客户端接收到完整的文件,有些则接受失败,出现10060错误。
疑问:1.客户端在等待服务器发送文件的时候,会不会因为时间太长导致这个连接出现问题。
这个要怎么解决啊?小弟想用异步方式去解决,但具体怎么实现不知道啊?异步方式是通过函数参数去控制一下就可以实现了吗?
哪位大侠可以给个实例参考一下
2.下面是部分代码: 哪位大侠何以看一下这是阻塞方式还是非阻塞方式?
服务端发送文件部分代码
memcpy(sendfile,Attri->name,1020);
if(stat(sendfile,&statbuf)==-1)
{
fprintf(stderr,"Err:Get stat on %s Error:%sn",sendfile,strerror(errno));
printf("%d***Err:Get stat on %s Error:%sn",nums,sendfile);
goto Error2;
}
if(S_ISDIR(statbuf.st_mode))
{
printf("Err: This is directory!n");
goto Error2;
}
if(S_ISREG(statbuf.st_mode))
printf("%d %s File size:%ld bytesn",nums,sendfile,statbuf.st_size);
if(statbuf.st_size != Attri->size)
{
printf("Err: Create %s failed!n",sendfile);
goto Error2;
}
strcpy(buffer,"signed_");
strcat(buffer, sendfile);
strcpy(attri2->name,buffer);
bzero(buffer,BUFLEN);
attri2->size = statbuf.st_size;
md5_process(attri2->name,attri2->md5);
attri2->md5[16] = '';
memcpy(buffer,(char *)attri2,1024);
if(-1==send(new_server_socket,buffer,BUFLEN,0))
{
printf("send failed:n");
printf(" Error:%sn",strerror(errno));
goto Error2;
}
bzero(buffer,BUFLEN);
bytes_read=-2;
printf("Create send file %s:n",sendfile);
if((to_fd = open(sendfile,O_RDONLY))==-1)
{
printf("%d***send client:open %s failed!n",nums,sendfile);
goto Error2;
//exit(1);
}
inte = attri2->size/1024;
residual = attri2->size%1024;
if(residual>0) inte +=1;
k=0;
printf("%d Send signed file %s: size=%dn",nums,sendfile,attri2->size);
tchl=0;
for(k=0;kname);
goto Error1;
}
else if(bytes_read>0)
{
if(-1==send(new_server_socket,buffer,bytes_read,0))
{
printf("Err: send failed; k=%d Get stat on %s Error:%sn",k,sendfile,strerror(errno));
goto Error1;
}
bzero(buffer,BUFLEN);
tchl += bytes_read;
}
}
printf("%d Send signed file %s ok size:%d n",n
客户端接收文件部分代码
#ifdef RECE
//begin receive file from sign server!
printf("************ begin receive file from sign server! ************n");
//length = recv(client_socket,buffer,BUFFER_SIZE,0);
nRecv=0;
while(nRecv!=sizeof(buffer))
{
length = recv(client_socket,buffer+nRecv,1024-nRecv,0);
if(length == SOCKET_ERROR || length == 0)
{
id = WSAGetLastError();
switch (id)
{
case WSANOTINITIALISED: printf("Err:not initializedn"); break;
case WSASYSNOTREADY: printf("Err:sub sys not readyn"); break;
case WSAHOST_NOT_FOUND: printf("Err:name server not foundn"); break;
case WSATRY_AGAIN: printf("Err:server failn"); break;
case WSANO_RECOVERY: printf("Err:no recoveryn"); break;
case WSAEINPROGRESS: printf("Err:socket blocked by other progn"); break;
case WSANO_DATA: printf("Err:no data recordn"); break;
case WSAEINTR: printf("Err:blocking call canciledn"); break;
case WSAEPROCLIM: printf("Err:limit exceededn");break;
case WSAEFAULT: printf("Err:lpWSAData in startup not validn");break;
default: printf("Err:unknown error id = %dn",id); break;
};
printf("Err: Receive first info from server failed!n");
free(Attri2);
closesocket(client_socket);
WSACleanup();
return NULL;
}
nRecv +=length;
printf("Info:nRecv=%d ",nRecv);
}
memcpy((char*)Attri2,buffer,1024);
printf("%d File name: %sn",nums,Attri2->name);
printf("%d File size: %ld bytesn",nums,Attri2->size);
memset(buffer,0,BUFFER_SIZE);
printf("%d Create file %s!n",nums,Attri2->name);
if((to_fd=fopen(Attri2->name,"wb+"))==NULL)
{
fprintf(stderr,"Open %s Error:%sn",Attri2->name,strerror(errno));
printf("Err: %d Open %s Error:%sn",nums,Attri2->name);
free(Attri2);
closesocket(client_socket);
WSACleanup();
return NULL;
}
inte=0;
residual=0;
inte = Attri2->size/1024;
residual = Attri2->size%1024;
k=0;
tems =0;
if(residual>0) inte +=1;
bytes_read=0;
tems = Attri2->size;
printf("Begin to receive file from server!n");
for(k=0;k=1024)
{
while(nRecv!=sizeof(buffer))
{
length = recv(client_socket,buffer+nRecv,1024-nRecv,0);
if(length == SOCKET_ERROR || length == 0)
{
id = WSAGetLastError();
switch (id)
{
case WSANOTINITIALISED: printf("Err:not initializedn"); break;
case WSASYSNOTREADY: printf("Err:sub sys not readyn"); break;
case WSAHOST_NOT_FOUND: printf("Err:name server not foundn"); break;
case WSATRY_AGAIN: printf("Err:server failn"); break;
case WSANO_RECOVERY: printf("Err:no recoveryn"); break;
case WSAEINPROGRESS: printf("Err:socket blocked by other progn"); break;
case WSANO_DATA: printf("Err:no data recordn"); break;
case WSAEINTR: printf("Err:blocking call canciledn"); break;
case WSAEPROCLIM: printf("Err:limit exceededn");
case WSAEFAULT: printf("Err:lpWSAData in startup not validn");
default: printf("Err:unknown error id = %dn",id); break;
};
printf("Err: filename:%s 1024receive error.\n",Attri2->name);
fclose(to_fd);
free(Attri2);
closesocket(client_socket);
WSACleanup();
return NULL;
}
nRecv +=length;
}
notsame =0;
bytes_read = BUFFER_SIZE;
tems -=1024;
fwrite(buffer,bytes_read,1,to_fd);
}
else
{
while(nRecv!=residual)
{
length = recv(client_socket,buffer+nRecv,residual-nRecv,0);
if(length == SOCKET_ERROR || length == 0)
{
id = WSAGetLastError();
switch (id)
{
case WSANOTINITIALISED: printf("not initializedn"); break;
case WSASYSNOTREADY: printf("sub sys not readyn"); break;
case WSAHOST_NOT_FOUND: printf("name server not foundn"); break;
case WSATRY_AGAIN: printf("server failn"); break;
case WSANO_RECOVERY: printf("no recoveryn"); break;
case WSAEINPROGRESS: printf("socket blocked by other progn"); break;
case WSANO_DATA: printf("no data recordn"); break;
case WSAEINTR: printf("blocking call canciledn"); break;
case WSAEPROCLIM: printf("limit exceededn");
case WSAEFAULT: printf("lpWSAData in startup not validn");
default: printf("unknown error id = %dn",id); break;
};
printf("Err: file name:%s length %d rest receive error.n",Attri2->name,length);
fclose(to_fd);
free(Attri2);
closesocket(client_socket);
WSACleanup();
return NULL;
}
nRecv +=length;
}
bytes_read = residual;
int oo = fwrite(buffer,bytes_read,1,to_fd);
printf(" rec tems:%dn",tems);
}
memset(buffer,0,BUFFER_SIZE);
}
fclose(to_fd);
md5_process(Attri2->name,md5check);
md5check[16] = '';
if(strcmp(md5check,Attri2->md5)!=0)
{
printf("Err: Receive file %s failed!n",Attri2->md5);
remove(Attri2->name);
}
else
{
printf("Info: Receivefile %s successfully!n",Attri2->name);
}
|
“length = recv(client_socket,buffer+nRecv,1024-nRecv,0);
if(length == SOCKET_ERROR || length == 0)”
length == 0不是错误吧,是因为服务器关闭了连接。
if(length == SOCKET_ERROR || length == 0)”
length == 0不是错误吧,是因为服务器关闭了连接。
|
可能是中断,查看errno看看是否是EINTR
|
把程序格式弄好一点儿,看着好别扭啊。
|
程序没注释又不整齐。。。
|
唉,手机翻看你的程序更累