problem changing code from TCP to UDP
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: problem changing code from TCP to UDP

  1. #1
    Join Date
    Mar 2012
    Posts
    99

    problem changing code from TCP to UDP

    i had created a client server program in MFC using TCP but by doing this the server can only work on one computer because you have to tell the client the ip address to connect to i.e
    Code:
     ServerAddr.sin_addr.s_addr = inet_addr("10.13.31.112");
    so i want to change my code so that i can use broadcasting but i found out that it only works using UDP. I tried changing the code but the error message "error with sendto: 10047" displays when i run the program.
    that error means
    Address family not supported by protocol family. An address incompatible with the requested protocol was used. All sockets are created with an associated address family (that is, AF_INET for Internet Protocols) and a generic protocol type (that is, SOCK_STREAM). This error is returned if an incorrect protocol is explicitly requested in the socket call, or if an address of the wrong family is used for a socket, for example, in sendto.
    here is the code from server:
    Code:
    WSADATA wsaData; 
    	WSAStartup(MAKEWORD(2,2), &wsaData);
    
    	int port = 7171;
    	if (param)
    		port = reinterpret_cast<short>(param);
    
    	SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    	if (s == -1)
    	{
    		closesocket(s);
    		return 1;
    	}
    
    	sockaddr_in brdcastaddr;
    	int len = sizeof(brdcastaddr);
        char sbuf[1024]; 
    	brdcastaddr.sin_family = AF_INET;
    	brdcastaddr.sin_port = htons(port);
    	brdcastaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    	
    	char opt = 1; 
    
    	int bind_ret = bind(s, (sockaddr*)&brdcastaddr, sizeof(brdcastaddr));           
    	if (bind_ret == -1)
    	{
    		CString text;
    		text.Format(_T("ERROR binding: %d"), WSAGetLastError());
    		AfxMessageBox(text);
    		closesocket(s);
    		return 1;
    	}
    
        setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char));
    	memset(&brdcastaddr,0, sizeof(brdcastaddr));
    
    
    	int ret = sendto(s, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
        if(ret < 0)
        {
    		CString text;
    		text.Format(_T("ERROR with sendto: %d"), WSAGetLastError());
    		AfxMessageBox(text);
            return 1;
        }
    
    	int listen_ret = listen(s, 5); 
    	if (listen_ret == -1)
    	{
    		CString text;
    		text.Format(_T("ERROR listening: %d"), WSAGetLastError());
    		AfxMessageBox(text);
    		closesocket(s);
    		return 1;
    	}
    
    	while (true)
    	{
    		sockaddr_in client_addr;
    		int len = sizeof(client_addr);
    		SOCKET client_sock = accept(s, (sockaddr*)&client_addr, &len);
    	
    		ClientInfo info;
    		info.sock = client_sock;
    		info.addr = inet_ntoa(client_addr.sin_addr);
    		{
    			Mutex<CriticalSection>::Lock lock(client_cs);
    			clients.push_back(info);
    		}
    
    		unsigned tid;
    		_beginthreadex(NULL, 0, tcp_servers_client, reinterpret_cast<void*>(client_sock), 0, &tid);
    	}
    
    	closesocket(s);
    	return 0;

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

    Re: problem changing code from TCP to UDP

    For broadcast client should use INADDR_BROADCAST
    Victor Nijegorodov

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center