Craspberry_Pi
Posts: 5
Joined: Thu Dec 13, 2012 3:16 pm

Receive UDP

Thu Dec 13, 2012 3:36 pm

Hello,

i have a problem... ;)
Since 2 Days i'am trying to receive a simple UDP Message.

My code at the moment:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{	 
    int handle = socket( AF_INET, SOCK_DGRAM, 0 );
    if(handle < 0){
		perror("socket");
		exit(1);
	}
	 
	struct sockaddr_in servaddr;
	servaddr.sin_family = AF_INET;
	servaddr.sin_port=41414;
	servaddr.sin_addr.s_addr= INADDR_ANY;

    if ( bind( handle, (struct sockaddr*)&servaddr, sizeof(servaddr) ) < 0 ){
	perror("bind");
	exit(1);
	}
		  	
    while ( 1 )
    {
		struct sockaddr_in cliaddr;
		char packet_data[1024];	

		socklen_t len= sizeof(cliaddr); 
		int received_bytes = recvfrom( socket, packet_data, sizeof(packet_data),0, (struct sockaddr*)&cliaddr, &len );
		if ( received_bytes > 0 )
	    printf("Here is the message: %s\n",packet_data);
    }
     close(handle);
     return 0; 
}
I cant receive anything.
There is just nothing happening .

While trying to find a solution i noticed that right after i send the UDP-message, there is ICMP-message "Destination unreachable(Port unreachable)" from my raspberry pi.
But i dont have a clue why.

Can anybody help me with this?

P.S.:
It looks like there is no general problem with my Pi, because if i try the same with Python it works like a charme

User avatar
[email protected]
Posts: 1984
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Receive UDP

Thu Dec 13, 2012 5:28 pm

Are you sure it compiles? The first argument to recvfrom is wrong - it should be 'handle'...

-Gordon
--
Gordons projects: https://projects.drogon.net/

Craspberry_Pi
Posts: 5
Joined: Thu Dec 13, 2012 3:16 pm

Re: Receive UDP

Thu Dec 13, 2012 5:54 pm

... oh you are right. There was a warning during the Compilation.
Now it compiles without Problems.

But the main problem is still there:
I still dont receive any UDP messages ...










EDIT
..... Okay i found the last problem

Code: Select all

	servaddr.sin_port=(41414);
should be:

Code: Select all

	servaddr.sin_port=htons(41414);
Thank you Gordon for your fast Help.
Now it works!

User avatar
Venezuela
Posts: 23
Joined: Thu Dec 13, 2012 4:00 pm
Location: Caracas

Re: Receive UDP

Thu Dec 13, 2012 8:51 pm

Excelent! :lol:

taoufiksmires
Posts: 4
Joined: Thu Mar 20, 2014 10:03 pm

Re: Receive UDP

Fri Mar 21, 2014 7:58 pm

hello ,
i have a project in university with raspberry , i need to transmitt some value with udp to my pc for , im not good in socket , so can you help me ?? any help is good for me lol , because i dont have enough time to search ! like how can i install socket in raspberry ? the programe i can use and how can i have the value from my raspberry !! monday is THE FINAL DAY :oops:

User avatar
DougieLawson
Posts: 34165
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Receive UDP

Fri Mar 21, 2014 8:04 pm

Here's three samples I picked up on my travels round the internets.

udp.broadcast.c

Code: Select all

  #include <arpa/inet.h>
  #include <netinet/in.h>
  #include <stdio.h>
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <unistd.h>

  #define BUFLEN 512
  #define NPACK 10
  #define PORT 9930

  #define SRV_IP "224.0.0.1"
  /* diep(), #includes and #defines like in the server */

  void diep(char *s)
  {
    perror(s);
    exit(1);
  }


  int main(void)
  {
   struct sockaddr_in si_other;
   int s, i, slen=sizeof(si_other);
   char buf[BUFLEN];

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
      diep("socket");

    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);
    if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
     fprintf(stderr, "inet_aton() failed\n");
      exit(1);
    }

    for (i=0; i<NPACK; i++) {
      printf("Sending packet %d\n", i);
      sprintf(buf, "This is packet %d\n", i);
      if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
        diep("sendto()");
    }

    close(s);
    return 0;
  }
udp.client.c

Code: Select all

  #include <arpa/inet.h>
  #include <netinet/in.h>
  #include <stdio.h>
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <unistd.h>

  #define BUFLEN 512
  #define NPACK 10
  #define PORT 9930

  #define SRV_IP "10.1.1.4"
  /* diep(), #includes and #defines like in the server */

  void diep(char *s)
  {
    perror(s);
    exit(1);
  }


  int main(void)
  {
   struct sockaddr_in si_other;
   int s, i, slen=sizeof(si_other);
   char buf[BUFLEN];

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
      diep("socket");

    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);
    if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
     fprintf(stderr, "inet_aton() failed\n");
      exit(1);
    }

    for (i=0; i<NPACK; i++) {
      printf("Sending packet %d\n", i);
      sprintf(buf, "This is packet %d\n", i);
      if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
        diep("sendto()");
    }

    close(s);
    return 0;
  }
udp.server.c

Code: Select all

  #include <arpa/inet.h>
  #include <netinet/in.h>
  #include <stdio.h>
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <unistd.h>

  #define BUFLEN 512
  #define NPACK 10
  #define PORT 9930

  void diep(char *s)
  {
    perror(s);
    exit(1);
  }

  int main(void)
  {
    struct sockaddr_in si_me, si_other;
    int s, i, slen=sizeof(si_other);
    char buf[BUFLEN];

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
      diep("socket");

    memset((char *) &si_me, 0, sizeof(si_me));
    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(s, &si_me, sizeof(si_me))==-1)
        diep("bind");

    for (i=0; i<NPACK; i++) {
      if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
        diep("recvfrom()");
      printf("Received packet from %s:%d\nData: %s\n\n",
             inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
    }

    close(s);
    return 0;
 }
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

sag007
Posts: 1
Joined: Mon Aug 13, 2018 4:30 am

Re: Receive UDP

Mon Aug 13, 2018 4:34 am

Hey All,

I'm new to the whole programming thing and have been experiencing issues with the supplied code, it gives the following error when I compile it;

In file included from Socket.c:6:0:
/usr/include/arm-linux-gnueabihf/sys/socket.h:174:16: note: expected ‘int’ but argument is of type ‘int (*)(int, int, int)’
extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,

It does however seem to be listening but doesn't quite receive the message and print it out.

Thanks in advance for any help you can offer.

Nathan

Return to “C/C++”