0% found this document useful (0 votes)
99 views18 pages

Introduction To Socket Programming: Sandip Chakraborty

This document provides an introduction to socket programming. It discusses how sockets provide an interface for applications to communicate over network protocols like TCP and UDP. It describes the socket API and functions for creating, binding, listening for and accepting connections. It explains how sockets handle data transfer and provides examples of code snippets and resources for socket programming tutorials.

Uploaded by

radib
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)
99 views18 pages

Introduction To Socket Programming: Sandip Chakraborty

This document provides an introduction to socket programming. It discusses how sockets provide an interface for applications to communicate over network protocols like TCP and UDP. It describes the socket API and functions for creating, binding, listening for and accepting connections. It explains how sockets handle data transfer and provides examples of code snippets and resources for socket programming tutorials.

Uploaded by

radib
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/ 18

Introduction to Socket Programming

Sandip Chakraborty

Department of Computer Science and Engineering,

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

August 8, 2018

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 0 / 14


Connecting Network with Operating System

Check the net module (download Kernel source and check


/usr/src/linux/net)!

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 1 / 14


Application Multiplexing in TCP/IP

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 2 / 14


Application Multiplexing in TCP/IP

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 3 / 14


Application Multiplexing in TCP/IP

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 4 / 14


What are Sockets?

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 5 / 14


Socket Programming Framework/API

A set of system calls to get the service from TCP/IP protocol stack (net
module in the OS kernel).

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 6 / 14


Socket Types

The Internet is a trade-off between performance and reliability - Can


you say why?

Some application requires fine grained performance (example -


multimedia applications), while others require reliability (example -
file transfer)

Transport layer supports two services - Reliable (TCP), and Unreliable


(UDP)

Two types of sockets:


1 Stream Socket (SOCK STREAM): Reliable, connection oriented
(TCP based)

2 Datagram Socket (SOCK DGRAM): Unreliable, connection less


(UDP based)

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 7 / 14


Socket API

int s = socket(domain, type, protocol); - Create a socket


domain: Communication domain, typically used AF INET (IPv4
Protocol)
type: Type of the socket - SOCK STREAM or SOCK DGRAM
protocol: Specifies protocols - usually set to 0 – Explore!

int status = bind(sockid, &addrport, size); - Reserves a


port for the socket.
sockid: Socket identifier
addrport: struct sockaddr in - the (IP) address and port of the
machine (address usually set to INADDR ANY chooses a local address)
size: Size of the sockaddr structure

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 8 / 14


struct sockaddr in
sin family : Address family, AF INET for IPv4 Protocol

sin addr.s addr: Source address, INADDR ANY to choose the local
address

sin port: The port number

We need to use htons() function to convert the port number from


host byte order to network byte order.

struct sockaddr in serveraddr;


int port = 3028;
serveraddr.sin family = AF INET;
serveraddr.sin addr.s addr = INADDR ANY;
serveraddr.sin port = htons(port);

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 9 / 14


Host Byte Order to Network Byte Order - Why?

Little Endian and Big Endian System

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 10 / 14


Host Byte Order to Network Byte Order - Why?

Little Endian and Big Endian System

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 10 / 14


Host Byte Order to Network Byte Order - Why?

Little Endian and Big Endian System

Assume a communication from a Little Endian to a Big Endian


System or vice-versa!

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 10 / 14


Host Byte Order to Network Byte Order - Why?

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 11 / 14


Listen and Accept a Socket Connection

struct sockaddr in cli addr;


listen(sockfd,5);
clilen = sizeof(cli addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli addr,
&clilen);

Active Open and Passive Open


The server needs to announce its address, remains in the open state
and waits for any incoming connections - Passive Open

The client only opens a connection when there is a need for data
transfer - Active Open

Connection is initiated by the client

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 12 / 14


Data Transfer through Sockets

1 For SOCK STREAM:


read(newsockfd,buffer,255);
write(newsockfd,‘‘I got your message’’,18);

2 For SOCK DGRAM:


recvfrom(sock,buf,1024,0,(struct sockaddr
*)&from,&fromlen);
sendto(sock,‘‘Got your message’’,17,0,(struct sockaddr
*)&from,fromlen);

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 13 / 14


Putting it All Together

Check the details and sample codes at


http://www.linuxhowtos.org/C_C++/socket.htm.

Socket Programming Tutorials


Beej’s Guide to Network Programming -
http://beej.us/guide/bgnet/

http://cs.baylor.edu/~donahoo/practical/CSockets/
textcode.html

http:
//www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 14 / 14


Thank You

Sandip Chakraborty (IIT Kharagpur) NPTEL August 8, 2018 14 / 14

You might also like