📄 udp_client.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_client_cxx_Version = "$Id: udp_client.cxx,v 1.5 2000/12/18 23:42:39 bko Exp $";#ifdef __vxworks#include "vxWorks.h"#include "sockLib.h"#include "inetLib.h"#include "stdioLib.h"#include "strLib.h"#include "hostLib.h"#include "ioLib.h"#include "udpExample.h"#include "resolvLib.h"/**************************************************************************** * * udpClient - send a message to a server over a UDP socket * * This routine sends a user-provided message to a server over a UDP socket. * Optionally, this routine can request that the server display the message. * This routine may be invoked as follows: * -> udpClient "remoteSystem" * Message to send: * Greetings from UDP client * Would you like server to display your message (Y or N): * y * value = 0 = 0x0 * * RETURNS: OK, or ERROR if the message could not be sent to the server. */STATUS udpClient( char * serverName /* name or IP address of server */){#ifdef __vxworks int rc = resolvInit ("192.168.5.254", "private.vovida.com", 0);#endif struct request myRequest; /* request to send to server */ struct sockaddr_in serverAddr; /* server's socket address */ char display; /* if TRUE, server prints message */ int sockAddrSize; /* size of socket address structure */ int sFd; /* socket file descriptor */ int mlen; /* length of message */ /* 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_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); } // connect( sFd, // (struct sockaddr*) &serverAddr, // sizeof(serverAddr)); /* build request, prompting user for message */ strcpy(myRequest.message, "hello"); myRequest.display = TRUE; /* send request to server */ if (sendto (sFd, (caddr_t) &myRequest, sizeof (myRequest), 0, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) // if (sendto (sFd, (caddr_t) &myRequest, sizeof (myRequest), 0, // (struct sockaddr *) 0, 0) == ERROR) { perror ("sendto"); close (sFd); return (ERROR); } close (sFd); return (OK);}#elseint main(){ return -1;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -