I want to build a server waiting for clients broadcast
msg. Codes as the following: SOCKET s = socket(AF_INET, SOCK_DGRAM ,
IPPROTO_UDP ); if (s == INVALID_SOCKET){
err = WSAGetLastError();
CString str;
str.Format("Socket error: %d",err);
AfxMessageBox(str);
} struct sockaddr_in addrs={0};
addrs.sin_family = AF_INET;
addrs.sin_port = htons(9000);
addrs.sin_addr.s_addr = inet_addr
("255.255.255.255");
if(bind(s, (sockaddr*)&addrs, sizeof(addrs))
==SOCKET_ERROR){
err = WSAGetLastError();
CString str;
str.Format("Error to bind: %d",err);
AfxMessageBox(str);
}
int b = TRUE;
if (setsockopt(s,SOL_SOCKET, SO_BROADCAST, (const
char *)&b, sizeof(int))==SOCKET_ERROR){
err = WSAGetLastError();
CString str;
str.Format("Error to set opt: %d",err);
AfxMessageBox(str);
} char arch[ 1024 ] = "";
sockaddr_in addr;
int addrlen = sizeof(sockaddr_in); while(!g_bStop){
if (connect(s, (sockaddr*)&addr, sizeof
(addr)) == SOCKET_ERROR){
err = WSAGetLastError();
CString str;
str.Format("Connect err: %d",err);
AfxMessageBox(str);
return 2;
}
//receive the msg and check it
if (recvfrom(s, arch, sizeof(arch),
MSG_PEEK,(sockaddr*)&addr, &addrlen) == SOCKET_ERROR){
err = WSAGetLastError();
CString str;
str.Format("Connect err: %d",err);
AfxMessageBox(str);
return 3;
}
////do something//// Sleep(500);
}
closesocket( s );Results are 2 warning dialogs:
1. Error to bind: 10049
2. Connect err: 10047Can anybody know how to correct it?
msg. Codes as the following: SOCKET s = socket(AF_INET, SOCK_DGRAM ,
IPPROTO_UDP ); if (s == INVALID_SOCKET){
err = WSAGetLastError();
CString str;
str.Format("Socket error: %d",err);
AfxMessageBox(str);
} struct sockaddr_in addrs={0};
addrs.sin_family = AF_INET;
addrs.sin_port = htons(9000);
addrs.sin_addr.s_addr = inet_addr
("255.255.255.255");
if(bind(s, (sockaddr*)&addrs, sizeof(addrs))
==SOCKET_ERROR){
err = WSAGetLastError();
CString str;
str.Format("Error to bind: %d",err);
AfxMessageBox(str);
}
int b = TRUE;
if (setsockopt(s,SOL_SOCKET, SO_BROADCAST, (const
char *)&b, sizeof(int))==SOCKET_ERROR){
err = WSAGetLastError();
CString str;
str.Format("Error to set opt: %d",err);
AfxMessageBox(str);
} char arch[ 1024 ] = "";
sockaddr_in addr;
int addrlen = sizeof(sockaddr_in); while(!g_bStop){
if (connect(s, (sockaddr*)&addr, sizeof
(addr)) == SOCKET_ERROR){
err = WSAGetLastError();
CString str;
str.Format("Connect err: %d",err);
AfxMessageBox(str);
return 2;
}
//receive the msg and check it
if (recvfrom(s, arch, sizeof(arch),
MSG_PEEK,(sockaddr*)&addr, &addrlen) == SOCKET_ERROR){
err = WSAGetLastError();
CString str;
str.Format("Connect err: %d",err);
AfxMessageBox(str);
return 3;
}
////do something//// Sleep(500);
}
closesocket( s );Results are 2 warning dialogs:
1. Error to bind: 10049
2. Connect err: 10047Can anybody know how to correct it?
解决方案 »
- WriteFile需不需要SetFilePointer?
- 急问(关于进度条显示问题)
- 用Image类创建一个对象时出错,高手帮看一下!
- 大家好,再见
- 有谁知道网上的那些html->chm的编译、反编译工具是如何实现的?
- 黑良心的,没人性的网站 http://www.ok530.com 自动给QQ发送添加的信息.
- 关于VC++工具使用的一个问题
- 有没有大连人啊!我要去这个公司面试!!!多谢,100分送上!
- 迷惑数天的问题??恳请各位高手指教!!!如何控制cformview对话框中滚动条的问题???
- CBitmap的问题
- 用CStdioFile类依次读文件的每一行的时候,怎么判断文件结束啊!谢谢指教!
- 请教:CDatabase类和CRecordset类在功能上和其对象在内存中的状态有什么区别?
>>addrs.sin_addr.s_addr = inet_addr("255.255.255.255");
2.
this problem may be comes form the problem 1. however, after you change the problem 1, this problem is still exist. let me know
>> The 2nd problem still there.2. addrs.sin_addr.s_addr = htonl(INADDR_BROADCAST);
>> 2 problems still.Might you give more details, thanks.
1. Server keep waiting for msg (recvfrom), and answer the client (sendto)
2. Client broadcast msg (sendto), and quit after getting answer from server (recvfrom)The problems are:
1. client bind error: 10048 (WSAEADDRINUSE)
2. client stop at recvfrom.Please refer to the following codes, easy to understand though a bit long: ---SERVER SITE---
WSADATA wsaData;
int err;
CString strErr;
err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
SOCKET s = socket(AF_INET, SOCK_DGRAM , IPPROTO_UDP );
if (s == INVALID_SOCKET){
err = WSAGetLastError();
strErr.Format("Socket error: %d",err);
AfxMessageBox(strErr);
} struct sockaddr_in addrs={0};
addrs.sin_family = AF_INET;
addrs.sin_port = htons(9000);
addrs.sin_addr.s_addr = INADDR_ANY; if(bind(s, (sockaddr*)&addrs, sizeof(addrs))==SOCKET_ERROR){
err = WSAGetLastError();
strErr.Format("Error to bind: %d",err);
AfxMessageBox(strErr);
}
int b = TRUE;
if (setsockopt(s,SOL_SOCKET, SO_BROADCAST, (const char *)&b, sizeof(int))==SOCKET_ERROR){
err = WSAGetLastError();
strErr.Format("Error to set opt: %d",err);
AfxMessageBox(strErr);
} char arch[ 1024 ] = "";
sockaddr_in addr;
int addrlen = sizeof(sockaddr_in); while(1){
memset(&addr, 0, sizeof(addr));
memset(arch, 0, sizeof(arch));
//receive the msg and check it
if (recvfrom(s, arch, sizeof(arch), 0,(sockaddr*)&addr, &addrlen) == SOCKET_ERROR){
err = WSAGetLastError();
strErr.Format("recvfrom err: %d",err);
AfxMessageBox(strErr);
continue;
}
//check if client broadcasting
if (strstr(arch,"Client calling") != NULL ){
//answer to client by broadcast
addr.sin_family = AF_INET;
addr.sin_port = htons(9000);
addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
if (sendto(s, "Server Ready", 13, 0, (sockaddr*)&addr, sizeof(addr))== SOCKET_ERROR){
err = WSAGetLastError();
strErr.Format("Sendto err: %d",err);
AfxMessageBox(strErr);
}
}
Sleep(500);
}
closesocket( s );
WSACleanup();---CLIENT SITE---
WSADATA wsaData;
int err;
CString strErr;
err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
SOCKET s = socket(AF_INET, SOCK_DGRAM , IPPROTO_UDP );
if (s == INVALID_SOCKET){
err = WSAGetLastError();
strErr.Format("Socket error: %d",err);
AfxMessageBox(strErr);
} struct sockaddr_in addr={0};
addr.sin_family = AF_INET;
addr.sin_port = htons(9000);
addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
if(bind(s, (sockaddr*)&addr, sizeof(addr))==SOCKET_ERROR){
err = WSAGetLastError();
strErr.Format("Error to bind: %d",err);
AfxMessageBox(strErr);
} int b = TRUE;
if (setsockopt(s,SOL_SOCKET, SO_BROADCAST, (const char *)&b, sizeof(int))==SOCKET_ERROR){
err = WSAGetLastError();
strErr.Format("Error to set opt: %d",err);
AfxMessageBox(strErr);
} char arch[ 1024 ] = "";
sockaddr_in addrs;
int addrlen = sizeof(sockaddr_in); while(!g_bCloseMe){
if (connect(s, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR){
err = WSAGetLastError();
strErr.Format("Connect err: %d",err);
AfxMessageBox(strErr);
break;
}
addrs.sin_family = AF_INET;
addrs.sin_port = htons(9000);
addrs.sin_addr.s_addr = htonl(INADDR_BROADCAST); if (sendto(s, "Client calling", 15, 0, (sockaddr*)&addrs, sizeof(addrs))== SOCKET_ERROR){
err = WSAGetLastError();
strErr.Format("Sendto err: %d",err);
AfxMessageBox(strErr);
continue;
}
AfxMessageBox("Client sent");
memset(&addr, 0, sizeof(addr));
memset(arch, 0, sizeof(arch));
//receive the msg and check it
if (recvfrom(s, arch, sizeof(arch), 0,(sockaddr*)&addr, &addrlen) == SOCKET_ERROR){ ////BLOCK HERE
err = WSAGetLastError();
strErr.Format("Recvfrom err: %d",err);
AfxMessageBox(strErr);
continue;
}
AfxMessageBox("Client received"); //server ready flag
if (strstr(arch, "Server Ready") != NULL){
//server found
break;
}else AfxMessageBox(arch);
Sleep(500);
}
closesocket( s );
WSACleanup();
1.
the information below is coming from msdn.
WSAEADDRINUSE (10048)
"if an application attempts to bind a socket to an IP address/port that has already been used for an existing socket. "Are u testing the Sever & Client on the same PC. If u just did waht i said, when u use bind() in Client is report the error. Otherwise, something goes to strange.2.
this problem because in the client sendto() doesnt work.
u can do something like this to solve it out.
nSend = sendto(...); // to see if the nSend change
Thread of Answer to client | Thread of looking up server
socket(AF_INET, | socket: same as left column
SOCK_DGRAM, |
IPPROTO_UDP ); |
setsockopt: SOL_SOCKET | setsockopt: SOL_SOCKET
SO_REUSEADDR | SO_BROADCAST
bind: Port 9000 | connect: Port 9000
IP INADDR_ANY | IP INADDR_BROADCAST
setsockopt: SOL_SOCKET | LOOP until find server
SO_BROADCAST | sendto: Port 9000
LOOP forever | IP INADDR_BROADCAST
recvfrom: (waiting data) | recvfrom: (waiting data)
sendto: Port 9000 | ----BUT BLOCK HERE----
IP INADDR_BROADCAST | Sleep(500)
Sleep(500) | END LOOP
END LOOP | closesocket
closesocket |As shown above, the client blocks at recvfrom, even the data it has just sent out is received by server and then server send out answer data again and also received by server itself. In a word, the server can receive everything it sends or the client sends, but client receives nothing.Then I modify the looking up thread of client and divide it into 2 threads, one for sendto and one for recvfrom: Thread of sendto in client | Thread of recvfrom in client
LOOP until found server | Spawn sendto thread
socket(AF_INET, | LOOP until found server
SOCK_DGRAM, | socket(AF_INET, SOCK_DGRAM,
IPPROTO_UDP ); | IPPROTO_UDP);
setsockopt: SOL_SOCKET | setsockopt: SOL_SOCKET,
SO_BROADCAST | SO_REUSEADDR
connect: Port 9000 | bind: Port 9000
IP INADDR_BROADCAST | IP INADDR_ANY
sendto: Port 9000 | recvfrom: (waiting data)
IP INADDR_BROADCAST | closesocket
closesocket | server found if data includes ServerInfo
Sleep(5000) | Sleep(500)
END LOOP | END LOOPNow the problem is client does receive data, but only sent by itself. The data sent by server is not received, even when the server and client running in the PC. Can you help troubleshoot? Thank you!