Posts: 22
Joined: Sun Mar 06, 2016 9:22 pm

netbeans UDP

Thu Mar 17, 2016 4:49 pm

I would like to make a client/server application between my pi and my PC.
The pi will send UPD packet every 100ms and the PC send packet to the pi every 100ms too.
I don't know if the server should be on the pi or on the PC ?
My first step will be to send packet from the pi and print on a console the packet receive from the PC.
Is someone have a link that explain how to use UDP on a PI 3 with netbeans?

Thanks you
pi3 with raspbian
C/C++ cross compilation with netbeans.

User avatar
Posts: 113
Joined: Sun Mar 31, 2013 8:28 pm
Location: Top of the food chain
Contact: Website

Re: netbeans UDP

Fri Mar 18, 2016 9:54 am

UDP is very similar to TCP but there are some fundamental mechanics differences..

For UDP server:
  • Create a socket
  • Bind
  • !! Note: Do not listen() as you would in TCP
  • Receive (loop)
For UDP Client:
  • Create a socket
  • !! Note: Do not connect() as you would in TCP
  • Send packet
Server Code:

Code: Select all

int ListenPort=8888;
string ReplyToSend="PONG!";

int listen_fd;
int reuseaddr_on = 1;
struct sockaddr_in listen_addr;
struct sockaddr_in remaddr; storage for // remote address
socklen_t addrlen = sizeof(remaddr);

char* ReceiveBuffer=new char[16384];
int ReceiveBufferSize=16384;

listen_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (listen_fd < 0) err(1, "listen failed");
if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr_on, sizeof(reuseaddr_on)) == -1) err(1, "setsockopt failed");
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = INADDR_ANY;
listen_addr.sin_port = htons(ListenPort);
if (bind(listen_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) err(1, "bind failed");
for ( ;; ) {
    int retval = recvfrom(listen_fd, ReceiveBuffer, ReceiveBufferSize, 0, (struct sockaddr *)&remaddr, &addrlen));
    // Print the received message:
    if (retval > 0) { ReceiveBuffer[retval] = 0; printf("Received message: \"%s\"\n", ReceiveBuffer); }
    // optionally send a "PONG!" reply packet back to the client.
    sendto(listen_fd, ReplyToSend.c_str(), ReplyToSend.length(), 0, (struct sockaddr *)&remaddr, addrlen);
Client Code:

Code: Select all

struct sockaddr_in serverSockAddr;
struct hostent *serverHostEnt;
unsigned long hostAddr;

string Server="yourserver.yourdomain";
string TextToSend="PING!";
int SendPort=8888;

int sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero((char*)&serverSockAddr, sizeof(serverSockAddr)); // clear the structure
hostAddr = inet_addr(Server.c_str()); // convert IP address to long
if ( (long)hostAddr != (long)-1)
        // If "Server" is an actual IP address, this will succeed so we can use the result right away
    else {
        // Otherwise we have to resolve using gethostbyname()
        serverHostEnt = gethostbyname(Server.c_str());
        if (!serverHostEnt) { fprintf(stderr, "could not obtain address of %s\n", Server.c_str()); return 0; }
        if (serverHostEnt == NULL) { puts("Gethost problem"); return false; }
        bcopy(serverHostEnt->h_addr,&serverSockAddr.sin_addr, serverHostEnt->h_length);

    serverSockAddr.sin_family = AF_INET;
    serverSockAddr.sin_port = htons(SendPort);

    if (sendto(sockfd, TextToSend.c_str(), TextToSend.length(), 0, (struct sockaddr *)&serverSockAddr, sizeof(serverSockAddr)) < 0) { perror("sendto failed"); return 0; }
If you choose to use two-way communication over one UDP connection (send the optional "PONG!" message back to the client) then you'll have to set up a recv for that in the client code.

Either do that or have each participating machine run in client as well as server mode. When using UDP you can go either way, depends on the implementation really.

Note I pulled this code together from a few custom libraries, hence there is mixed use of string/char etc. But the aim is to show the specific steps involved and hopefully save you some time struggling with the common pitfalls in coding UDP applications.


Return to “C/C++”