udpclient.c

来自「基于实时嵌入式系统的voip系统(real time embeded syste」· C语言 代码 · 共 93 行

C
93
字号
/**************************************************************************
*  udpClient.c - UDP client
*
*  Author - Ed Hursey
*  Date - November 2005
*  Note - This file is based on the udp client example
*         from the VxWorks Netguide manual.
*  Description - This file contains functions for creating
*  a client socket, closing that socket and sending requests
*  across that socket.
*
**************************************************************************/
#include "vxWorks.h"
#include "sockLib.h"
#include "inetLib.h"
#include "stdioLib.h"
#include "strLib.h"
#include "hostLib.h"
#include "ioLib.h"
#include "voip.h"


/**************************************************************************
*   Function - createUdpClient
*   Parameters - serverName - ip address of the server that we will
*     send messages to.
*   Returns - OK, or ERROR if server not found
*   Purpose - Create a udp connection.
*   Note - This function leaves the socket open, so it must be closed explicity
*      with the close function.  This prevents the client from having to create
*      a new socket for each request it sents.
/**************************************************************************/
STATUS createUdpClient( char * serverName /* name or IP address of server */ )
{
   /* create client's socket */
   if ((sFd = socket (AF_INET, SOCK_DGRAM, 0)) == ERROR)
   {
      perror ("socket");
      return (ERROR);
   }

   /* bind not required - port number is dynamic */
   /* build server socket address */
   sockAddrSize = sizeof (struct sockaddr_in);
   bzero ((char *) &serverAddr, sockAddrSize);
   serverAddr.sin_len = (u_char) sockAddrSize;
   serverAddr.sin_family = AF_INET;
   serverAddr.sin_port = htons (SERVER_PORT_NUM);

   if (((serverAddr.sin_addr.s_addr = inet_addr (serverName)) == ERROR) &&
       ((serverAddr.sin_addr.s_addr = hostGetByName (serverName)) == ERROR))
   {
      perror ("unknown server name");
      close (sFd);
      return (ERROR);
   }

   return (OK);
}

/**************************************************************************
*   Function - udpSendRequest
*   Parameters - None
*   Returns - OK, or ERROR if server not found
*   Purpose - Sends a request to the server over the socket that was
*     created with createUdpClient function.  The message that is sent is
*     retrievied from the global transmitRequest object.
*   Note - This function assumes the socket has already been created.
/**************************************************************************/
STATUS udpSendRequest()
{
   /* send request to server */
	if (sendto (sFd, (caddr_t) &transmitRequest, sizeof (transmitRequest), 0,
		(struct sockaddr *) &serverAddr, sockAddrSize) == ERROR)
	{
		perror ("sendto");
		close (sFd);
		return (ERROR);
	}
   return (OK);
}

/**************************************************************************
*   Function - closeUdpClient
*   Parameters - None
*   Returns - None
*   Purpose - This closes the client socket that was created with the
*     createUdpClient function.
/**************************************************************************/
VOID closeUdpClient()
{
   close (sFd);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?