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

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

Advertisement

int argc;
char *argv[];
{
int addrlen, i, j;
/* This example uses 10 byte messages. */
char buf[10];
if (argc != 2) {
fprintf(stderr, "Usage:%s <remote host>\n" argv[0];
exit(1);
}
/* clear out address structures */
memset ((char *)&myaddr_in, 0, sizeof(struct sockaddr_in));
memset ((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
/* Set up the peer address to which we will connect. */
peeraddr_in.sin_family = AF_INET;
/* Get the host information for the hostname that the
* user passed in.
*/
hp = gethostbyname (argv[1]);
/* argv[1] is the host name. */
if (hp == NULL) {
fprintf(stderr, "%s: %s not found in /etc/hosts\n",
exit(1);
}
peeraddr_in.sin_addr.s_addr=
((struct in_addr*)(hp->h_addr)->s_addr;
/* Find the information for the "example" server
* in order to get the needed port number.
*/
sp = getservbyname ("example", "tcp");
if (sp == NULL) {
fprintf(stderr, "%s: example not found in
/etc/services\n", argv[0]);
exit(1);
}
peeraddr_in.sin_port = sp->s_port;
/* Create the socket. */
s = socket (AF_INET, SOCK_STREAM, 0);
if (s == -1) {
perror(argv[0]);
fprintf(stderr,"%s: unable to create socket\n", argv[0])
exit(1);
}
/* Try to connect to the remote server at the
* address which was just built into peeraddr.
*/
if (connect(s, &peeraddr_in, sizeof(struct sockaddr_in)) ==-1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to connect to remote\n",
exit(1);
}
/* Since the connect call assigns a random address
* to the local end of this connection, let's use
* getsockname to see what it assigned.
Chapter 2
Using Internet Stream Sockets
Example Using Internet Stream Sockets
argv[0], argv[1]);
argv[0]);
Note that
55

Advertisement

Table of Contents
loading

Table of Contents