Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VxWorks And The Socket Receive Function

944 views
Skip to first unread message

Bruce Friedman

unread,
Jan 28, 1998, 3:00:00 AM1/28/98
to

VxWorks Gurus -

I hope someone can help as this problem has been driving me crazy for
almost a week now ->

PROBLEM:
I am trying to create a function that will continually examine and read
data from a non-blocking UDP Socket (the non-blocking part is important
here). I can create the socket okay, but I cannot get it to read in a
non-blocking mode. If I use the ioctl() function to set FIONBIO to be non
blocking - it seems to never read anything; just passing over the recvfrom
function like it wasn't there. However - if I put a statement (printf();)
just before or just after the recvfrom statement, it works fine. I have
also been able to successfully get it to block read - waiting until
something is there - but this is no good for me. If I leave out the
ioctl() call, it defaults to blocking. I have also put in an external
delay timer as well as making all variables global. Why oh why does
inserting a printf() work???? Since I can not have a printf() in the final
version of my code, unfortunately this is not an option. I have placed the
code that I am using at the end of this E-Mail in hopes that someone can
find something wrong with it

SYSTEM:
VxWorks version V5.2C
KERNEL: WIND version 2.4
Copyright Wind River Systems, Inc., 1984-1997
CPU: Digital Alpha VME 4/288. Processor #0
Memory Size: 0xd18400.
BSP version V5.2C
DEC Tulip Ethernet Drivers

SAMPLE CODE:
typedef unsigned int in_addr_t;
#include "errno.h"
#include "sys/types.h"
#include "time.h"
#include "vxWorks.h"
#include "fioLib.h" /* printf */
#include "ioLib.h"
#include "stdioLib.h"
#include "taskLib.h"
#include "memLib.h" /* free malloc */
/* #include "varargs.h" */

#include "sys/socket.h" /* bind recvfrom sendto setsockopt shutdown socket */
#include "sys/ioctl.h" /* bind recvfrom sendto setsockopt shutdown socket */
#include "netinet/in.h" /* inet_addr inet_ntoa_b */
#include "socketvar.h" /* bind recvfrom sendto setsockopt shutdown socket */
#include "arpa/inet.h"

#include "inetLib.h" /* inet_addr inet_ntoa_b */
#include "hostLib.h" /* gethostname */
#include "etherLib.h"
#include "ifLib.h"
#include "if_tu.h"
#include "netdb.h"

typedef UINT_32 SOCKET;
#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR (-1)

void main(void)
{
char Recv_Data[4096];
SOCKET The_Socket;
UINT_16 The_Port = 5001;
UINT_16 The_Type = SOCK_DGRAM;
UINT_16 The_Peers_IP = INADDR_ANY;
int Sockaddr_Size = sizeof(struct sockaddr_in);
int Bytes_Rxed = 0;
unsigned long Sock_Opt_ul = 0;
struct sockaddr_in P2P_Rx_Sockaddr_Info;
struct sockaddr_in Rx_Sockaddr_Info;
double Run_Duration_Sec = 100.0;
int Done = 0;
int fionbio = FIONBIO;
int Counter = 0;


/*------------------------------------*/
/* Create the peer to peer Rx socket. */
bzero((char *)&P2P_Rx_Sockaddr_Info, Sockaddr_Size);
P2P_Rx_Sockaddr_Info.sin_family = AF_INET;
P2P_Rx_Sockaddr_Info.sin_addr.s_addr = htonl(The_Peers_IP);
P2P_Rx_Sockaddr_Info.sin_port = htons(The_Port);


The_Socket = socket(AF_INET, SOCK_DGRAM, 0);
ioctl(The_Socket, fionbio, &Sock_Opt_ul);
bind(The_Socket, (struct sockaddr *) &P2P_Rx_Sockaddr_Info, Sockaddr_Size);

printf("\nI'm looking for friends on port %u.\n", The_Port);

/* Messaging loop. */
while (!Done)
{
/* Receive data on this port (non-blocking) and process it. */
Bytes_Rxed = recvfrom(The_Socket, Recv_Data, 4096, 0,
(struct sockaddr *) &Rx_Sockaddr_Info, &Sockaddr_Size);

if (Bytes_Rxed > 0)
printf("I received %d Bytes of Data \n", Bytes_Rxed);

Counter++;
Done = (Counter == 1000);

}

/* Goodbye. */
shutdown(The_Socket, 2);
close (The_Socket);
}

--------------------------------------------------------------------------

_/
_/_/_/_/_/_/ Bruce Friedman | Amherst Systems, Inc
FROM _/_/_/_/ b...@amherst.com | 30 Wilson Rd.
_/_/_/_/_/_/ Voice 716-631-0610 x213 | Buffalo, NY 14221
_/ Fax 716-631-0629 |


Gordon Uchenick

unread,
Jan 28, 1998, 3:00:00 AM1/28/98
to

Here's what's wrong with your sample code. I have no idea why the =
additional printf would make things work or not work. It seems to me =
that the select() function is much more straightforward. Why don't you =
consider using that?

PROBLEM:
I am trying to create a function that will continually examine and read
data from a non-blocking UDP Socket (the non-blocking part is important
here). I can create the socket okay, but I cannot get it to read in a

non-blocking mode.=20
<SNIP>


void main(void)
{
char Recv_Data[4096];
SOCKET The_Socket;

UINT_16 The_Port =3D 5001;
UINT_16 The_Type =3D SOCK_DGRAM;
UINT_16 The_Peers_IP =3D INADDR_ANY;
int Sockaddr_Size =3D sizeof(struct =
sockaddr_in);
int Bytes_Rxed =3D 0;

HERE'S THE PROBLEM! Sock_Opt_ul should be 1. You are turning OFF =
non-blocking I/O.

unsigned long Sock_Opt_ul =3D 0;


struct sockaddr_in P2P_Rx_Sockaddr_Info;
struct sockaddr_in Rx_Sockaddr_Info;

double Run_Duration_Sec =3D 100.0;
int Done =3D 0;
int fionbio =3D FIONBIO;
int Counter =3D 0;


/*------------------------------------*/
/* Create the peer to peer Rx socket. */
bzero((char *)&P2P_Rx_Sockaddr_Info, Sockaddr_Size);

P2P_Rx_Sockaddr_Info.sin_family =3D AF_INET;
P2P_Rx_Sockaddr_Info.sin_addr.s_addr =3D htonl(The_Peers_IP);
P2P_Rx_Sockaddr_Info.sin_port =3D htons(The_Port);


The_Socket =3D socket(AF_INET, SOCK_DGRAM, 0);
ioctl(The_Socket, fionbio, &Sock_Opt_ul);
bind(The_Socket, (struct sockaddr *) &P2P_Rx_Sockaddr_Info, =
Sockaddr_Size);

printf("\nI'm looking for friends on port %u.\n", The_Port);

/* Messaging loop. */
while (!Done)
{
/* Receive data on this port (non-blocking) and process it. */

YOU SHOULD PUT THIS INTO EACH LOOP ITERATION:

SockAddr_Size =3D sizeof(Rx_Sockaddr_Info);

Bytes_Rxed =3D recvfrom(The_Socket, Recv_Data, 4096, 0,


(struct sockaddr *) &Rx_Sockaddr_Info, &Sockaddr_Size);

if (Bytes_Rxed > 0)
printf("I received %d Bytes of Data \n", Bytes_Rxed);

Counter++;
Done =3D (Counter =3D=3D 1000);

}

<SNIP>

0 new messages