Sockets
(Part II)
LESSON 22

(November 17, 1995)
Daniel Z. Tabor Jr.
New Jersey Institute of Technology


Outline:
Sockets (Part II)


[Outline]

Iterative / Concurrent Servers:

[Outline]

Socket System Calls:
Select

[Outline]

Obtaining Local and Remote
Socket Addresses:

[Outline]

Obtaining and Setting
Socket Options:

[Outline]

Obtaining and Setting
Host Names:

[Outline]

Obtaining and Setting
Internal Host Domain:

[Outline]

Obtaining and Setting
The Time of Day:

[Outline]

System Calls vs. Network Library Calls:

[Outline]

Accessing the Domain Naming System:

[Outline]

Obtaining Information about
Hosts:

[Outline]

Obtaining Information about
Networks:

[Outline]

Obtaining Information about
Protocols:

Library routines to provide access to the database of protocols available on a machine.
[Outline]

Obtaining Information about
Network Services:

[Outline]

Routines for Integer Conversions:

[Outline]

IP-Address Manipulation Routines:

[Outline]

Socket Call Traces:
Trumpet WinSock

[Outline]

Recommend Socket Programming Steps for
Client Side "Whois":

[Outline]

Recommend Socket Programming Steps for:
Server Side "Whois":

[Outline]

Socket Structures:
Sockaddr_in

struct sockaddr_in {
u_short sin_family;
u_short sin_port;
u_longs in_addr;
char sin_zero[8];
/* Structure to hold addr */
/* type of addr */
/* protocol port number */
/* IP address */
/* unused (set to zero) /*
[Outline]

Socket Structures:
Hostent

struct hostent {
char
char
int
int
char
}
*h_name;
**h_aliases;
h_addrtype;
h_length;
**h_addr_list;
/* official host name */
/* other aliases */
/* address type */
/* address length */
/* list of addresses */
#define h_addr h_addr_list[0]
[Outline]

Socket Structures:
Servent

struct servent {
char
char
int
char
*s_name;
**s_aliases;
s_port;
*s_proto;
/* official service name */
/* other aliases */
/* port for this service */
/* protocol to use */
};
[Outline]

Socket Structures:
Protoent

struct protoent {
char
char
int
*p_name;
**p_aliases;
*p_proto;
/* official protocol name */
/* list of aliases allowed */
*p_proto; /* official protocol # */
};
[Outline]

Sample Socket Program:
Client-side

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
/* Define the port number for client/server */
#define SERV_TCP_PORT
#define BSIZE
6305
256
main (argc, argv)
int argc;
char *argv[];
{
int
int
FILE
char
char
char
i, j, s;
len, readin;
*fp;
*myname, c;
*host;
*fname;
char buf[BUFSIZ+1];
struct sockaddr_in sa;
struct hostent *hp;
/* socket addr. structure */
/* host name lookup */
myname = argv[0];
if (argc != 3)
{ fprintf(stderr,
"Usage: %s host username.\n", myname);
exit(1);
}
host = argv[1];
fname = argv[2];
/* Get host information from "hosts" database, such
as, h_addr: address, h_addrtype: host address type */
if ((hp = gethostbyname(host)) == NULL)
{ fprintf(stderr, "%s: %s: no such host!\n", myname, host);
exit( 1 );
}
/* Open the input file */
if ( (fp = fopen(fname, "r")) == NULL )
{ fprintf(stderr, "%s: %s: no such file!\n", myname, fname);
exit( 1 ); }
/* Get data from the input file */
j = 0;
while ((c = fgetc(fp)) != EOF)
{ buf [j++] = c; }
buf [j] = EOF;
/* Fill in the "sockaddr_in" structure with some
information from "hostent" structure. */
sa.sin_family = hp->h_addrtype;
sa.sin_port = htons(SERV_TCP_PORT);
bcopy ((char *)hp->h_addr, (char *)&sa.sin_addr, hp- >h_length );
/* Open a TCP/STREAM socket */
if ( (s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
{ perror("socket");
exit(1) }
/* Setup connection to the remote server */
if (connect(s, &sa, sizeof sa) < 0)
{ perror("connect");
exit(1) }
/* Send the messages */
write(s, buf, strlen(buf), 0);
/* Receive the reply and print it out */
bzero((char *) buf, sizeof(buf));
while ( (len = read(s, buf, BUFSIZ)) > 0)
{
printf("Receiving from server: %s\n", host);
printf(">>> %s\n", buf);
}
/* Close the socket */
close(s);
exit(0);
}

[Outline]

(Home)

(Calendar)

(Materials)

(Students)

(Professor)


Last Modification: (Sunday, August 25, 1996)