📄 udp_server.cxx
字号:
/* ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */static const char* const udp_server_cxx_Version = "$Id: udp_server.cxx,v 1.6 2000/12/18 23:42:39 bko Exp $";#ifdef __vxworks/* udpServer.c - UDP server example *//*DESCRIPTIONThis file contains the server-side of the VxWorks UDP example code.The example code demonstrates the useage of several BSD 4.3-stylesocket routine calls.*//* includes */#include "vxWorks.h"#include "sockLib.h"#include "inetLib.h"#include "stdioLib.h"#include "strLib.h"#include "ioLib.h"#include "fioLib.h"#include "udpExample.h"#include "resolvLib.h"/********************************************************************* * * udpServer - read from UDP socket and display client's message if requested * * Example of VxWorks UDP server: * -> sp udpServer * task spawned: id = 0x3a1f6c, name = t2 * value = 3809132 = 0x3a1f6c * -> MESSAGE FROM CLIENT (Internet Address 150.12.0.11, port 1028): * Greetings from UDP client * * RETURNS: Never, or ERROR if a resources could not be allocated. */STATUS udpServer (void){#ifdef __vxworks int rc = resolvInit ("192.168.5.254", "private.vovida.com", 0);#endif struct sockaddr_in serverAddr; /* server's socket address */ struct sockaddr_in clientAddr; /* client's socket address */ struct request clientRequest; /* request/Message from client */ int sockAddrSize; /* size of socket address structure */ int sFd; /* socket file descriptor */ char inetAddr[INET_ADDR_LEN]; /* buffer for client's inet addr */ /* set up the local address */ sockAddrSize = sizeof (struct sockaddr_in); bzero ((char *) &serverAddr, sockAddrSize); serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons (SERVER_PORT_NUM); serverAddr.sin_addr.s_addr = htonl (INADDR_ANY); /* create a UDP-based socket */ if ((sFd = socket (AF_INET, SOCK_DGRAM, 0)) == ERROR) { perror ("socket"); return (ERROR); } /* bind socket to local address */ if (bind (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) { perror ("bind"); close (sFd); return (ERROR); } /* read data from a socket and satisfy requests */ FOREVER { if (recvfrom (sFd, (char *) &clientRequest, sizeof (clientRequest), 0, (struct sockaddr *) &clientAddr, &sockAddrSize) == ERROR) { perror ("recvfrom"); close (sFd); return (ERROR); } /* if client requested that message be displayed, print it */ if (1) { /* convert inet address to dot notation */ inet_ntoa_b (clientAddr.sin_addr, inetAddr); printf ("MSG FROM CLIENT (Internet Address %s, port %d):\n%s\n", inetAddr, ntohs (clientAddr.sin_port), clientRequest.message); } }}#elseint main(){ return 0;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -