📄 vxserver.c
字号:
/* vxServer.c - simple UDP demo server */
/* Copyright 1984-1994 Wind River Systems, Inc. */
/*
modification history
--------------------
01d,09oct97,dlk fixed val/buf problem, cleaned up.
01c,09aug96,tnm removed ntohl() on val - version 4.1.2 of class.
01b,05dec94,bss cleaned up. WRS coding conventions enforced.
01a,???????,??? written.
*/
/*
DESCRIPTION
This module provides a simple example of the issues involved in
sending data between different machines: these machines
might store data with different byte orderings, structure
alignments, floating-point representations, etc.
In this example, we are only sending an integer to the server so
we will only deal with byte ordering. The client converts
the data from a machine-dependent byte-ordering
to a standard representation (network byte ordering) and
sends the data to the server. The server then converts the data
from the standard representation to the machine-dependent
representation on the server. Finally the server sends
a reply to the client.
*/
/* includes */
#include "vxWorks.h"
#include "sockLib.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "inetLib.h"
#include "ioLib.h"
#include "string.h"
#include "stdio.h"
#include "taskLib.h"
/* typedefs */
typedef int SOCK_FD;/*
/* forward declarations */
LOCAL void error (char * str);
/******************************************************************************
*
* vxServer - processes UDP client requests
*
* This routine sits in an infinite loop handling client requests.
* When a request is received, the client's data is converted
* to the local byte-ordering and then displayed. Next,
* a reply is sent to the client. Then the server will process
* the next request or block if there is none.
*/
void vxServer (u_short port)
{
int clientAddrLength;
SOCK_FD sockFd;
struct sockaddr_in clientAddr;
struct sockaddr_in srvAddr;
char inetAddr[INET_ADDR_LEN];
u_short clientPort;
char *reply = "Here is your reply\n";
int val;
clientAddrLength = sizeof (clientAddr);
/* Create a socket */
if ( (sockFd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
error ("Socket failed");
/*
* Bind to a well known address. INADDR_ANY says
* any network interface will do. htonX()
* routines put things in network byte order.
*/
bzero ((char *)&srvAddr, sizeof(srvAddr));
srvAddr.sin_family = AF_INET;
srvAddr.sin_port = htons(port);
srvAddr.sin_addr.s_addr = INADDR_ANY;
if (bind (sockFd, (struct sockaddr *) &srvAddr, sizeof(srvAddr)) < 0)
{
close (sockFd);
error ("Bind failed");
}
FOREVER
{
if (recvfrom (sockFd, (char *) &val, sizeof(val), 0,
(struct sockaddr *) &clientAddr,
&clientAddrLength) < 0)
{
close (sockFd);
error ("recvfrom failed");
}
val = ntohl (val);
inet_ntoa_b (clientAddr.sin_addr, inetAddr);
clientPort = ntohl (clientAddr.sin_port);
printf ("Message received from client "
"(port = %d, inet = %s):\n",
clientPort, inetAddr);
printf ("%d\n", val);
if (sendto (sockFd, reply, strlen(reply) + 1,
0,(struct sockaddr *) &clientAddr,
clientAddrLength) < 0)
{
close (sockFd);
error ("sendto failed");
}
}
}
void error (char * str)
{
perror (str);
exit (1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -