Upload
clifford-rice
View
212
Download
0
Embed Size (px)
Citation preview
Hands On NetworkingSocket Programming
Ram P Rustagi, ISE [email protected]
Abhishek Gupta, ISE DeptLaxmi Kuber, MCA Dept
June 28-30, 2012
Socket programming
• Sockets– Protocol independent method of creating a connection
a connection– Can be either
• Connection based or connection less– Connection established before communication– Destination information provided with each
packet– Packet based or stream based
• Message boundaries vs one stream– Reliable vs unreliable
• Message recovered in case– Loss, duplication, corruption, out of order delivery
2
Socket programming
• Ports– Three categories– Well known ports
• Assigned and managed by IANA• Ranges from 1 1023
– Registered ports• Can be registered by IANA• Ranges from 1024 to 49151
– Dynamic ports• Free to be used by application at will
3
Socket characteristics
• Sockets are charaterized by– Domain, type and transport protocol
• Common domains are– AF-UNIX– AF_INET– AF_INET6
• Types are– Datagram vs streams
• Transport protocols– TCP– UDP
4
5
Socket Programming
• Server side – Concurrent connections handling
• Multi processing• Single threaded using select(), poll()• Non-blocking communication
• WE-1– Simple client server programming (using C)
• WE-2– Multi-processing server side programming
• WE-3– Server side single threaded concurrent connections
• Using select()• Using poll()
• WE-4– Using non-blocking I/O
5
6
Socket Programming
• Socket programming concepts– Client side and server side differences– Well known ports, reserved ports and ephemeral ports– UDP vs TCP programming
• Client side programming– Socket()– Connect() // only for TCP– Read(), write()– Close()
• Server side programming– Socket()– Bind()– Listen()– Accept()– Read(), write()– Close() 6
Socket API’s• socket: creates a socket of a given domain, type,
protocol (buy a phone)
• bind: assigns a name to the socket (get a telephone number)
• listen: specifies the number of pending connections that can be queued for a server socket. (call waiting allowance)
• accept: server accepts a connection request from a client (answer phone)
• connect: client requests a connection request to a server (call)
• send, sendto: write to connection (speak)• recv, recvfrom: read from connection (listen)• shutdown: end the call
socket
Bind
Listen
accept
Send/recv
Close
shutdown
close
socket
connect
Send/recv
shutdown
close
server client
Non-Blocking I/O
• Programs that use non-blocking I/O– every function has to return immediately– Can not wait or get blocked
• To make a non-blocking program– make the socket non-blocking– ioctl(sockfd, FIONBIO, (char *)&on);
programs
• WE-1– Simple client/server programs– Sample given for both in C and Java
• WE-2– multi-processing server side– C –based, server creates a child for each connection– Java based, server creates a thread for each connection
• WE-3– Available only for C based– Use of select
10