Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all! I already searched a few hours but I can't find the right answer. I have a problem with an UDP-broadcast. First of all: How can I set the ...
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Posts
    15

    [SOLVED] C/C++: Broadcast receives no answer

    Hi all!

    I already searched a few hours but I can't find the right answer.
    I have a problem with an UDP-broadcast.
    First of all: How can I set the outgoing port for the socket? In wireshark, my outgoing port is 1024, and i want to set it to 6800 (or a similar port).
    Second: I see the outgoing broadcast-message in wireshark, but no answer. There are three ECU's in the LAN, which should answer.

    The following code is a mixture of C/C++ and jointed from different articles:

    Code:
    bool broadcast()
    {
    	int ttl = 128;
    	int broadcast=1;
    	int reuse=1;
    	int sockRecv, sockSend;
    	struct sockaddr_in sinRecv, sinSend;
    	char sendBuffer[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x11};
    	
    	// Create the Listener-Part ======================================
    	sockRecv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    	memset(&sinRecv,0,sizeof(sinRecv));
    	sinRecv.sin_family = AF_INET; // address family Internet
    	sinRecv.sin_port = (unsigned short)htons(6811);//BROADCAST_PORT); //Port to connect on
    	sinRecv.sin_addr.s_addr = htonl(INADDR_ANY); 	
    	if (bind(sockRecv, (struct sockaddr*)&sinRecv, sizeof sinRecv) < 0) {
    		perror("bind");
    		exit(1);
    	}
    	
    	// Create the Sender-Part ======================================
    	sockSend = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    	memset(&sinSend,0,sizeof(sinSend));
    	sinSend.sin_family = AF_INET;
    	sinSend.sin_port = (unsigned short)htons(6811);
    	sinSend.sin_addr.s_addr = /*INADDR_BROADCAST;//*/inet_addr("192.168.0.255");//inet_addr("0.0.255.255") | htonl(host_addr.sin_addr.s_addr);	
    	
    	if ((setsockopt(sockSend, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse)) == -1) {
    		perror("setsockopt - SO_SOCKET ");
    		exit(1);
    	}
    	if ((setsockopt(sockSend, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast)) == -1) {
    		perror("setsockopt - SO_SOCKET ");
    		exit(1);
    	}
    	if (setsockopt(sockSend, IPPROTO_IP, IP_TTL, (const char*)&ttl, sizeof(ttl)) == -1) {
            perror("TTL setsockopt failed: ");
            exit(1);
        }	
    	if(connect(sockSend, (struct sockaddr*)&sinSend, sizeof(sinSend)) < 0) {
    		perror("connect");
    		exit(1);
    	}
    	
    	int nBytesSent = send(sockSend, (const char*) sendBuffer, sizeof(sendBuffer), 0);
    	std::cout << nBytesSent << " Bytes gesendet" << std::endl;
    	
    	int nBytesRecv = 1;
    	char recvBuffer[80];
    	memset(recvBuffer, 0, sizeof(recvBuffer)); //Clear the buffer
    	socklen_t addr_len;
    	nBytesRecv = recv(sockRecv, recvBuffer, sizeof recvBuffer, 0);//
    	//nBytesRecv = recvfrom(sockRecv, recvBuffer, sizeof recvBuffer, MSG_DONTROUTE, (struct sockaddr*)&sinRecv, &addr_len);
    	if(nBytesRecv > 0)
    	{
    		time_t now = time(NULL);
    		std::cout << "recvfrom: " << nBytesRecv << " Bytes: ";
    		for(int i = 0; i < nBytesRecv; i++)
    			std::cout << (int)(recvBuffer[i]) << " ";
    		std::cout << std::endl;
    	}
    	else
    		perror("recvfrom");
    
    	close(sockSend);
    	close(sockRecv);
    	return 0;
    }
    In wireshark I can only see the outgoing message. But the "recvBuffer" (in the lower part of the code) contains the same content as the sendBuffer.

    Is it right, that I need a sender and further more a receiver? Or should I receive the broadcast-answer on the same socket on which I send the bc?

    Please, could anybody help?
    Any hint is very welcome!

  2. #2
    Just Joined! bbchina's Avatar
    Join Date
    May 2010
    Posts
    3
    sendto() //send upd data
    recvfrom() // recv udp data

    memset(recvBuffer, '\0', sizeof(recvBuffer)); //Clear the buffer

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Posts
    15
    Hi bbchina!

    Thanks for your reply!

    I changed the both functions but the result is the same:

    Wireshark shows the outgoing broadcast (curiously with 14 zeros at the end of the packet), but I receive no answer. The recvfrom-function receives only a kind of echo (the same bytes which are assigned to the sendBuffer).

    Could it be, that the outgoing port (1024) is bad for this? Or could it be, that the recvSocket don't work correctly?

    Thanks!

    P.S.: Has the sendBuffer the right content or what should I send, to receive an answer?

  4. #4
    Just Joined! bbchina's Avatar
    Join Date
    May 2010
    Posts
    3

    Smile

    Hi,

    please reference this book 5.3 Datagram Sockets(Page 26)
    Here is a very simple upd server and upd client
    ================================================== =====
    beejnet.pdf.bz2

    Sorry, My English is too bad

  5. #5
    Just Joined!
    Join Date
    Jul 2008
    Posts
    15
    Hi again!

    Thanks for the pdf! I read it, but I still can't find an important difference.

    I think, it's the Frame-Length of my UDP-packet. As I explained, there are additional zeros at the end of the packet (see the attached jpg).

    Does anybody know, where they come from or how I can trim the "Trailer" of the packet?

    Bye!
    Attached Images Attached Images

  6. #6
    Just Joined!
    Join Date
    Jul 2008
    Posts
    15
    Ok! - I solved it!!

    The problem was neither the port 1024 nor the additional zeros.

    The fault was, that I called the connect()-function. This blocked the socket or something else.

    So I deleted it, an now it works!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...