[go: up one dir, main page]

0% found this document useful (0 votes)
12 views36 pages

Intro To Sock Prog

Uploaded by

Mahammad azad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views36 pages

Intro To Sock Prog

Uploaded by

Mahammad azad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Socket programming

An Introduction

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 1 / 36


Today’s class – overview

Network programming fundamentals


TCP/IP protocol stack
Network programming model
Berkeley socket – library

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 2 / 36


Fundamentals

A network exists anytime there is communication between two or


more parties.
The implementations are defined by such aspects as medium and
protocol.
The network medium is the substance used to transmit the information;
The protocol is the common system that defines how the information is
transmitted.
Types of networks, the methods for connecting networks, how network
data is moved from network to network, and the protocols used on
today’s popular networks.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 3 / 36


Types of Networks

Circuit-switched networks are networks that use a dedicated link


between nodes, or points; A packet-switched network takes the
information communicated on the network and breaks it into a series
of packets, or pieces.
One advantage of a circuit-switched network is the guaranteed capacity.
A big disadvantage of circuit-switched networks is cost.
On a packet-switched network, many nodes can exist in the network,
and all nodes can use the same network resources as all of the others,
sharing in the cost.
The disadvantage of packet-switched networks is the inability to
guarantee capacity. As more and more node sharing the resources try
to communicate, the portion of the resource available to each node
decreases.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 4 / 36


Internetworking I

The most common network technology is the concept of a local area


network or LAN. The basis for the Internet is internetworking, which
is about connecting one network to another.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 5 / 36


Internetworking II

A LAN typically takes the form of two or more computers joined


together via a hub or switch.
Connecting two LANs together forms yet another network, this one
consisting of two smaller networks connected together so that
information can be shared not only between nodes on a particular
LAN, but also between nodes on separate LANs via the larger
network.
By using routers, network addressing schemes, transmission
technologies and network protocols, it becomes feasible to connect an
unlimited number of LANs to each other and allows nodes on these
LANs to communicate with nodes on remote networks as if they were
on the same local network.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 6 / 36


Addressing

How can we locate a nearby computer or one on the other side of the
world?
Ethernet addresses are physical address tied directly to the hardware
interface connected via the Ethernet cable running to each node.
An Ethernet address is an integer with a size of 48 bits.
Ethernet addresses are tied to the Ethernet device itself, not the node
hosting the interface.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 7 / 36


MAC Addresses

First three octets(hex) tells the manufacturer of the network


interface. SMC, Dux
ip addr
/sbin/ifconfig eth0
/sbin/ifconfig -a
00:12:3F:E9:93:FB
Hardware address is also called Media Access Control (MAC) address

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 8 / 36


Routing

The Internet is a network built by physically connecting other


networks.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 9 / 36


Gateways

A special device called a gateway is used to connect networks


together.
Gateways have two or more physical network interfaces; they replay
packets destinated for other networks, and they receive packets
destinated for nodes on one of their own networks.
Gateway can also be called routers.
The gateway takes on the task of correctly routing packets with
foreign destinations to either the remote network itself or another
gateway.
Network gateways route packets based on destination networks, not
the destination nodes.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 10 / 36


Addresses

IP addresses are the Internet protocol addresses, which means IP


addresses are used by the protocol, not by the physical nodes. IP
addresses are virtual.
There is no required correlation between a particular IP address and
its physical interface.
The private IP addresses can be “hidden” behind a single public
address using a technique called Network Address Translation (NAT),
where an entire range of private addresses is translated into a single
public address by the private network’s gateway.
127.0.0.1 is known as loopback address. The loopback address is used
to specify the local machine (or localhost). When use the loopback
address, the sender is the receiver and vice versa. 127.0.0.0 network is
considered a reserved network for loopback use.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 11 / 36


Ports

Ports are virtual destination “points” and allow a node to conduct


multiple network communication simultaneously.
On Linux systems, the number of ports is limited to 65,535, and many
of lower port numbers are reserved.
A list of reserved ports and the names of the services that use them
can be found in the /etc/services file.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 12 / 36


Network Byte Order

Each hardware manufacture can have its own hardware architecture,


data representation on the platform could be either Little Endian or
Big Endian.
The standard network byte order is Big Endian.
The Linux is based on Intel, and the data representation is in the Little
Endian.
The Internet protocols solve this byte-order problem by defining a
standard way of representing integers called network byte order that
must be used by all nodes on the network.
Each host platform makes the conversion from its local byte
representation to the standard network byte order before sending a
packet.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 13 / 36


Protocol layering

TCP/IP is a protocol suite that handle every issue that might be


encountered on a network. Such issues include security, packet loss,
hardware failure, network congestion, and data corruption.
It is hard and unpractical to develop a single ”super” protocol to
address all these issues. It is more logical to develop a system in which
complementary protocols, each handling a specific task, work together
in a standarized fashion.
Protocol stack: Each layer in a stack takes responsibility for a
particular aspect of sending and receiving information on a network,
which each layer in the stack working in concert with the other layers.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 14 / 36


Protocol layering

TCP/IP is a protocol suite that handle every issue that might be


encountered on a network. Such issues include security, packet loss,
hardware failure, network congestion, and data corruption.
Protocol layers

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 15 / 36


The Client-Server Model

A TCP connection is a connection of equals and applications are


present at both endpoints of a TCP connection. The model of
interaction on a TCP connection is known as the client-server model.
A ‘Server’ refers to an application that offers a service that can be
utilised by any other application over the network.
A ‘Client’ is the application sending the request to the server and
waiting for the response.
The differences between a server and a client are:
The client, unlike the server, typically stops sending requests once the
server has responded.
Ports are handled differently; the server typically uses a reserved port,
clients do not.
A small exercise? When you see two endpoints: 192.168.2.1:80 and
10.0.0.4:11908, can you find out which one is a server endpoint and
which one is a client endpoint?

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 16 / 36


socket API

Part of Unix operating system


Different for different Unix operating system
A function library for network communication

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 17 / 36


Berkely Sockets System Calls

Socket and Socket descriptor


Socket address structures
Elementary socket functions
Helper function that make it easier to deal with integer byte
conversion, structure manipulation, and socket options.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 18 / 36


Network Programming
Provide more details of how to use the Berkely Sockets API

How to use the TCP/IP protocol(not to develop, or modify).


Get to know what functions available
What feastures they have, options, etc.
Understand the relation between the API and OS.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 19 / 36


Berkeley Unix or BSD Unix
Basic principles of how to use the sockets API

TCP/IP software has been ported to the Unix operating system.


An interface is created between the TCP/IP-based network and the
networked application that used it.
The interface should use existing Unix system calls and algorithms,
and add new functions only when absolutely necessary.
While other network interfaces exist, the Berkeley socket interface is
considered the de facto standard and is widely supported and
accepted.
The Berkeley socket interface is the interface used by Linux.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 20 / 36


File system communication
File descriptor

In a typical application, I/O functions are performed on files. These


functions work by using a file descriptor.
A file descriptor is a small integer returned by a function that an
application can use for subsequent operations.
The system maintains a file descriptor table for each process. When
the process opens a file, a pointer to the internal data structures for
that file is placed in the file descriptor table and the index to the
pointer is returned to the calling function.
The application only needs to work with the descriptor; the operating
system will handle the underlying operations by following the pointer in
the descriptor table and getting the information it needs.
Other functions use this file descriptor to perform reads and writes,
change file options, and close the file when finished.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 21 / 36


The Socket
An active socket is identified by an integer called socket descriptor.

A socket descriptor is created that points to a data structure holding


information about that socket. The socket data structure will include
information such as the socket type, the local address and port being
used by the socket, and the remote address and port that will receive
communications from the socket.
To communicate across a network, a connection(socket) to the
network must be opened.
Once it’s opened, data is read from or written to the socket. When
communication is finished, the connection to the network is closed
and the resources used by the socket are realeased.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 22 / 36


Using Sockets
Function call sequence

Figure: The sequence of steps to establish and undertake communication via TCP
Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 23 / 36
Using Sockets
Socket Constants

The socket interface uses a set of predefined symbolic constants and


data structure declarations.
protocol type constants and address family constants are found in the
/usr/include/ directory. The include statements are
#i n c l u d e <s y s / t y p e s . h> and
#i n c l u d e <s y s / s o c k e t . h>

An application can use serveral different types of sockets.


Address family

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 24 / 36


Using Sockets
Address structure

The most important structure is the sockaddr in structure. The


sockaddr in structure contains both an IP address and protocol port
number.
Sockaddr s t r u c t u r e
struct sockaddr in {
short sin family ;
u short sin port ;
u long sin addr ;
char s i n z e r o [ 8 ] ;
};

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 25 / 36


Using Sockets
Address structure – servent

struct servent {
c h a r * s name ;
c h a r ** s a l i a s e s ;
int s port ;
char * s proto ;
};

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 26 / 36


Byte order functions

TCP/IP protocol specifies the way to represent the binary integers found
in the protocol headers. Network byte order defines the binary integers
must be specified with the most significant byte first.
different operating systems and hardward platforms have choosen
different ways to represent numbers.
TCP/IP protocol is Big Endian.
These functions automatically convert short and long integers from
network type order to the local host’s native byte order.
These functions operate on 16-bit and 32-bit integers: htons(),
ntohs, ntohl.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 27 / 36


Using Socket Functions
Core functions

Depending on your needs, your application might use only a few of the
following functions, all of them, or even some that are not listed here.
Socket() function
i n t s o c k e t ( i n t domain , i n t t y p e , i n t p r o t o c o l )

socket() creates a network endpoint, and returns a socket descriptor.


domain - Protocol or address family (AF INET for TCP/IP; AF UNIX
for internal).
type - Type of service (SOCK STREAM for TCP; SOCK DGRAM for
UDP).
protocol - The protocol number to use, typically 0 for default
socket() returns a -1 if there is an error. Otherwise, the socket
desriptor.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 28 / 36


Core functions
Bind() function

The bind() function is used to bind the socket to a given address once
the socket is created.
i n t b i n d ( i n t s o c k f d , s t r u c t s o c k a d d r * my addr , s o c k

The second parameter is a pointer to the sockaddr structure. The


structure is initialised as follows before the bind() is called.
/ * r e t r i e v e t h e p o r t number f o r l i s t i n g * /
simplePort = a t o i ( argv [ 1 ] ) ;
/ * s e t up t h e a d d r e s s s t r c u t u r e * /
b z e r o (& s i m p l e S e r v e r , s i z e o f ( s i m p l e S e r v e r ) ) ;
s i m p l e S e r v e r . s i n f a m i l y = AF INET ;
s i m p l e S e r v e r . s i n a d d r . s a d d r = h t o n l (INADDR ANY ) ;
simpleServer . s i n p o r t = htons ( simplePort ) ;
/ * b i n d t o t h e a d d r e s s and p o r t w i t h o u r s o c k e t * /
returnStatus = bind ( simpleSocket , sin addr , s i z e o f ( s i n a d d r ) ) ;

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 29 / 36


Core funtion
connect() function

This function is used by a TCP client to establish a connection with a


TCP server.
i n t connect ( i n t s , s t r u c t sockaddr * servaddr ,
socklen t l );

/* s e t u p t h e s e r v e r i n f o */
x f e r S e r v e r . s i n f a m i l y = AF INet ;
x f e r S e r v e r . s i n a d d r . s addr = i n e t a d d r ( argv [ 1 ] ) ;
x f e r S e r v e r . s i n p o r t = h t o n s (SERVERPORT ) ;
/* c o n n e c t t o t h e s e r v e r */
r e t u r n S t a t u s = c o n n e c t ( sockd ,
( s t r u c t s o c k a d d r * )& x f e r S e r v e r ,
sizeof ( xferServer ));

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 30 / 36


Core functions
Listen() function

This function tells our socket that we are ready to accept request of
connections, and it also specifies a maximum number of connections that
can be queued before connections are refused.
i n t l i s t e n ( i n t s , i n t backlog )

r e t u r n S t a t u s= l i s t e n ( s i m p l e S o c k e t , 5 ) ;
i f ( r e t u r n S t a t u s == −1) {
f p r i n t f ( s t d e r r , ” c a n n o t l i s t e n on s o c k e t ! \ n ” ) ;
close ( simpleSocket );
exit (1);
}

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 31 / 36


Core functions
Accept

It is used in the server.


i n t a c c e p t ( i n t s , s t r u c t s o c k a d d r * addr ,
socklen t * addrlen );

The accept is a blocking function.


The 2nd and 3rd parameters passed to accept() are locations for storing
information about the client, not about the server. The value for the client
name is set to 0 initially, as is the length of the name. Both of the
structures should be populated with correct info.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 32 / 36


Identification Functions
gethostbyaddr() and gethostbyname(); both are now obsolete

gethostbyaddr() perfroms a lookup that returns a host name when given


an address; gethostbyname() performs the reverse.
s t r u c t h o s t e n t * g e t h o s t b y a d d r ( c o n s t c h a r * addr , i n t l e n , i n t t y p e ) ;
s t r u c t h o s t e n t * g e t h o s t b y a d d r ( c o n s t c h a r * addr , i n t l e n , i n t t y p e ) ;

struct hostent {
c h a r * h name ; / * o f f i c a l name o f h o s t * /
c h a r ** h a l i a s e s ;
int h addrtype ;
int h length ;
c h a r ** h a d d r l i s t ;
};

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 33 / 36


Identification Functions
gethostname() and sethostname()

gethostname() returns the name of the current local host where the
function call originates, while sethostname() can be used to set the name
of the host.
i n t g e t h o s t n a m e ( c h a r * name , i n t namelen ) ;
i n t s e t h o s t n a m e ( c o n s t c h a r * name , i n t namelen ) ;

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 34 / 36


Identification functions
getservbyname() and getservbyport(); both are now obsolete

Getservbyname() and getservbyport() return a pointer to the servent


structure.
s t r u c t s e r v e n t * g e t s e r v b y n a m e ( c o n s t c h a r * name , c o n s t c h a r * p r o t o ) ;
s t r u c t s e r v e n t * g e t s e r v b y p o r t ( i n t port , const char * proto ) ;

It is important to remember that the port number must be in network byte


order.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 35 / 36


Identification functions
getsockopt() and setsocketopt()

These two functions manipulate a socket’s options, which can be used to


configure behaviour, such as keeping connections alive, setting timeout
values, or setting the buffer size.
i n t g e t s o c k o p t ( i n t s , i n t l e v e l , i n t optname , v o i d * o p t v a l ,
s o c k l e n t * o p t l e n ) ; i n t s e t s o c k o p t ( i n t s , i n t l e v e l , i n t optname ,
const void *optval , socklen t optlen ) ;

SO LINGER which is used with the linger structure, determining whether


a socket stay open or not. Other opions include SO KEEPLIVE,
SO RCVTIMEO, SO REUSEADDR, etc.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 36 / 36

You might also like