error 10061 socket 帮忙。谢谢大家!

huaishui 2007-11-15 01:23:37
我写了一个程序。一个客户端一个服务器端。发送文件从客户端到服务器端。但是只要客户端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();
}

大家帮我看看。十分感谢!
...全文
6238 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
简单的工作室 2008-12-02
  • 打赏
  • 举报
回复
我测试了下。而后主控监听程序没有打开,只运行TIDCLIENT CONNET过去的话,就有这个错误的~~
huaishui 2007-11-17
  • 打赏
  • 举报
回复
恩.楼上说得有道理.不过.为什么如果把server和client都放到同一台电脑里就可以执行.不会有错误.但是只要放到不同电脑里才会出现这个10061的错误.楼上的给解释一下.哈!
stivenjia 2007-11-16
  • 打赏
  • 举报
回复
s1=accept(Serverd_socket,NULL,NULL);
在这里
计算机运行时间N快也许当你启动之后瞬间就执行过这里也就说当你的Client Connect Server的时候你的Server不知道在干什么呢?也许在Listen到了但是你没有accept它,也许执行一次后就永不回头导致了无法accept
你的明白?
lllxy 2007-11-16
  • 打赏
  • 举报
回复
估计是xp防火墙的问题
huaishui 2007-11-15
  • 打赏
  • 举报
回复
我关闭了防火墙.杀毒软件都是一样的.但是如果把server合client都放到一个电脑里操作.这种问题就没有.可以连接.也可以发送和接收.谁还能在看看.server端那里有错?????????
mr.zhoux 2007-11-15
  • 打赏
  • 举报
回复
估计是xp防火墙吧...或者其他的- -~"
ccpaishi 2007-11-15
  • 打赏
  • 举报
回复
10061一般是服务器没有开设服务吧。
会不会是端口写错了还是其他一些原因,找找看。看看服务器端代码有没有什么问题
ouyh12345 2007-11-15
  • 打赏
  • 举报
回复
10061:由于目标机器积极拒绝,无法连接。

--

开了防火墙?
iyranly 2007-11-15
  • 打赏
  • 举报
回复
你在connect时GetLastError是什么?意思说什么出错?没有找到服务器端还是什么?

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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