getting a binding error in server program - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 5 FirstFirst 12345 LastLast
Results 16 to 30 of 67

Thread: getting a binding error in server program

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

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    i had different names for things but it is based from the MSDN code
    Well, it is based from the MSDN code but with your own bugs implemented in there!
    Please, compare!
    Victor Nijegorodov

  2. #17
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    ok i fixed that error i was getting.
    now a load of the error messages i added are displaying when u run the program:
    getsockopt for SO_KEEPALIVE failed with error: 10038
    setsockopt for SO_KEEPALIVE failed with error: 10038
    getsockopt for SO_KEEPALIVE failed with error: 10038
    ERROR binding in the server socket: 10013
    Last edited by beginner91; March 19th, 2013 at 06:33 AM.

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

    Re: getting a binding error in server program

    From MSDN:
    WSAENOTSOCK (10038)

    Socket operation on non-socket.

    An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.
    I'd recommend you to first test MSDN example without your own changes.
    Only after it will work - try to change it according to your needs!
    Victor Nijegorodov

  4. #19
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    ok i moved some code around. the problem was it wasn't reading a socket.
    now the only error message displaying is the one that started this problem:
    ERROR binding with server socket 10013

    all the code:
    Code:
    void CSocketTestServerDlg::StartServer()
    {
    	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));
    
    
    
    	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); //listening socket
        if(m_serversocket == -1)
        {
            AfxMessageBox("Socket Initialiation Error");
        }
    
        //if(bind(m_serversocket, (SOCKADDR*)&serveraddr,sizeof(SOCKADDR_IN)) < 0) 
    	int len = sizeof(brdcastaddr);
        char sbuf[1024];
    
        int ret = sendto(m_serversocket, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
        if(ret < 0)
        {
    		CString text;
    		text.Format(_T("ERROR binding in the server socket: %d"), WSAGetLastError());
    		AfxMessageBox(text);
            exit(1);
        }
    
        if(listen(m_serversocket,0) < 0)
        {
             AfxMessageBox("ERROR listening in the server socket");
             exit(1);
        }	
    
    
    	 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);
    	}
    
    
    //------------------------ set socket ---------------------------------//
        iResult = setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);
        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();
    
    	
    
        SetTimer(0x01, 100, NULL);
    }
    Last edited by beginner91; March 19th, 2013 at 07:05 AM.

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

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    ok i moved some code around. the problem was it wasn't reading a socket.
    now the only error message displaying is the one that started this problem:
    ERROR binding with server socket 10013
    Please, go back to the post#4!
    Victor Nijegorodov

  6. #21
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    yes i know what that error means
    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.
    but i then started adding all the error messages so the problem was never solved. what do i need to do to fix it?

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

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    yes i know what that error means
    Then why do you ignore the way to fix it:
    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

  8. #23
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    but i have done that:
    Code:
    setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);

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

    Re: getting a binding error in server program

    where?????
    Victor Nijegorodov

  10. #25
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    please look at post #19
    i edited the code and put in the line //----------- set socket ---------// so it is easy to find

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

    Re: getting a binding error in server program

    It's you who has to look at your own code!
    Your setsockopt call is commented out!
    Victor Nijegorodov

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

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    ok i moved some code around. the problem was it wasn't reading a socket.
    now the only error message displaying is the one that started this problem:
    ERROR binding with server socket 10013

    all the code:
    Code:
    void CSocketTestServerDlg::StartServer()
    {
    	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));
    
    
    
    	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); //listening socket
        if(m_serversocket == -1)
        {
            AfxMessageBox("Socket Initialiation Error");
        }
    
        //if(bind(m_serversocket, (SOCKADDR*)&serveraddr,sizeof(SOCKADDR_IN)) < 0) 
    	int len = sizeof(brdcastaddr);
        char sbuf[1024];
    
        int ret = sendto(m_serversocket, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
        if(ret < 0)
        {
    		CString text;
    		text.Format(_T("ERROR binding in the server socket: %d"), WSAGetLastError());
    		AfxMessageBox(text);
            exit(1);
        }
        ...
    }
    Don't you see it?
    Besides, this line (being uncommeted) must be after socket creation.
    Victor Nijegorodov

  13. #28
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    that is the old setsockopt. sorry i just commented it out instead of removing it
    i rewrote it when i added the error message code as you can see if you look at the code i posted and look for //----------- set socket ---------// comment

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

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    that is the old setsockopt. sorry i just commented it out instead of removing it
    i rewrote it when i added the error message code as you can see if you look at the code i posted and look for //----------- set socket ---------// comment
    Please, post your final code now.
    Victor Nijegorodov

  15. #30
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    final code:
    Code:
    void CSocketTestServerDlg::StartServer()
    {
    	//---------------------------------------
        // Declare variables
    	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);
    	
    
    	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;
    
    	//---------------------------------------
        // Create a listening socket
    	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 BufLen = 1024;
    
        int ret = sendto(m_serversocket, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
        if(ret < 0)
        {
    		CString text;
    		text.Format(_T("ERROR binding in the server socket: %d"), WSAGetLastError());
    		AfxMessageBox(text);
            exit(1);
        }
    
        if(listen(m_serversocket,0) < 0)
        {
             AfxMessageBox("ERROR listening in the server socket");
             exit(1);
        }	
    
    
    	 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 *) &bOptVal, bOptLen);
        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("SO_KEEPALIVE Value: %ld\n"), iOptVal);
    		AfxMessageBox(text);
    	}
    
        closesocket(m_serversocket);
        WSACleanup();
    
    	
    
        SetTimer(0x01, 100, NULL);
    }
    sorry for the confusion

Page 2 of 5 FirstFirst 12345 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