IPX/SPX Programming Manual

Sample Programs
HP NonStop IPX/SPX Programming Manual528022-001
6-5
Sample SAP Programs
#define PROCESS_NAME "$NV3A"
main(int argc, char *argv[])
{
struct sockaddr_nv snv;
char buf[DATA_BUF_SIZE];
int bytesSent;
int sock;
nvaddr_t *pNvaddr;
nvaddr_t *find_server(char *pServer, short type);
socket_set_nv_name(PROCESS_NAME);
/*
* Look for server in the SAP table.
*/
pNvaddr = find_server(SERVER_NAME, SERVER_TYPE);
if (pNvaddr == NULL) {
fprintf(stderr, "SPX Client: server %s not found\n", SERVER_NAME);
exit(0);
}
/*
* Open a socket.
*/
sock = socket(AF_NV, SOCK_STREAM, 0);
if (sock < 0) {
perror("SPX Client: socket failure");
exit(1);
}
/*
* Set up the the remote socket address.
* This includes the address family, the 4 byte net,
* the 6 byte remote host, and the 2 byte remote
* port (i.e. remote IPX socket number.)
*/
snv.snv_family = AF_NV;
memcpy(&snv.snv_addr, pNvaddr, sizeof(nvaddr_t));
/*
* Connect to remote.
*/
if (connect(sock, (struct sockaddr *)&snv, sizeof(snv)) < 0) {
perror("SPX Client: connect failure");
close(sock);
exit(1);
}
printf("SPX Client: connected to port 0x%hx\n",
snv.snv_addr.s_port);
/*
* Send data to remote. Note that the number of bytes
* sent could be less than DATA_BUF_SIZE.
*/
bytesSent = send(sock, buf, DATA_BUF_SIZE, 0);
if (bytesSent == -1) {
perror("SPX Client: send failure");
close(sock);
exit(1);
}
printf("SPX Client: sent %d bytes\n", bytesSent);
/*
* Close the socket.
*/