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

Thread: getting a binding error in server program

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

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    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
    But the line you have highlighted does NOT execute! Just because your "program" exits about 15...20 lines "ealier"!

    Again: reread my post#6 and begin from the basics and learning how to debug!
    Victor Nijegorodov

  2. #32
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    i never realized the line didn't get executed.
    is it because its in the wrong place?

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

    Re: getting a binding error in server program

    Yes.
    And the third time: you must debug your code to not only realize but also be sure that something gets executed or not.
    Victor Nijegorodov

  4. #34
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    ok i debugged it and went through each line
    these are the lines that don't get executed:
    WSADATA wsaData;
    SOCKADDR_IN brdcastaddr;
    char sbuf[1024];

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

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    ok i debugged it and went through each line
    these are the lines that don't get executed:
    WSADATA wsaData;
    SOCKADDR_IN brdcastaddr;
    char sbuf[1024];
    Because there is nothing to execute! They all are just the definitions!
    Victor Nijegorodov

  6. #36
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    well i don't what the problem is. those are the only lines that don't get executed and then i reach the error message

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

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    well i don't what the problem is. those are the only lines that don't get executed and then i reach the error message
    Well, let's begin from the very begin. Please, answer the following questions:
    1. What is the reason of the error you got?
    2. What is recommended to fix this error?
    3. Did you implement this recommendation?
    4. If you did - then where and how?
    Victor Nijegorodov

  8. #38
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    the reason for the error is from the 10013 error which is:
    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).

    but my setsockopt is using SO_BROADCAST.

    i have moved the setsockopt code above the sendto code
    the code executes setsockopt and jumps into the else statement
    this time the WSAGetLastError is 0
    then it executes sendto and displays the WSAGetLastError

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

    Re: getting a binding error in server program

    Please, post the actual code!
    Victor Nijegorodov

  10. #40
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    the only change i made was moving some code around
    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);
    	
    
        int portno = 1818;
    	SOCKADDR_IN brdcastaddr; 
    
    	//---------------------------------------
        // Create a listening socket
    	m_serversocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); 
        if(m_serversocket == -1)
        {
            AfxMessageBox("Socket Initialiation Error");
    		WSACleanup();
        }
    	
    	memset(&brdcastaddr,0, sizeof(brdcastaddr));
        brdcastaddr.sin_family = AF_INET;
        brdcastaddr.sin_port = htons(portno);
        brdcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
    	
    
    	int len = sizeof(brdcastaddr);
        char sbuf[1024]; 
    	int BufLen = 1024;
    
    
    	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");
    		CString text;
    		text.Format(_T("ERROR : %d"), WSAGetLastError()); //---error---//
    		AfxMessageBox(text);
    	}
    
    
        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()); //---error---//
    		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 = 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);
    }
    Last edited by beginner91; March 19th, 2013 at 08:30 AM.

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

    Re: getting a binding error in server program

    What error do you get now?
    Victor Nijegorodov

  12. #42
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    the code executes setsockopt and jumps into the else statement
    this time the WSAGetLastError is 0
    then it executes sendto and displays the WSAGetLastError which is still 10013

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

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    the only change i made was moving some code around
    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);
    	
    
        int portno = 1818;
    	SOCKADDR_IN brdcastaddr; 
    
    	//---------------------------------------
        // Create a listening socket
    	m_serversocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); 
        if(m_serversocket == -1)
        {
            AfxMessageBox("Socket Initialiation Error");
    		WSACleanup();
        }
    	
    	memset(&brdcastaddr,0, sizeof(brdcastaddr));
        brdcastaddr.sin_family = AF_INET;
        brdcastaddr.sin_port = htons(portno);
        brdcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
    	
    
    	int len = sizeof(brdcastaddr);
        char sbuf[1024]; 
    	int BufLen = 1024;
    
    
    	iResult = setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);
         ...
    }
    Why do you pass FALSE in the bOptVal parameter?
    Victor Nijegorodov

  14. #44
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    that code is from MSDN. should it be true?
    it is set to false at the start but then down a few lines it is set to true
    is this a mistake?
    Last edited by beginner91; March 19th, 2013 at 08:36 AM.

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

    Re: getting a binding error in server program

    Again:
    you must read the documentation to understand what value you need and in which case!
    So, if you need to set a SO_BROADCAST option it must be TRUE, if instead you need to remove this option then FALSE.
    Victor Nijegorodov

Page 3 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