You are on page 1of 11

Sockets

Client-server paradigm

Client-server paradigm
Typical network app has two pieces: client and server Client: initiates contact with server (speaks first) typically requests service from server, for Web, client is implemented in browser; for e-mail, in mail reader Server: provides requested service to client, via replies e.g., Web server sends requested Web page, mail server delivers e-mail
application transport network data link physical

request

reply
application transport network data link physical

Client server motivation

Fundamental motivation
TCP/IP does not automatically create running programs when messages arrive So a program must be waiting to accept communications when requests arrive This is the server while the other party is the client

Client-server applications
Have one of two types of interaction: connectionless style (UDP) or connection-oriented mode (TCP)

Socket types

Socket identification:
IP address of client and server hosts port number of client and server applications

Socket types:
reliable, byte stream-oriented (TCP)
Unreliable, connection-less datagram (UDP) Raw Socket (IP)

Server Process

UNIX
version

Server Process

socket() bind() listen() accept()


get a blocked client TCP

socket()
UDP

bind()

Client Process
1 2

recvfrom()
get a blocked client

Client Process

socket() connect() write()


process request

read()
procees request

socket() bind() sendto()

write()

read()

sendto()

recvfrom()
5

Server Process

Winsock
or Unix

Server Process

socket() bind() listen() accept()


get a blocked client

version
TCP

socket()
UDP

bind()

Client Process
1 2

recvfrom()
get a blocked client

Client Process

socket() connect() send()


process request

recv()
process request

socket() bind() sendto()

send()

recv()

sendto()

recvfrom()
6

IP (Raw) Socket

To use RAW sockets in Unix it is mandatory that one have root authority. To create a RAW socket write: s=socket(AF_INET,SOCK_RAW,[protocol])
Then you can send or receive over it. Raw sockets are used to generate / receive packets of a type that the kernel doesn't explicitly support.

IP Raw Socket example

A familiar example is PING. Ping works by sending out an ICMP echo packet.
It formats an ICMP echo packet and sends it out over a SOCK_RAW, waiting for a response.

Names and addresses

struct hostent *gethostbyname(char *hostname);


Converts textual name to a hostent structure This structure contains all IPv4/IPv6 addresses of a host

/* Returns in Network Order */ unsigned long inet_addr(char *ipaddrstring);


Converts dotted decimal address to a 32 bit address Similar functionality provided by inet_pton() Deprecated. Better to use inet_aton()

Byte ordering routines

How to interpret bits on the wire


Different computers store bytes in different order in memory Network byte order (nbo) is big endian

Example: internal representation of 224

10

Kindly visit us at linuxsocketprogrammingshare.blogspot.com And at our facebook group https://www.facebook.com/groups/codingtheworld/

Thank you

11

You might also like