HP Rp7410 BSD Sockets Interface Programmer's Guide - Page 55
s = socket AF_INET, SOCK_STREAM, 0
View all HP Rp7410 manuals
Add to My Manuals
Save this manual to your list of manuals |
Page 55 highlights
Using Internet Stream Sockets Example Using Internet Stream Sockets int argc; char *argv[]; { int addrlen, i, j; /* This example uses 10 byte messages. */ char buf[10]; if (argc != 2) { fprintf(stderr, "Usage:%s \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", argv[0], argv[1]); 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", argv[0]); 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. Note that Chapter 2 55