error 10061 socket 帮忙。谢谢大家!
我写了一个程序。一个客户端一个服务器端。发送文件从客户端到服务器端。但是只要客户端connect。出现 10061错误。但是如果 在同个电脑上运行就没有这个问题,在网上找了不少。也不知道怎么解决。希望那位高手看一下。
客户端程序:
sockaddr_in Clientd_local;
SOCKET Clientd_socket;
int ret;
//initialize client address
Clientd_local.sin_family=AF_INET;
Clientd_local.sin_port=htons(5000);
Clientd_local.sin_addr.S_un.S_addr=inet_addr(download_IP);
Clientd_socket=socket(AF_INET,SOCK_STREAM,0);
printf("download ip: %s\n",download_IP);
//connect server
printf(" C O N N E C T T O S E R V E R \n");
ret = connect(Clientd_socket,(LPSOCKADDR)&Clientd_local,sizeof(Clientd_local));
//error message
if ( ret < 0 ){
printf("Connect Server error......\n");
DWORD error;
error = GetLastError();
printf( "error windows error code :%d\n",error ); //pop up error message "10061"
closesocket(Clientd_socket);
}else{
printf("Sucess connect to Server......\n");
}
CString file_name;
int send_re =0;
CFile send_file;
recv(Clientd_socket,Receive_data,100,0);
Download_name = Receive_data;
Download_name.TrimLeft();
Download_name.TrimRight();
file_name = zmfile[0].name;
if ( Download_name == file_name ){
send_re = 1;
}
file_name = zmfile[1].name;
if ( Download_name == file_name ){
send_re = 2;
}
file_name = zmfile[2].name;
if ( Download_name == file_name ){
send_re = 3;
}
/*open download file ready for download
*/
if ( send_re > 0 ){
if (send_file.Open(zmfile[send_re-1].path,CFile::modeRead | CFile::typeBinary|CFile::shareDenyNone)){
printf("open file1 succeed!\n");
}else{
printf("open file1 failed!\n");
}
}
WIN32_FIND_DATA FindFileData;
FindClose(FindFirstFile(zmfile[send_re-1].path,&FindFileData));
send(Clientd_socket,(char*)&FindFileData,sizeof(WIN32_FIND_DATA),0);
UINT dwRead = 0;
byte* data;
//printf("send file path:\n");
while( dwRead < FindFileData.nFileSizeLow){
data = new byte[2048];
UINT dw=send_file.Read(data, 2048);
send(Clientd_socket,(char*)data,2048,0);
dwRead+=dw;
delete [] data;
}
//close file and socket
send_file.Close();
closesocket(Clientd_socket);
WSACleanup();
服务器端:
void Start_Server(){
//struct of infomation about SOCKET
SOCKADDR_IN Peerd_local;
//set new SOCKET
SOCKET Serverd_socket;
char Send_data[100];
int rc=0;
int number_p;
//address
Peerd_local.sin_family = AF_INET;
//port number
Peerd_local.sin_port=htons(5000);
//IP address
Peerd_local.sin_addr.S_un.S_addr=INADDR_ANY;
/* create socket
AF_INET: IPv4 protocol
SOCK_STREAM:Bite mode
*/
Serverd_socket = socket(AF_INET,SOCK_STREAM,0);
DWORD aaaa;
aaaa = GetLastError();
printf( " set aaaa windows error code :%d\n",aaaa );
/* if socket error
set error message
*/
if ( Serverd_socket == INVALID_SOCKET ){
printf("Create SOCKET error......!\n");
}else{
printf(" D O W N L O A D S E R V E R \n");
}
//bind server
rc = bind(Serverd_socket,(LPSOCKADDR)&Peerd_local,sizeof(Peerd_local));
//listen client
number_p = listen(Serverd_socket,100);
//build new SOCKET
SOCKET s1;
//SOCKADDR_IN Peerd_local_1;
//int len = sizeof(sockaddr);
s1=accept(Serverd_socket,NULL,NULL);
int timeout=100;
rc=setsockopt(s1,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,sizeof(timeout));
//set download buf
int buf=10240;
rc=setsockopt(s1,SOL_SOCKET,SO_RCVBUF,(char*)&buf,sizeof(int));
DWORD sss;
sss = GetLastError();
printf( "Error windows number :%d\n",sss );
//send download file to client
memcpy(Send_data,Download_com,100);
send(s1,(char*)Send_data,100,0);
//download file from client
WIN32_FIND_DATA fd;
//receive download all of infomation
recv(s1,(char*)&fd,sizeof(WIN32_FIND_DATA),0);
//save to c drive
CString m_ProDir="c:\\";
CFile destFile;
CString destFile_path = m_ProDir+fd.cFileName;
destFile_path.TrimLeft();
destFile_path.TrimRight();
if ( destFile.Open((LPCTSTR)destFile_path, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary)){
UINT dwRead = 0;
//download data
while( dwRead < fd.nFileSizeLow )
{
byte* data = new byte[2048];
memset(data,0,2048);
UINT dw=recv(s1,(char*)data,2048,0);
destFile.Write(data,dw);
dwRead+=dw;
delete [] data;
}
//set download file propertities
SetFileTime((HANDLE)destFile.m_hFile,&fd.ftCreationTime,&fd.ftLastAccessTime,&fd.ftLastWriteTime);
SetFileAttributes(m_ProDir+fd.cFileName,fd.dwFileAttributes);
printf("Download file success please close screen!\n");
}else{
printf("Download file failed!\n");
}
//close socket and file
//Sleep(10000);
destFile.Close();
closesocket (s1);
closesocket (Serverd_socket);
WSACleanup();
}
大家帮我看看。十分感谢!