getting a binding error in server program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 5 1234 ... LastLast
Results 1 to 15 of 67

Thread: getting a binding error in server program

  1. #1
    Join Date
    Mar 2012
    Posts
    99

    getting a binding error in server program

    i created a client server program in MFC but when i run the server the error message "ERROR binding in the server socket" displays.
    this is the first time i am using code like this so I don't know how to fix this error.

    here is the code:
    Code:
    void CSocketTestServerDlg::StartServer()
    {
        WORD w = MAKEWORD(1,1);
        WSADATA wsadata;
        ::WSAStartup(w, &wsadata);
    
    	char opt = 1; 
    	setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char));
    	SOCKADDR_IN brdcastaddr;
        int portno = 1818;
    
        memset(&brdcastaddr,0, sizeof(brdcastaddr));
        brdcastaddr.sin_family = AF_INET;
        brdcastaddr.sin_port = htons(portno);
        brdcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
    
    	m_serversocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if(m_serversocket == -1)
        {
            AfxMessageBox("Socket Initialiation Error");
        }
    
         int len = sizeof(brdcastaddr);
        char sbuf[1024];
        int ret = sendto(m_serversocket, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
        if(ret < 0)
        {
             AfxMessageBox("ERROR binding in the server socket");
             exit(1);
        }
    
        if(listen(m_serversocket,0) < 0)
        {
             AfxMessageBox("ERROR listening in the server socket");
             exit(1);
        }	
    
        SetTimer(0x01, 100, NULL);
    }
    Last edited by beginner91; March 19th, 2013 at 06:26 AM.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: getting a binding error in server program

    From MSDN article "sendto":
    Return value
    If no error occurs, sendto returns the total number of bytes sent, which can be less than the number indicated by len. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
    So, why did you miss/ingore to call WSAGetLastError?
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    like a said i am new to this kind of code. but adding WSAGetLastError doesn't do anything
    AfxMessageBox("ERROR binding in the server socket", WSAGetLastError() );

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    like a said i am new to this kind of code. but adding WSAGetLastError doesn't do anything
    AfxMessageBox("ERROR binding in the server socket", WSAGetLastError() );
    WSAGetLastError gives zou the error code, one of the possible codes listed in the sendto article!
    So which one is in your case?
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    the program doesn't run when i add WSAGetLastError to the messagebox. it just builds then nothing happens
    do i need to add more code?

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    the program doesn't run when i add WSAGetLastError to the messagebox. it just builds then nothing happens
    do i need to add more code?
    You must first learn the basics of C++/Visual C++ programming! Like using MessageBox, preparing/formatting text for output and so on...
    The next steps are reading documentation and debugging.
    Only after that you wil be probably able to develop more complicated things like sockets.
    Example of using WSAGetLastError:
    Code:
    CString text;
    text.Format(_T("ERROR calling the socket 'sendto' function: %d"), WSAGetLastError());
    AfxMessageBox(text);
    Victor Nijegorodov

  7. #7
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    thank you for the code. the error code is 10013

  8. #8
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: getting a binding error in server program

    It is WSAEACCES (10013)
    From MSDN (October 2000):
    WSAEACCES [/B](10013)
    Permission denied.

    An attempt was made to access a socket in a way forbidden by its access permissions. An example is using a broadcast address for "sendto" without broadcast permission being set using setsockopt(SO_BROADCAST).
    From the MSDN sendto:
    WSAEACCES
    The requested address is a broadcast address, but the appropriate flag was not set. Call setsockopt with the SO_BROADCAST parameter to allow the use of the broadcast address.
    Victor Nijegorodov

  9. #9
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    so the problem is with is line of code:
    setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char));

    i am using the SO_BROADCAST parameter so what is the problem with the code?

  10. #10
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: getting a binding error in server program

    BTW, You called setsockopt but didn't check its error code. Why?
    Victor Nijegorodov

  11. #11
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    ok i didn't check the error code for anything in my program so i made some changes
    i used the code from the here http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    Code:
     
    int iResult = 0;
    	WSADATA wsaData;
    	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    
    	BOOL bOptVal = FALSE;
        int bOptLen = sizeof (BOOL);
    
        int iOptVal = 0;
        int iOptLen = sizeof (int);
    	
       // WORD w = MAKEWORD(1,1);
      //  WSADATA wsadata;
       // ::WSAStartup(w, &wsadata);
    
    	//char opt = 1; 
    	//setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char));
    
    	
    
    	 bOptVal = TRUE;
    
        iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
        if (iResult == SOCKET_ERROR) 
    	{
    		CString text;
    		text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
    		AfxMessageBox(text);
        }
    	else
    	{
    		CString text;
    		text.Format(_T("SO_KEEPALIVE Value: %ld\n"), iOptVal);
    		AfxMessageBox(text);
    	}
    
        iResult = setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &iOptVal, &iOptLen);
        if (iResult == SOCKET_ERROR) 
    	{
    		CString text;
    		text.Format(_T("setsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
    		AfxMessageBox(text);
        }
    	else
    		AfxMessageBox("ERROR binding in the server socket");
    
        iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
        if (iResult == SOCKET_ERROR)
    	{
    		CString text;
    		text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
    		AfxMessageBox(text);
        } else
    	{
    		CString text;
    		text.Format(_T("SSO_KEEPALIVE Value: %ld\n"), iOptVal);
    		AfxMessageBox(text);
    	}
    
        closesocket(m_serversocket);
        WSACleanup();
    but now i am getting this compile error:
    Error 2 error C2664: 'setsockopt' : cannot convert parameter 5 from 'int *__w64 ' to 'int'
    Last edited by beginner91; March 19th, 2013 at 06:25 AM.

  12. #12
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: getting a binding error in server program

    Well, if you cannot correctly do so simple copy/paste operation then I cannot help you more...
    Your code differs from the MSDN original one!
    Victor Nijegorodov

  13. #13
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: getting a binding error in server program

    One more thing: please, use Code tags while posting code snippets! See Announcement: Before you post....
    Victor Nijegorodov

  14. #14
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    i did not copy and paste since i had different names for things but it is based from the MSDN code

  15. #15
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    sorry. i edited my posts

Page 1 of 5 1234 ... LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center