IPX/SPX Programming Manual

Sample Programs
HP NonStop IPX/SPX Programming Manual528022-001
6-2
Client and Server Programs Using SPXII
int sock, newsock;
int bytesReceived;
int fromlen;
char recvbuf[DATA_BUF_SIZE];
/*
* Specify the name of the NonStop IPX/SPX
* Protocol Process if required.
*/
socket_set_nv_name("$NV2A");
/*
* Open a socket.
*/
if ((sock = socket(AF_NV, SOCK_STREAM, 0)) < 0) {
perror("SPX Server: socket failure");
exit(1);
}
memset(&snv, 0, sizeof(snv));
snv.snv_family = AF_NV;
snv.snv_addr.s_port = WELL_KNOWN_PORT;
/*
* Bind to the socket that we intend to listen on.
*/
if (bind(sock, (struct sockaddr *)&snv, sizeof(snv)) < 0)
{
perror("SPX Server: bind failure");
exit(1);
}
/*
* Set up socket for incoming connection requests.
*/
if (listen(sock, 5) < 0) {
perror("SPX Server: listen failure");
exit(1);
}
for(;;) {
/*
* Wait for a connection request.
*/
printf("SPX Server: waiting for connection request\n");
fromlen = sizeof(from);
newsock = accept(sock,(struct sockaddr*)&from, &fromlen);
if (newsock < 0) {
perror("SPX Server: accept failure");
exit(1);
}
printf("SPX Server: connected to port 0x%hx\n",
from.snv_addr.s_port);
for (;;) {
/*
* Receive data until either an EOF or error is received.
*/
if ((bytesReceived = recv(newsock, recvbuf, DATA_BUF_SIZE, 0)) <
0) {
perror("SPX Server: recv failure");
FILE_CLOSE_((short)newsock);