Thursday, April 3, 2008

TCP/IP Programming
in C


Client-Server Model


Definitions

  • Server - a process which is able to receive and respond to properly formatted requests from other processes.

  • Client - a process which is able to request information from a server on the same host or on a different host.


Requirements for Network Interfaces


Both Server and Client

  • Sending and receiving processes must be able to execute on any host

even on the same host

even on different hardware and OS

  • Guarantee data integrity

  • Handle disconnection in a well-coordinated fashion, including:

      • informing the other side of the connection

      • informing the user

      • closing all files


Client Only

  • Ability to address the specific server it requires

  • Built small – not too demanding on the client host


Server Only

  • Able to support multiple clients...

and multiple simultaneous requests...

from different hosts

  • Usually on more robust and responsive hardware

  • Main processing of the system is done here



IP Addressing and Protocol


  • Provides a method of addressing any

station on the network

  • Management and diagnostic services

  • Packet size limitation


Three types of addresses


  • Ethernet

  • IP

  • Port number

Purpose Format Used


Ethernet Address Low level Addressing 6 short integers Routing tables

(hardware address) within Ethernet “burned into”


IP Address Long distance and 4 short integers Inter- Intra-Net

“dotted quad” program addressing adrressing



Port Number Identify one process One long 32bit Values Name up to

within a host integer 1024


TCP/IP Client - Server Addressing

When the Client specifies:

1. IP Address >>> Tells which Host

2. Port Number >>> Tells which Server Process

within Host



After IP . . .
Next Level of Protocol
Can be either . .
.


TCP or UDP

connection oriented no connection datagram


reliable delivery (expects non-reliable delivery

acknowledgment)


point to point broadcast


non-guaranteed record guaranteed record boundaries

boundaries


socket parm=

SOCK_STREAM SOCK_DGRAM




Important Address & Protocol Functions


Provide information about:

  • Host names as configured by

      • Hosts files

      • DNS

      • NIS

  • Protocol names

  • Services

      • names

      • Port numbers

      • Protocols


gethostbyname - retrieves all information about a host, given its name or IP. This works whether the name is stored in /etc/hosts, NIS or DNS.


/etc/hosts should contain a single line for each host with the following information:

<internet address><official host name><aliases>

For example:

192.45.36.5 hpdxsg testhost

If your system is in a domain naming environment, an official host name consists of the full domain extended host name.

For example:

        1. hpdxsg.xsg.hp.com hpdxsg testhost


getprotobyname - gets a protocol number, given its name. Used for finding the protocol number for TCP or other protocols used in calling other routines.


#The form for each entry is:

<official protocol name><official protocol number><aliases>

#Internet (IP) protocols

#

ip 0 IP #internet protocol, pseudo protocol number

icmp 1 ICMP #internet control message protocol

ggp 3 GGP #gateway-gateway protocol



getservbyname - receives 2 arguments:

(1) char * - service name or alias

(2) char * - protocol name (tcp or udp)


#The form for each entry is:

<official service name><port number/protocol name><aliases>

echo 7/tcp #Echo

echo 7/udp #




TCP/IP Socket Functions

Server/Client/ Order

Both of Call Purpose


accept S 4 Wait for client connection


bind B 2 Associate socket with IP address


connect C 3 Requests connection to a server


listen S 3 Allocates queue for clients


recv B 5 Receives data block by length from a socket


send B 5 Sends a data block to a socket with opt.

IP address


shutdown B 6 Shutdown socket as directed


socket B 1 Allocate memory areas with port/address


read B 5 Inputs a text line from a socket


write B 5 Outputs text to a socket



TCP/IP Normal Sequence

Client Server

1 socket socket

2 bind bind

3 connect listen

4 accept



TCP/IP Support Functions

Purpose Param Returns

gethostbyname Retrieve a host IP address host name 1. name

2. aliases

3. addrtype

4. addrien

5. addrlist

6. 1st addr


getprotobyname Retrieve the internal protocol name 1. name

protocol number 2. aliases

3. number


getservbyname Check for existence of serv name & protocol 1. name

of given services 2. aliases

3. port

4. protocol


getpeemame Find IP address of remote socket address remote IP addr

communication host


getsockname Find socket address of socket address local IP addr

open socket



Steps in Making a TCP Connection

1. Query system information

gethostbyname-

getprotobyname-

getservbyname


2. Open a “socket” to allocate data structure


3. “Bind” the socket to attach it to the local address


4. (For servers) Allocate a “listen” queue to wait for a connect

request to pass its server address.


5. (For servers) Perform “accept” to allow an incoming

request to pass its socket address.


All data transfers use this passed socket. The server “accept”

socket is still available to service other incoming connections.


6. (For clients) Perform “connect” to request connection to a

given server IP address and port number.

No comments: