Simple Udp Send/Receive

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Simple Udp Send/Receive

Jump to solution
1,546 Views
nolle
Contributor I

Hi,

I am quite new to the MQX and I am having trouble narrowing down which functions to use to simply send and receive a udp packet. I'm pretty sure I have to use socket(), bind(), sendto() and recvfrom() but I don't know what I need to pass to these functions. I am using MQX 3.8 and M52259. Any help would be extremely appreciated.

 

Thank you

Labels (1)
Tags (1)
0 Kudos
1 Solution
605 Views
drummer
Contributor IV
/* This installs the socket and sets up for udp datagram */
    _io_socket_install("socket:");
udp_addr.sin_family      = AF_INET;
udp_addr.sin_port        = 5050;
udp_addr.sin_addr.s_addr = INADDR_ANY;
sock2 = socket(PF_INET, SOCK_DGRAM, 0);
return_error_if(sock2 == RTCS_SOCKET_ERROR);
error = bind(sock2, &udp_addr, sizeof(udp_addr));
return_error_if(error != RTCS_OK);
/* this waits for a UDP datagram */
  while (1) 
    {
  udp_sock = RTCS_selectall(0);
return_error_if(udp_sock == RTCS_SOCKET_ERROR);
if(udp_sock == sock2)
{
/* Datagram socket received data. */
memset(&udp_addr, 0, sizeof(udp_addr));// clean out ip info
memset(&udp_union.udp_buffer, 0, UDP_BUF_LEN); //clean out buffer
rlen = sizeof(udp_addr);
peer_adr_len = recvfrom(sock2, udp_union.udp_buffer, UDP_BUF_LEN, 0, &udp_addr, &rlen);
/* I like to use union because I'm old school */
    union
    {
in_struct in_msg;
    out_struct out_msg;
    uchar udp_buffer[UDP_BUF_LEN];
    }udp_union;
if (peer_adr_len == RTCS_ERROR) 
{
continue;
}
else
{
memcpy(&remote, &udp_addr, sizeof(remote)); // capture a copy of ip info
remote.sin_addr.s_addr = INADDR_BROADCAST;
if( strcmp(udp_union.in_msg.company_text,search_name))
continue;
/* the UDP is broadcast to all so I have to make sure its for me */
/* sending formatted data */
error = sendto(sock2,&udp_union.out_msg,512,0,&remote, sizeof(remote));
EASY PEESY PUDDIN PIE

 
 

View solution in original post

0 Kudos
3 Replies
606 Views
drummer
Contributor IV
/* This installs the socket and sets up for udp datagram */
    _io_socket_install("socket:");
udp_addr.sin_family      = AF_INET;
udp_addr.sin_port        = 5050;
udp_addr.sin_addr.s_addr = INADDR_ANY;
sock2 = socket(PF_INET, SOCK_DGRAM, 0);
return_error_if(sock2 == RTCS_SOCKET_ERROR);
error = bind(sock2, &udp_addr, sizeof(udp_addr));
return_error_if(error != RTCS_OK);
/* this waits for a UDP datagram */
  while (1) 
    {
  udp_sock = RTCS_selectall(0);
return_error_if(udp_sock == RTCS_SOCKET_ERROR);
if(udp_sock == sock2)
{
/* Datagram socket received data. */
memset(&udp_addr, 0, sizeof(udp_addr));// clean out ip info
memset(&udp_union.udp_buffer, 0, UDP_BUF_LEN); //clean out buffer
rlen = sizeof(udp_addr);
peer_adr_len = recvfrom(sock2, udp_union.udp_buffer, UDP_BUF_LEN, 0, &udp_addr, &rlen);
/* I like to use union because I'm old school */
    union
    {
in_struct in_msg;
    out_struct out_msg;
    uchar udp_buffer[UDP_BUF_LEN];
    }udp_union;
if (peer_adr_len == RTCS_ERROR) 
{
continue;
}
else
{
memcpy(&remote, &udp_addr, sizeof(remote)); // capture a copy of ip info
remote.sin_addr.s_addr = INADDR_BROADCAST;
if( strcmp(udp_union.in_msg.company_text,search_name))
continue;
/* the UDP is broadcast to all so I have to make sure its for me */
/* sending formatted data */
error = sendto(sock2,&udp_union.out_msg,512,0,&remote, sizeof(remote));
EASY PEESY PUDDIN PIE

 
 
0 Kudos
605 Views
nolle
Contributor I

First, thanks for the quick reply I do have a couple questions. What libraries would I have to include with this code because I am getting a substantial number of errors when I compile it.

Ex.

UDP_addr.sin_family      = AF_INET; 

>not a struct/union/class

and

= return_error_if(sock2 == RTCS_SOCKET_ERROR); 

>function has no prototype

 

Also I was wondering if you had missed some closing brackets.

 

Thank you

0 Kudos
605 Views
drummer
Contributor IV

There is a nice demo example in the rtcs folder under examples called ipc_udp.

 

 sockaddr_in    udp_addr, remote;

defined in rtcs.h

/*
** Socket Address Structure
*/
typedef struct sockaddr_in {
uint_16 sin_family;
uint_16 sin_port;
in_addr sin_addr;
} sockaddr_in;

 

This is a define.

I don't know where I got it.

I seldom write my own code.

I like to cut and paste.

#define return_error_if(c) { if (c) _mqx_exit(0); }

 


0 Kudos