CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 4 of 5 FirstFirst 12345 LastLast
Results 46 to 60 of 67
  1. #46
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    i assumed the code from the MSDN website would be right.
    ok its set to true now
    it is still displaying the error for setsockopt and now it is also displaying the error message from this code:
    Code:
      if(listen(m_serversocket,0) < 0)
        {
    		 CString text;
    		text.Format(_T("ERROR listening in the server socket: %d"), WSAGetLastError()); //---error---//
    		AfxMessageBox(text);
             exit(1);
        }
    error is 10045

  2. #47
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: getting a binding error in server program

    Well, it is already one step forward!
    But why don't you want to find the meaning adn desription of this error? Don't you have MSDN? Or are you banned by Google?

    BTW, this error means:
    WSAEOPNOTSUPP (10045)

    Operation not supported.

    The attempted operation is not supported for the type of object referenced. Usually this occurs when a socket descriptor to a socket that cannot support this operation, for example, trying to accept a connection on a datagram socket.
    For the listen function there is some additional info:
    The referenced socket is not of a type that supports the listen operation.
    Victor Nijegorodov

  3. #48
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    ok so the error i get with listen is 10045 which is:
    Operation not supported. The attempted operation is not supported for the type of object referenced. Usually this occurs when a socket descriptor to a socket that cannot support this operation is trying to accept a connection on a datagram socket.
    i don't know how to solve this problem. description of the error doesn't help

  4. #49
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: getting a binding error in server program

    You got this WSAEOPNOTSUPP (10045) trying to call function listen, so the error description in this case is much shorter:
    The referenced socket is not of a type that supports the listen operation.
    It means that you did something was wrong between socket creation and calling listen.
    The easiest way to find out what is wrong is to compare your code with some working example (from MSDN or other sites) for listening socket.
    Victor Nijegorodov

  5. #50
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    i have changed so much code so i started again.
    here is the current code:
    Code:
    WSADATA wsaData; 
    		SOCKET ListeningSocket, NewConnection; 
    		SOCKADDR_IN ServerAddr; 
    		int Port = 7171;
    		char recvbuff[1024];
    		int i, nlen;
    
    		if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
    		{
    			CString text;
    			text.Format(_T("ERROR WSAStartup failed: %d"), WSAGetLastError());
    			AfxMessageBox(text);
    			exit (1);
    		}
    	
    		//ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    		  ListeningSocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    
    		if (ListeningSocket == INVALID_SOCKET)
    		{
    			CString text;
    			text.Format(_T("ERROR at socket, error code: %d"), WSAGetLastError());
    			AfxMessageBox(text);
    			WSACleanup();
    			exit (1);
    		}
    	
    
    		ServerAddr.sin_family = AF_INET;
    		ServerAddr.sin_port = htons(Port);
    		ServerAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
    
    		if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
    		{
    			CString text;
    			text.Format(_T("ERROR bind failed, Error code: %d"), WSAGetLastError());
    			AfxMessageBox(text);
    			closesocket(ListeningSocket);
    			WSACleanup();
    			exit (1);
    		}
    
    		if (listen(ListeningSocket, 5) == SOCKET_ERROR)
    		{
    			CString text;
    			text.Format(_T("ERROR listen: Error listening on socket: %d"), WSAGetLastError());
    			AfxMessageBox(text);
    			closesocket(ListeningSocket);
    			WSACleanup();
    			exit (1);
    		}
    
    		while(1)
    		{
    			NewConnection = SOCKET_ERROR;
    			while(NewConnection == SOCKET_ERROR)
    			{
    				NewConnection = accept(ListeningSocket, NULL, NULL);
    
    				//getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
    
    				memset(&ServerAddr, 0, sizeof(ServerAddr));
    				nlen = sizeof(ServerAddr);
    
    				int ret = sendto(ListeningSocket, recvbuff, strlen(recvbuff), 0, (sockaddr*)&ServerAddr, nlen);
    			    if(ret < 0)
    				{
    					CString text;
    					text.Format(_T("ERROR Error broadcasting to the clients: %d"), WSAGetLastError());
    					AfxMessageBox(text);
    				}
    
    				//getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);
    
    			}
    				
    		}
    
    		if( shutdown(NewConnection, SD_SEND) != 0)
    			printf("\n\nServer: Well, there is something wrong with the shutdown. The error code: %ld\n", WSAGetLastError());
    			CString text;
    			text.Format(_T("ERROR Something wrong with shutdown, error code: %d"), WSAGetLastError());
    			AfxMessageBox(text);
    		
    
        SetTimer(0x01, 100, NULL);
    }
    now when i run it the error message "ERROR bind failed, Error code: 10049" displays
    that error means
    Cannot assign requested address.
    The requested address is not valid in its context. This normally results from an attempt to bind to an address that is not valid for the local computer. This can also result from connect, sendto, WSAConnect, WSAJoinLeaf, or WSASendTo when the remote address or port is not valid for a remote computer (for example, address or port 0).
    Last edited by beginner91; March 20th, 2013 at 05:09 AM.

  6. #51
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: getting a binding error in server program

    Didn't you find some working example of socket server application?
    Victor Nijegorodov

  7. #52
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    not one that uses broadcast. its very difficult to find. if you could link any examples that would be great

  8. #53
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: getting a binding error in server program

    Why do you use INADDR_BROADCAST instead of INADDR_ANY for your server socket?
    Victor Nijegorodov

  9. #54
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    i was told to use it. its a requirement

  10. #55
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    i was told to use it. its a requirement
    The first "requirement" for you is reading the documentation!
    Understand?

    If you read the documentation about INADDR_BROADCAST you'd know that it is used to send a broadcast (see http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx)
    To receive the broadcasts socket uses INADDR_ANY (see the http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx)
    Last edited by VictorN; March 20th, 2013 at 05:53 AM.
    Victor Nijegorodov

  11. #56
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    sorry those links don't work for me

  12. #57
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    but i understand. so INADDR_ANY is used in the server and INADDR_BROADCAST is used in the client

  13. #58
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    sorry those links don't work for me
    Sorry! My fault...
    I've edited links.
    Victor Nijegorodov

  14. #59
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    ok so i changed it to INADDR_ANY but i'm still getting the same error with listen

  15. #60
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    ok so i changed it to INADDR_ANY but i'm still getting the same error with listen
    with listen or with bind?
    Victor Nijegorodov

Page 4 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
  •  





Click Here to Expand Forum to Full Width

Featured