Professional Documents
Culture Documents
Computer networking/socket/naj/1
What is network program
• Communicating among processes
• The processes may reside …
– in a single computer or
– in computers connected to a network or
– Computers across a network
• Client server computing
Request
Client Server
Response
Computer networking/socket/naj/2
Examples Client-Server
• Web client Web server
(iexplorer, netscape
Navigator, mozzila, . . . ) (IIS, Apache)
• FTP Client FTP server
• Telnet Client Telnet server
• ...
• These are all precompiled programs and
may be used to communicate across a
network.
Computer networking/socket/naj/3
What is a Socket?
• Is it like this?
App
App
3 2
1
socket Dest. 3 2
1
socket Dest
Computer networking/socket/naj/7
Stream Oriented Comm.
Server Client
• Create a Socket
Socket
• Bind it to a local port
bind
• Listen for a incoming
listen connection request
Big-Endian
machine Little-Endian
12.40.119.128
128.119.40.12 machine
Computer networking/socket/naj/12
IPV4 Socket Add Structure
<netinet/in.h>
struct sockaddr_in{
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
}; //datatypes are as of posix.1g
¾sin_len: length of structure?
included in 4.3 BSD-Reno
¾Sa_family_t: address family of
socket address structure
<sys/socket.h>
Computer networking/socket/naj/13
IPV4 Socket Add Structure
¾sin_family: socket address family
¾AF_INET IPv4 family
¾AF_INET6 IPv6
¾AF_LOCAL UNIX domain
¾AF_ROUTE routing socket
¾AF_KEY key socket
¾in_port_t: 16 bit unsigned integer
¾sin_port: port no of TCP or UDP
¾This number is used to identify a particular
server program
Computer networking/socket/naj/14
Ports
• Each host has 65,536 Port 0
ports
Port 1
• Some ports are
reserved for specific
apps called as IANA Port 65535
Computer networking/socket/naj/18
System Calls(3)
AF_INET AF_LOCAL AF_LOCAL AF_ROUTE AF_KEY
SOCK_STREAM TCP TCP YES
SOCK_DGRAM UDP UDP YES
SOCK_RAW IPv4 IPv6 YES YES
• YES are valid but do not have acronyms
• There are also various other values for family and
type arguments
• On success the socket function returns a non-
negative integer, called socket descriptor
• This call is used by both server and client
• sockfd=socket(AF_INET, SOCK_STREAM,0);
Computer networking/socket/naj/19
System Calls(4)
#include<sys/socket.h>
int bind(int sockfd, const struct sockaddr *myaddr,
socklen_t addrlen);
• ‘bind’ assigns a local protocol address to a
socket
• Local protocol address consists of IP
address along with a port number
• Second argument is a pointer(?) to address
structure
• Third argument is the length(size) of
address structure (32-bit integer)
Computer networking/socket/naj/20
System Calls(5)
• With TCP, calling bind lets us specify a port
number, an IP address, both or neither.
Process specifies Result
IP address Port
Computer networking/socket/naj/35