1 Comnet 2010 - Recitation 2 - Sockets Communication Networks Recitation 2.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Socket Programming CS3320 Fall 2010.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Socket Programming Application Programming Interface.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
Computer Networks Sockets. Sockets and the OS F An end-point for Internet connection –What the application “plugs into” –OS provides Application Programming.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
1 Netcomm Sockets Communication Networks Recitation 2.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
Introduction to Project 1 Web Client and Server Jan 2006.
Computer Networks Sockets. Outline F Socket basics F Socket details.
UNIX Sockets COS 461 Precept 1.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles.
CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.
Lecture 2 Socket Programming Spring 2004
ECE 4110 – Internetwork Programming Client-Server Model.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
CS x760 Computer Networks1 Socket Programming. CS 6760 Computer Networks2 Socket Programming  What is a socket?  Using sockets  Types (Protocols) ‏
Computer Networks : TP1 Prof. Dr. Amine Berqia and Prof. Dr. Fernando Lobo {bamine,
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
1 Introduction to Computer Networks Ilam University By: Dr. Mozafar Bag Mohammadi Sockets.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C m Note:
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
UNIX Internet Socket API
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
EECS340 Recitation 1: Very helpful to your project Hongyu Gao 1.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
@Yuan Xue CS 283Computer Networks Spring 2013 Instructor: Yuan Xue.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Socket Programming What is a socket? Using sockets Types (Protocols)
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
Socket Programming in C
Review: TCP Client-Server Interaction
Introduction to Computer Networks
Imam Ahmad Trinugroho, ST., MMSI
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming.
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming(1/2)
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming What is a socket? Using sockets Types (Protocols)
Presentation transcript:

1 Comnet Recitation 2 - Sockets Communication Networks Recitation 2

2 Comnet Recitation 2 - Sockets TCP/IP Socket Programming Cont.

3 Comnet Recitation 2 - Sockets Outline Send/Receive data Terminating a connection Dealing with blocking calls Client-server model summary

4 Comnet Recitation 2 - Sockets IP Address/Interfaces Identifies a host interface (not a host)Identifies a host interface (not a host) Interface: connection between host or router and the physical network linkInterface: connection between host or router and the physical network link Hosts may have multiple interfacesHosts may have multiple interfaces

5 Comnet Recitation 2 - Sockets Sending / Receiving Data With a connection (SOCK_STREAM):With a connection (SOCK_STREAM): –int count = send(sock, &buf, len, flags); count: # bytes transmitted (-1 if error)count: # bytes transmitted (-1 if error) buf: char[], buffer to be transmittedbuf: char[], buffer to be transmitted len: integer, length of buffer (in bytes) to transmitlen: integer, length of buffer (in bytes) to transmit flags: integer, special options, usually just 0flags: integer, special options, usually just 0 –int count = recv(sock, &buf, len, flags); count: # bytes received (-1 if error)count: # bytes received (-1 if error) buf: void[], stores received bytesbuf: void[], stores received bytes len: max # bytes receivedlen: max # bytes received flags: integer, special options, usually just 0flags: integer, special options, usually just 0

6 Comnet Recitation 2 - Sockets Example of send() … connect(sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)); … char *msg = "Comnet is the course!"; int len, bytes_sent; len = strlen(msg); bytes_sent = send(sock, msg, len, 0);

7 Comnet Recitation 2 - Sockets Sending / Receiving Data (cont’d) Without a connection (SOCK_DGRAM):Without a connection (SOCK_DGRAM): –int count = sendto(sock, &buf, len, flags, &addr, addrlen); count, sock, buf, len, flags: same as sendcount, sock, buf, len, flags: same as send addr: struct sockaddr, address of the destinationaddr: struct sockaddr, address of the destination addrlen: sizeof(addr)addrlen: sizeof(addr) –int count = recvfrom(sock, &buf, len, flags, &addr, &addrlen); count, sock, buf, len, flags: same as recvcount, sock, buf, len, flags: same as recv name: struct sockaddr, address of the sourcename: struct sockaddr, address of the source namelen: sizeof(name): value/result parameternamelen: sizeof(name): value/result parameter

8 Comnet Recitation 2 - Sockets Sending/Receiving notes send( ) might send only a fraction of the buffersend( ) might send only a fraction of the buffer –Need to push the rest –See sendall( ) in Beej’s guide recv( ) might get several messages together, or a fraction of a messagerecv( ) might get several messages together, or a fraction of a message –Need to be able to identify message’s beginning and end

9 Comnet Recitation 2 - Sockets Closing connection When finished using a socket, the socket should be closed:When finished using a socket, the socket should be closed: status = close(s);status = close(s); –status: 0 if successful, -1 if error –s: the file descriptor (socket being closed) Closing a socketClosing a socket –closes a connection (for SOCK_STREAM) –frees up the port used by the socket

10 Comnet Recitation 2 - Sockets Skipping the bind() SOCK_DGRAM:SOCK_DGRAM: –if only sending, no need to bind. The OS finds a port each time the socket sends a packet –if receiving, need to bind SOCK_STREAM:SOCK_STREAM: –The OS binds a port to the client during connection setup

11 Comnet Recitation 2 - Sockets Dealing with blocking calls Many of the functions we saw block until a certain eventMany of the functions we saw block until a certain event –accept: until a connection comes in –connect: until the connection is established –recv, recvfrom: until a packet (of data) is received –send, sendto: until data is pushed into socket’s buffer For simple programs, blocking is convenientFor simple programs, blocking is convenient What about more complex programs?What about more complex programs? –multiple connections –simultaneous sends and receives –simultaneously doing non-networking processing

12 Comnet Recitation 2 - Sockets Dealing with blocking (cont’d) Options:Options: –create multi-process or multi-threaded code –turn off the blocking feature using the fcntl() file- descriptor control function –use the select() function call.

13 Comnet Recitation 2 - Sockets fcntl() function call int fcntl(int filedes, int cmd, args...);int fcntl(int filedes, int cmd, args...); –status: -1 if error –filedes: argument is an open file descriptor –cmd: specifies the operation to be performed –arg: depends upon the value of cmd sockfd = socket(PF_INET, SOCK_STREAM, 0); fcntl(sockfd, F_SETFL, O_NONBLOCK);

14 Comnet Recitation 2 - Sockets Using select() What does select do?What does select do? –can be permanent blocking, time-limited blocking or non-blocking –input: a set of file-descriptors –output: info on the file-descriptors’ status –i.e., can identify sockets that are “ready for use”: calls involving that socket will return immediately

15 Comnet Recitation 2 - Sockets select() function call int status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);int status = select(nfds, &readfds, &writefds, &exceptfds, &timeout); –status: # of ready objects, -1 if error –nfds: 1 + largest file descriptor to check –readfds: set of descriptors to check if read-ready –writefds: set of descriptors to check if write-ready –exceptfds: set of descriptors to check if an exception is registered –timeout: time after which select returns, even if nothing ready - can be 0 or 

16 Comnet Recitation 2 - Sockets To be used with select(): For sets, select uses a structure, struct fd_setFor sets, select uses a structure, struct fd_set –it is just a bit-vector –if bit i is set in [readfds, writefds, exceptfds], select will check if file descriptor (i.e. socket) i is ready for [reading, writing, exception] Before calling select:Before calling select: –FD_ZERO(&fdvar): clears the structure –FD_SET(i, &fdvar): to check file desc. i After calling select:After calling select: –int FD_ISSET(i, &fdvar): boolean returns TRUE iff i is “ready to read” Standard input (STDIN) is file descriptor 0Standard input (STDIN) is file descriptor 0

17 Comnet Recitation 2 - Sockets Timeout parameter struct timeval { int tv_sec; // seconds int tv_usec; // microseconds }; Set timeout to NULL for no timeoutSet timeout to NULL for no timeout Set both to 0 for immediate timeoutSet both to 0 for immediate timeout

18 Comnet Recitation 2 - Sockets Ready to read Listening port is read-ready if accept won’t blockListening port is read-ready if accept won’t block Closed connections are read-ready but recv( ) returns 0 bytesClosed connections are read-ready but recv( ) returns 0 bytes

19 Comnet Recitation 2 - Sockets Other useful functions bzero(char* c, int n): 0’s n bytes starting at cbzero(char* c, int n): 0’s n bytes starting at c gethostname(char *name, int len): gets the name of the current hostgethostname(char *name, int len): gets the name of the current host gethostbyname(const char *name): converts host’s name to structure containing long integergethostbyname(const char *name): converts host’s name to structure containing long integer char hostname[128]; struct hostent *he; gethostname( hostname, sizeof( hostname) ); printf( “My hostname:%s\n”, hostname ); he = gethostbyname( “ ); printf( “Address: %s\n”, inet_n_ntoa( he->h_addr ) );

20 Comnet Recitation 2 - Sockets Useful functions (cont.) gethostbyaddr(char *addr, int len, int type): converts IP hostname to structure containing long integergethostbyaddr(char *addr, int len, int type): converts IP hostname to structure containing long integer inet_aton(const char *cp, struct in_addr *inp): converts dotted-decimal char-string to binary (network order)inet_aton(const char *cp, struct in_addr *inp): converts dotted-decimal char-string to binary (network order) inet_ntoa(const struct in_addr in): converts binary to dotted-decimal stringinet_ntoa(const struct in_addr in): converts binary to dotted-decimal string struct hostent *he; struct in_addr addr; inet_aton( “ ”, &addr ); printf( “Address: %s\n”, inet_ntoa( addr.sin_addr ) ); he = gethostbyaddr( &addr, sizeof( addr ), AF_INET ); printf( “Name: %s\n”, he->h_name );

21 Comnet Recitation 2 - Sockets UDP Client-Server socket() close() recvfrom() sendto() recvfrom() bind() socket() blocks process request data (request) data (reply) UDP Client UDP Server bind() close()

22 Comnet Recitation 2 - Sockets TCP Client-Server socket() connect() write() read() close() socket() bind() listen() accept() read() write() close() read() TCP Client TCP Server blocks until connection from client process request end-of-file notification data (reply) data (request) connection establishment 3-way handshake

23 Comnet Recitation 2 - Sockets Client-server Example request reply telnet ftp Port 20 Port 23 Port 2356 Port 3325 Client 1 Client 2Server IP= IP= IP=

24 Comnet Recitation 2 - Sockets Quick Unix reminder ps –efps –ef grepgrep jobsjobs killkill manman ^z + bg^z + bg |, |,