HP Rp3440-4 - 9000 - 0 MB RAM Programmer's Manual page 56

Bsd sockets interface programmer’s guide
Hide thumbs Also See for Rp3440-4 - 9000 - 0 MB RAM:
Table of Contents

Advertisement

Using Internet Stream Sockets
Example Using Internet Stream Sockets
* addrlen needs to be passed in as a pointer,
* because getsockname returns the actual length
* of the address.
*/
addrlen = sizeof(struct sockaddr_in);
if (getsockname(s, &myaddr_in, &addrlen) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to read socket address\n",
exit(1);
}
/* Print out a startup message for the user. */
time(&timevar);
/* The port number must be converted first to
* host byte order before printing.
* this is not necessary, but the ntohs() call is
* included here so this program could easily be
* ported to a host that does require it.
*/
printf("Connected to %s on port %u at %s",
ctime(&timevar));
/* This sleep simulates any preliminary processing
* that a real client might do here.
*/
sleep(5);
/* Send out all the requests to the remote server.
* In this case five are sent but any random number
* could be used. The first four bytes of buf are
* set up to contain the request number. This
* number will be returned in the server's reply.
*/
/* CAUTION: If you increase the number of requests
* sent or the size of the requests, you should be
* aware that you could encounter a deadlock
* situation.
* sockets can only queue a limited amount of
* data on their receive queues.
*/
for (i=1; i<=5; i++) {
*buf = i;
if (send(s, buf, 10, 0) != 10) {
fprintf(stderr, "%s: Connection aborted on error ",
}
}
/* Now, shutdown the connection for further sends.
* This causes the server to receive an end-of-file
* condition after receiving all the requests that
* have just been sent, indicating that no further
* requests will be sent.
*/
56
argv[0]);
argv[1], ntohs(myaddr_in.sin_port),
Both the client's and server's
argv[0]);
fprintf(stderr, "on send number %d\n", i);
exit(1);
On most hosts,
Chapter 2

Advertisement

Table of Contents
loading

Table of Contents