Example: stock market

Introduction to Socket Programming - Indian Institute of ...

Introduction to Socket ProgrammingSandip ChakrabortyDepartment of Computer Science and Engineering, Indian Institute OF TECHNOLOGY KHARAGPURM arch 3, 2015 Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20150 / 32 Basics of TCP/IP Protocols - RevisitedSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20151 / 32 Basics of TCP/IP Protocols - RevisitedSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20152 / 32 Basics of TCP/IP Protocols - RevisitedSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20153 / 32 Basics of TCP/IP Protocols - RevisitedSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20154 / 32 Basics of TCP/IP Protocols - RevisitedSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20155 / 32 Connecting Network with Operating SystemCheck thenetmodule (download Kernel source and check/usr/src/linux/net)!

Introduction to Socket Programming Sandip Chakraborty Department of Computer Science and Engineering, INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR March 3, 2015 Sandip Chakraborty (IIT Kharagpur) CS 39006 March 3, 2015 0 / 32. Basics of TCP/IP Protocols - Revisited

Tags:

  Introduction, Programming, Sockets, Introduction to socket programming

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Introduction to Socket Programming - Indian Institute of ...

1 Introduction to Socket ProgrammingSandip ChakrabortyDepartment of Computer Science and Engineering, Indian Institute OF TECHNOLOGY KHARAGPURM arch 3, 2015 Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20150 / 32 Basics of TCP/IP Protocols - RevisitedSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20151 / 32 Basics of TCP/IP Protocols - RevisitedSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20152 / 32 Basics of TCP/IP Protocols - RevisitedSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20153 / 32 Basics of TCP/IP Protocols - RevisitedSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20154 / 32 Basics of TCP/IP Protocols - RevisitedSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20155 / 32 Connecting Network with Operating SystemCheck thenetmodule (download Kernel source and check/usr/src/linux/net)!

2 Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20156 / 32 Application Multiplexing in TCP/IPSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20157 / 32 Application Multiplexing in TCP/IPSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20158 / 32 Application Multiplexing in TCP/IPSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 20159 / 32 What are sockets ?Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201510 / 32 Socket Programming Framework/APIA set ofsystem callsto get the service from TCP/IP protocol stack (netmodule in the OS kernel).Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201511 / 32 Socket TypesThe Internet is a trade-off between performance and reliability -Canyou 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 (SOCKSTREAM): Reliable, connection oriented(TCP based)2 Datagram Socket (SOCKDGRAM): Unreliable, connection less(UDP based)Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201512 / 32 Socket APIint s = Socket (domain, type, protocol);- Create a socketdomain: Communication domain, typically usedAFINET(IPv4 Protocol)type: Type of the Socket -SOCKSTREAMorSOCKDGRAM protocol: Specifies protocols - usually set to 0 Explore!

3 Int status = bind(sockid, &addrport, size);- Reserves aport for the : Socket identifieraddrport:struct sockaddrin- the (IP) address and port of themachine (address usually set toINADDRANY chooses a local address)size: Size of thesockaddrstructureSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201513 / 32struct sockaddrinsinfamily: Address family,AFINETfor IPv4 : Source address,INADDRANYto choose the localaddresssinport: The port numberWe need to usehtons()function to convert the port number fromhost byte ordertonetwork byte sockaddrin serveraddr;int port = 3028; = AFINET; = INADDRANY; = htons(port);Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201514 / 32 Host Byte Order to Network Byte Order - Why?Little Endian and Big Endian SystemAssume a communication from a Little Endian to a Big EndianSystem or vice-versa!

4 Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201515 / 32 Host Byte Order to Network Byte Order - Why?Little Endian and Big Endian SystemAssume a communication from a Little Endian to a Big EndianSystem or vice-versa!Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201515 / 32 Host Byte Order to Network Byte Order - Why?Little Endian and Big Endian SystemAssume a communication from a Little Endian to a Big EndianSystem or vice-versa!Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201515 / 32 Host Byte Order to Network Byte Order - Why?Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201516 / 32 Listen and Accept a Socket Connectionstruct sockaddrin cliaddr;listen(sockfd,5);clilen = sizeof(cliaddr);newsockfd = accept(sockfd,(struct sockaddr *) &cliaddr, Active OpenandPassive OpenThe server needs to announce its address, remains in the open stateand waits for any incoming connections - Passive OpenThe client only opens a connection when there is a need for datatransfer - Active OpenConnection is initiated by the clientSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201517 / 32 Data Transfer through Sockets1 ForSOCKSTREAM:read(newsockfd,buffer,255) ;write(newsockfd, I got your message ,18).)

5 2 ForSOCKDGRAM:recvfrom(sock,buf,1024,0,(s truct sockaddr*)&from, sendto(sock, Got your message ,17,0,(struct sockaddr*)&from,fromlen);Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201518 / 32 Putting it All TogetherCheck the details and sample codes ++ Programming TutorialsBeej s Guide to Network Programming - ~donahoo/practical/ ~moorthy/Courses/os98/ Chakraborty (IIT Kharagpur)CS 39006 March 3, 201519 / 32 Extending the Server Socket for Multiple ConnectionsSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201520 / 32 Extending the Server Socket for Multiple ConnectionsSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201521 / 32select(): Synchronous I/O MultiplexingAssume this scenario:you are a server and you want to listen forincoming connections as well as keep reading from the connectionsyou already ()followed by anaccept()is a blocking ()accept()read()/write()close()[Gives a new file descriptor]Useselect()system Chakraborty (IIT Kharagpur)CS 39006 March 3, 201522 / 32select(): Synchronous I/O Multiplexingselect()gives you the power to monitor several sockets at the sametime.)

6 It ll tell you which ones are ready for reading, which are readyfor writing, and which sockets have raised exceptions, if you reallywant to know select(int numfds, fdset *readfds, fdset *writefds,fdset *exceptfds, struct timeval *timeout);Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201523 / 32 Aselect()Demo#define STDIN 0int main(void){struct timeval tv;fdset readfds; = 2; = 500000;FDZERO( FDSET(STDIN, select(1, &readfds, NULL, NULL, if (FDISSET(STDIN, &readfds))printf( A key was pressed! );elseprintf( Timed out. );return 0;}Sandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201524 / 32 Assignment: Design a Peer-to-Peer File Sharing SystemSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201525 / 32 Assignment: Design a Peer-to-Peer File Sharing SystemSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201526 / 32 Assignment: Design a Peer-to-Peer File Sharing SystemSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201527 / 32 Assignment: Design a Peer-to-Peer File Sharing SystemSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201528 / 32 Assignment: Design a Peer-to-Peer File Sharing SystemSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201529 / 32 Assignment: Design a Peer-to-Peer File Sharing SystemSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201530 / 32 Assignment.)))

7 Design a Peer-to-Peer File Sharing SystemSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201531 / 32 Assignment: Design a Peer-to-Peer File Sharing SystemSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201532 / 32 Thank YouSandip Chakraborty (IIT Kharagpur)CS 39006 March 3, 201532 / 32


Related search queries