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

    Re: getting a binding error in server program

    i thought i only needed to change this line:
    Code:
    ServerAddr.sin_addr.s_addr = INADDR_ANY;

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

    Re: getting a binding error in server program

    Note also the listen applies only to sockets that support connections, that is, those of type SOCK_STREAM!
    Datagram socket has to use recvfrom function.
    Victor Nijegorodov

  3. #63
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    so i'm missing the recvform function?
    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 = INADDR_ANY;
    
    		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);
    				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);
    				}
    
    			}
    				
    		}
    
    		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);
    }

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

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    so i'm missing the recvform function?
    Please, read the documentation about socket programming! Use MSDN and Google to find the examples!
    Victor Nijegorodov

  5. #65
    Join Date
    Mar 2012
    Posts
    99

    Re: getting a binding error in server program

    it is difficult to look up a document for every function especially when i don't know the functions needed. do you know of any fully completed examples/source code?

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

    Re: getting a binding error in server program

    Quote Originally Posted by beginner91 View Post
    it is difficult to look up a document for every function especially when i don't know the functions needed.
    Yes, it is.
    But you must if, of cousre, you want to become a programmer!
    Quote Originally Posted by beginner91 View Post
    do you know of any fully completed examples/source code?
    No, because I never used datagram sockets, only TCP.
    But I'm sure Google knows! Just ask him!
    Victor Nijegorodov

  7. #67
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: getting a binding error in server program


Page 5 of 5 FirstFirst ... 2345

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