You are on page 1of 17

Introduction to Sockets

Jan 2005

Why do we need sockets?


Provides an abstraction for
interprocess communication

Definition
The services provided (often by the operating
system) that provide the interface between
application and protocol software.

Application
Network API
Protocol A Protocol B

Protocol C

Functions
Define an end- point for
communication
Initiate and accept a connection
Send and receive data
Terminate a connection gracefully
Examples
File transfer apps (FTP), Web browsers
(HTTP), Email (SMTP/ POP3), etc

Types of Sockets
Two different types of sockets :
stream vs. datagram

Stream socket :( a. k. a. connection- oriented socket)


It provides reliable, connected networking service
Error free; no out- of- order packets (uses TCP)
applications: telnet/ ssh, http,

Datagram socket :( a. k. a. connectionless socket)


It provides unreliable, best- effort networking service
Packets may be lost; may arrive out of order (uses UDP)
applications: streaming audio/ video (realplayer),

Addressing

Client

Server

Addresses, Ports and Sockets


Like apartments and mailboxes

You are the application


Your apartment building address is the address
Your mailbox is the port
The post-office is the network
The socket is the key that gives you access to the
right mailbox

Client high level view


Create a socket
Setup the server address

Connect to the server


Read/write data
Shutdown connection

int connect_ socket( char *hostname, int port) {


int sock;
Ipv4 socket address structure
struct sockaddr_in sin;
struct
socketaddr_in{
Hostent
structure
struct hostent *host;
uint8_t
sin_len; /*length of the structure (16)*/
struct hostent{
sock = socket( AF_ INET, SOCK_ STREAM,
0);
sa_falimily_t
sin_family
/* AF_INT*/
char * h_name
/*official
name of host*/
if (sock == -1)
in_port_t
sin_port /* pointer ot
/*array
16 bitof\
TCP or UDP port number*/
char
**
h_aliases;
return sock;
struct in_addr sin_addr pointers to
/* 32
bitname*/
Ipv4 address */
alias
host = gethostbyname( hostname);
char
sin_zero(8)/*
unused*/
int
h_addrtype
/* host address type*/
if (host == NULL) {
} int
h_length
/* type,
lengthinof
address */
close( sock);
Socket(int
family , int
t protocol);
char ** h_addr_list
/*prt to
of for
ptrserror
with \
return -1;
return nonnegative value
forarray
OK, -1
IPv4
or
IPv6
address*/
}
}
memset (& sin, 0, sizeof( sin));
sin. sin_ family = AF_ INET;
struct
hostent
*gethostbyname(
const char *hostname);
unit16_t
htons(unit16_t
host16bitvaule)
sin. sin_ port = htons( port);
/*Return
nonnull
pointer
if
OK,
NULL
error
*/ to
/*Change the port number from hoston
byte
order
sin. sin_ addr. s_ addr = *( unsigned long *) host-> h_ addr_ list[ 0];
network byte order */
connect(int
if (connect( sock, (struct sockaddr *) &sin,
sizeof( sin))socketfd,
!= 0) { const struct sockaddr * servaddr,
socket_t addrlen)
close (sock);
/*Perform the TCP three way handshaking*/
return -1;
}
return sock;
}

Make the socket

Resolve the host

Setup up the struct


Connect

Server high level view


Create a socket
Bind the socket
Listen for connections
Accept new client connections
Read/write to client connections
Shutdown connection

Listening on a port (TCP)


int make_ listen_ socket( int port) {
struct sockaddr_ in sin;
int sock;
sock = socket( AF_ INET, SOCK_ STREAM, 0);
if (sock < 0)
Make the socket
return -1;
memset(& sin, 0, sizeof( sin));
sin. sin_ family = AF_ INET;
Setup up the struct
sin. sin_ addr. s_ addr = htonl( INADDR_ ANY);
sin. sin_ port = htons( port);
if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0)
return -1;
Bind
return sock;
bind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen);
}
/* return 0 if OK, -1 on error
assigns a local protocol adress to a socket*/

accepting a client connection (TCP)

int get_ client_ socket( int listen_ socket) {


struct sockaddr_ in sin;
int sock;
int sin_ len;
memset(& sin, 0, sizeof( sin));
Setup up the struct
sin_ len = sizeof( sin);
sock = accept( listen_ socket, (struct sockaddr *) &sin, &sin_
len);
Accept the client connection
return sock;
accept(int sockefd, struct sockaddr * claddr, socklen_t * addrlen)
/* return nonnegative descriptor if OK, -1 on error
}
return the next completed connection from the front of the
completed connection queue.
if the queue is empty,
the process is put to sleep(assuming blocking socket)*/

Sending / Receiving Data


With a connection (SOCK_STREAM):
int count = send(sock, &buf, len, flags);

count: # bytes transmitted (-1 if error)


buf: char[], buffer to be transmitted
len: integer, length of buffer (in bytes) to transmit
flags: integer, special options, usually just 0

int count = recv(sock, &buf, len, flags);

count: # bytes received (-1 if error)


buf: void[], stores received bytes
len: # bytes received
flags: integer, special options, usually just 0

Calls are blocking [returns only after data is sent (to


socket buf) / received]

socket()

TCP Server

bind() Well-known port

TCP Client

listen()

Socket()

accept()
Connection establishment blocks until connection from client
connect()
Data(request)
write()
read()
process request
read()
close()

Data(reply)
End-of-fle n
otifcation

write()

read()
close()

Dealing with blocking calls


Many functions block
accept(), connect(),
All recv()
For simple programs this is fine
What about complex connection routines
Multiple connections
Simultaneous sends and receives
Simultaneously doing non-networking processing

Dealing with blocking (cont..)


Options
Create multi-process or multi-threaded code
Turn off blocking feature (fcntl() system call)
Use the select() function

What does select() do?


Can be permanent blocking, time-limited blocking or nonblocking
Input: a set of file descriptors
Output: info on the file-descriptors status
Therefore, can identify sockets that are ready for use:
calls involving that socket will return immediately

select function call


int status = select()
Status: # of ready objects, -1 if error
nfds: 1 +largest file descriptor to check
readfds: list of descriptors to check if read-ready
writefds: list of descriptors to check if write-ready
exceptfds: list of descriptors to check if an
exception is registered
Timeout: time after which select returns

You might also like