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?

解决方案 »

  1.   

    1.The requested address is not valid in its context.2.An address incompatible with the requested protocol was used.
      

  2.   

    1.
    >>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
      

  3.   

    Hi, gull1234(俗不可耐) , I tried twice:1. addrs.sin_addr.s_addr = INADDR_ANY;
    >> The 2nd problem still there.2. addrs.sin_addr.s_addr = htonl(INADDR_BROADCAST);
    >> 2 problems still.Might you give more details, thanks.
      

  4.   

    Sorry, connect must be taken away from the server.I've planned to implement the communication between server and client by broadcast in this way: 
    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();
      

  5.   

    to laotong(老童)
    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
      

  6.   

    I commented out the bind for client because it does not necessary. You are right that the bind error for client due to running on same pc with server.sendto() does work in both client and server. When debugging, I see the msg sent by client is received by server, then server send out reply msg ("Server Ready") successfully (because server always running and receive the msg sent out by itself), but client NOT receive it and block there. Why server can receive it but client can not for the same broadcasting msg from server?
      

  7.   

    Can I simply depict my implementation here?At first the client block at recvfrom():                        Server | Client
       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!