首页 vxworks下udp通信的实现

vxworks下udp通信的实现

举报
开通vip

vxworks下udp通信的实现vxworks下udp通信的实现 -------------------------------------------------------------------------------- /* udpExample.h - header used by both UDP server and client examples */ #define SERVER_PORT_NUM 5002 /* server's port number for bind() */ #define REQUEST_MSG_S...

vxworks下udp通信的实现
vxworks下udp通信的实现 -------------------------------------------------------------------------------- /* udpExample.h - header used by both UDP server and client examples */ #define SERVER_PORT_NUM 5002 /* server's port number for bind() */ #define REQUEST_MSG_SIZE 1024 /* max size of request message */ /* structure used for client's request */ struct request { int display; /* TRUE = display message */ char message[REQUEST_MSG_SIZE]; /* message buffer */ }; -------------------------------------------------------------------------------- /* udpClient.c - UDP client example */ /* DESCRIPTION This file contains the client-side of the vxWorks UDP example code. The example code demonstrates the usage of several BSD 4.4-style socket routine calls. */ /* includes */ #include "vxWorks.h" #include "sockLib.h" #include "inetLib.h" #include "stdioLib.h" #include "strLib.h" #include "hostLib.h" #include "ioLib.h" #include "udpExample.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 */ ) { 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); } /* explicit bind not required - local 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); } /* build request, prompting user for message */ printf ("Message to send: \n"); mlen = read (STD_IN, myRequest.message, REQUEST_MSG_SIZE); myRequest.message[mlen - 1] = '\0'; printf ("Would you like the server to display your message (Y or N): \n"); read (STD_IN, &display, 1); switch (display) { case 'y': case 'Y': myRequest.display = TRUE; break; default: myRequest.display = FALSE; break; } /* send request to server */ if (sendto (sFd, (caddr_t) &myRequest, sizeof (myRequest), 0, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) { perror ("sendto"); close (sFd); return (ERROR); } close (sFd); return (OK); } -------------------------------------------------------------------------------- /* udpServer.c - UDP server example */ /* DESCRIPTION This file contains the server-side of the vxWorks UDP example code. The example code demonstrates the usage of several BSD 4.4-style socket 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" /********************************************************************* * * 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) { 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_len = (u_char) 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 (clientRequest.display) { /* 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); } } }
本文档为【vxworks下udp通信的实现】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_597436
暂无简介~
格式:doc
大小:25KB
软件:Word
页数:7
分类:
上传时间:2018-03-21
浏览量:130