problem changing code from TCP to UDP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beginner91
    New Member
    • Mar 2013
    • 1

    #1

    problem changing code from TCP to UDP

    i created a client server program in MFC using TCP and that worked fine. But i want the server to work on any computer so for the client to connect to the server i need to use broadcasting which can only be done in UDP. so i tried changing my code but i'm having a lot of problems. can someone please look at the code and tell me if i'm missing something (never used UDP before).

    heres the code for server
    when i run the program the message "error with sendto: 10047" displays
    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 = (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;
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Firstly before you do anything else look up "beej guide to network programming" in your favourite web search engine and read it.

    The problem with what you are trying to do is that it is not simple due to the different natures of TCP and UDP sockets.

    A TCP socket is a byte stream, that is the data is not sent in distinct blocks, just because you sent 3 lots of 10 bytes doesn't mean you will receive 3 lots of 10 bytes. You might receive 1 lot of 30 bytes or 30 lots of 1 byte or anything in-between. But you are guaranteed that if you are receiving data then it is the right data and arriving in the order it was transmitted. A TCP socket is connected, that is it is a point to point connection. The first thing you do is set up the connection.

    A UDP socket sends datagrams, that is the data is sent in distinct blocks which are limited in size. A UDP socket is not a connection, the is no feed back or guarantee of delivery in any order, either you do or don't receive the block of data as a distinct object with no reference to any other object transmitted. When you transmit a UDP datagram you just send it out and hope that someone is listening for it, no connection is made. This means that when you receive you just listen out for an incoming UDP datagram with bothering to listen for a connection because you wont get one. And that means that calling listen and accept have no meaning for a UDP socket.

    Like I said read beej's guide to network programming it will explain everything.

    Comment

    Working...