Topic : BSD Sockets
Author : Unknown
Page : << Previous 3  Next >>
Go to page :


perror("server: ");
      exit(1);
    }

    /* Create child server process.  Parent does no
       further processing -- it loops back to wait
       for another connection request.                */

    printf("Socket address in server %d is %x, %s\n",
      getpid(), myname.sa_data, myname.sa_data);

    if (fork() == 0) {  /* child process */
      close (sock);  /* child does not need it  */
      /*  . . . . .  */

      cnt = read(new_sd, buf, strlen(buf));
      printf ("Server with pid %d got message %s\n",
         getpid(), buf);
      strcpy (buf, "Message to client");
      cnt = write(new_sd, buf, strlen(buf));
      printf("Socket address in server %d is %x, %s\n",
        getpid(), myname.sa_data, myname.sa_data);

      /*  . . . . .  */
      close (new_sd); /* close prior to exiting  */
      exit(0);
    }   /* closing bracket for if (fork() ... )  */

  }  /* closing bracket for while (1) ... )    */

}  /* closing bracket for main procedure   */

/* client program */

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>

char buf[80];
struct sockaddr myname;

main()
{
  int   sock, adrlen, cnt;

  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock < 0) {
    printf("client socket failure %d\n", errno);
    perror("client: ");
    exit(1);
  }

  myname.sa_family = AF_UNIX;
  strcpy(myname.sa_data, "/tmp/billb");
  adrlen = strlen(myname.sa_data) +
      sizeof(myname.sa_family);

  if (connect( sock, &myname, adrlen) < 0) {
    printf("client connect failure %d\n", errno);
    perror("client: ");
    exit(1);
  }
  /*  . . . . .  */

  strcpy(buf, "Message sent to server");
  cnt = write(sock, buf, strlen(buf));
  cnt = read(sock, buf, strlen(buf));
  printf("Client with pid %d got message %s\n",
     getpid(), buf);
  printf("Socket address in server %d is %x, %s\n",
     getpid(), myname.sa_data, myname.sa_data);

  /*  . . . . .  */
  exit(0);
}






Connecting Across a Network
Sending data across a network is done in much the same way as sending data on a single machine. Establishing a connection is more difficult as a simple file name is not common among different machines. This is why networking domains other than AF_UNIX exist.
The following pages describe several of the routines available to establish a network connection. All these functions are for use with the AF_INET domain. The concept of network addressing is discussed first.






Network Addresses - The IP Address
The IP host address, or more commonly just IP address, is used to identify hosts connected to the Internet. IP stands for Internet Protocol and refers to the Internet Layer of the overall network architecture of the Internet. An IP address is a 32-bit quantity interpreted as 4 8-bit numbers or octets. Each IP address uniquely identifies the participating user network, the host on the network, and the class of the user network.
An IP address is usually written in a dotted-decimal notation of the form N1.N2.N3.N4, where each Ni is a decimal number between 0 and 255 decimal (00 through ff hexadecimal). Addresses are controlled and assigned by the Internet Network Information Center at SRI International. There are currently 5 defined classes of networks, called A, B, C, D and E; classes A, B and C are available to users. Each interface on a host system connected to the Internet must have its own IP address.

A brief synopsis of IP address classes A, B, and C is contained in the following table.


Class/Attribute   ABC   Address Format   N.H.H.HN1.N2.H.HN1.N2.N3.HFirst Octet0 < N < 127128 <= N1 < 192192<= N1 < 224Network IDNN1.N2N1.N2.N3Host IDH.H.HH.HHAvailabilityVery difficultAlmost exhaustedAmple supplyExample10.133.51.224128.247.116.3192.58.103.7

SubnetsAs the number of hosts on a given network grew large and more geographically separated, network management considerations and physical layer limitations such as maximum cable length gave impetus to research into possible changes in network installations without massive disruptions. Also, the use of heterogeneous physical layer networks (e.g., Ethernet and Token Ring) with the same IP network address was increasing.
The concept of subnet was introduced to help solve these problems. The benefits for Class A and Class B networks especially are worth the subnet planning and implementation effort.

The basic idea in subnetworking (a common word also used is subnetting) is to partition the host identifier portion of the IP address into two parts:


A subnet address within the network address itself; and
A host address on the subnet.
For example, a common Class B address format is N1.N2.S.H, where N1.N2 identifies the Class B network, the 8-bit S field identifies the subnet, and the 8-bit H field identifies the host on the subnet.





Network Host Names
Using IP addresses to access hosts on a network is fine for the IP software. Most people are more comfortable with names, and procedures for both proper name construction and translation of these names into IP addresses has been in existence for some time. The most commonly used is the Domain Name System (DNS), occasionally but inaccurately referred to as the Domain Name Service. Naming in DNS is done hierarchically, that is, in a tree-structure format, much like the UNIX file system naming. The top two levels are controlled by the Internet Network Information Center (NIC) at SRI International.
At the top of the domain are two-letter country designators and three-letter (usually) general category designators within the USA. Some examples are:


fr -- France
gov -- government
nz -- New Zealand
com -- commercial business
us -- USA
edu -- educational institution
uk -- United Kingdom
mil -- military
The next level usually identifies the institution. For example:

ibm -- IBM Corporation
utdallas -- UT-D
ti -- Texas Instruments (TI)
nasa -- NASA
The combination of these should be enough to specify the name, type and location of the organizational entity. For example:

ukuug.uk -- UNIX Users Group in the United Kingdom
utdallas.edu -- University of Texas at Dallas in the USA
ti.com -- Texas Instruments in the USA

Further hierarchies may be established within these organizations and are controlled locally. Some examples of host names are:

csservr2.utdallas.edu -- host csservr2 at UT-D
pac1.pac.sc.ti.com --   host pac1 within domain pac within domain sc at TI
sunk.ssc.gov -- host sunk at the Superconducting SuperCollider Laboratory

DNS and other software help in maintenance of these naming conventions and in the translation of host names to IP addresses and vice versa.





The /etc/hosts File
The correspondence between host names and IP addresses is maintained in a file called hosts in the (top-level) directory /etc. On most systems, any user can read this file. (A word of caution: on many systems, printing this file may be injurious to local paper supply!!)
Entries in this file look like the following:


127.0.0.1      localhost
192.217.44.208   snoopy beagle hound metlife
153.110.34.18   bugs wabbit wascal
153.110.34.19   elmer
153.110.34.20   sam

Note that more than one name may be associated with a given IP address. This file is used when converting from IP address to host name and vice versa.





Selecting a Service Port
The protocol specifications of the protocols used in the AF_INET domain require specification of a port. The port number is used to determine which process on the remote machine to talk to.
Certain network services have been assigned Well Known Ports. The port assignments to network services can be found in the file /etc/services. Selection of a Well Known Port involves searching this file and is done with the following functions:


#include <netdb.h>

struct servent *getservbyname(char *name, char *proto)

struct servent *getservbyport(int port, char *proto)


The two options for proto in each call are tcp for stream oriented communications, and udp for datagram oriented communications. port is the (known) port number when the service name is requested, while name is the character string containing the service name when the port number is requested.
The return value for each function is a pointer to a structure with the following form:



   struct    servent {
        char *s_name;      /* official name of service */
        char **s_aliases;  /* alias service name list */
        long s_port;       /* port service resides at */
        char *s_proto;     /* protocol to use */
     };


If a program does not need to communicate with a Well Known Port it is possible to choose an unused port for use by a program. Illustration of this technique is contained in the

Page : << Previous 3  Next >>