1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.

Slides:



Advertisements
Similar presentations
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Advertisements

Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Socket Programming Application Programming Interface.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. 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.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
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.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
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. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
EECC694 - Shaaban #1 lec #14 Spring The Application Layer Client/Server Computing, Basic Approaches: –Passing Messages. Example: Communication.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
Ashutosh Shukla Lecture 3 Network Programming CS 640: Computer Networking.
1 Programming with TCP/IP Ram Dantu. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot.
UNIX Socket Programming CS 6378
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Operating Systems Chapter 9 Distributed Communication.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
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.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
CS 3700 Networks and Distributed Systems
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
CS 3700 Networks and Distributed Systems
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Socket Programming in C
Socket Programming.
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming

2 Internet Networking What is socket?  First appeared in 4.1 BSD UNIX  An integer associated with a network connection.  Implemented as file descriptor.  Types of Internet Sockets:  Stream Sockets for TCP connection.  Datagram Socket for UDP communication.  Raw Sockets.

3 Internet Networking Socket Interface IP Stack Socket I/F ND1ND2ND3 Kernel User Space App1App2

4 Internet Networking Structures  ‘Struct sockaddr’ holds socket address information:  struct sockaddr { unsigned short sa_family;// AF_INET for internet char sa_data[14];// 14 bytes of protocol address. contains a destination address and port number for the socket. };  struct sockaddr_in’ is used to deal with ‘struct sockaddr’ for internet family (AF_INET):  struct sockaddr_in { short int sin_family;// AF_INET for internet unsigned short int sin_port;// Port number struct in_addr sin_addr;// Internet address unsigned char sin_zero[8];// padding (must be zero) };

5 Internet Networking Structures (Cont.)  A pointer to a ‘struct sockaddr_in’ can be cast to a pointer to a ‘struct sockaddr’ and vice-versa  they both have the same size.  In order to set the IP address one should use ‘struct in_addr’:  struct in_addr { unsigned long s_addr; // IPv4 address };

6 Internet Networking Bytes Order  The CPU and the NIC may store and read data from the memory in a different order. Memory CPU NIC R/W

7 Internet Networking Two ways to store data in the memory:  Big-Endian  Little-Endian One should convert the order of the bytes (if necessary) in order to transmit them in network in the same way More: On a big endian machine we see Byte: U N I X Location: On a little-endian machine we would see: Byte: N U X I Location:

8 Internet Networking Bytes Order  Accessories Function:  htons(x) – Convert short from host to network order.  htonl(x)  ntohs(x)  ntohl(x)  In order to be portable one should use this function even if not necessary.

9 Internet Networking System Calls - Socket  int socket(int domain, int type, int protocol);  Return a file descriptor (or -1 in case of error).  domain: AF_INET for internet.  type: SOCK_STREAM for TCP, SOCK_DGRAM for UDP and SOCK_RAW for raw socket.  protocol  Example for UDP: // Opening socket if ((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) error("Could not open socket");

10 Internet Networking System Call - Bind  Associate a socket with a port on the local machine  int bind(int sockfd, struct sockaddr *my_addr, int addrlen);  ‘sockfd’ is the socket file descriptor.  Associate a socket with a port on the local machine (usually used by servers).  ‘my_addr’ is a pointer to a struct sockaddr that contains information about the port.  ‘addrlen’ is set to sizeof(struct sockaddr).  return 0 on success or -1 on failure (when?).

11 Internet Networking Example:  // INADDR_ANY allowed to work without knowing the IP address of the machine running on #define SERVER_PORT 5555 struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(SERVER_PORT); if (bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) error("Could not bind to the socket");

12 Internet Networking Wait for incoming connections, Listen & Accept  int listen(int sockfd, int backlog);  Define the maximum of pending TCP connections.  ‘sockfd’ is the socket file descriptor.  ‘backlog is the number of pending connections incoming connections wait in this queue until accept(). this is the limit on how many can queue up  int accept(int sockfd, void *addr, int *addrlen);  Used by a TCP server to accept pending connection.  Blocked until a connection is arrived.  Return a new socket file descriptor used to receive and send data on this connection by send() and recv() for example.  The original keeps listening on its port  ‘sockfd’ is the new socket file descriptor.  ‘addr’ contains information (port and IP address) of the incoming connection (who is calling).  ‘addrlen’ is a local integer variable set to sizeof(struct sockaddr_in).

13 Internet Networking Connect to a Remote Host  int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);  Used by clients to connect a socket to a server.  ‘sockfd’ is the socket file descriptor.  ‘struct sockaddr’ contains the destination port and IP address  ‘addrlen’ set to sizeof(struct sockaddr).

14 Internet Networking Data Transfer – Send over stream sockets (TCP)  int send(int sockfd, const void *msg, int len, int flags);  Transmit data through a socket.  Can be used only when the socket is connected.  Return the number of bytes that has been sent (or -1 in case of error).  ‘buf’ is a pointer to the buffer to be sent, while ‘len’ is its length.  Flags – mostly zero.  Flag example: MSG_DONTROUTE Don't use a gateway to send out the packet, only send to hosts on directly connected networks.

15 Internet Networking Data Transfer – Receive over stream sockets (TCP)  int recv(int sockfd, void *buf, int len, unsigned int flags);  Obtain data from the socket.  Used only when the socket is in a connected state.  ‘buf’ is a pointer to the buffer for holding the received data and ‘len’ is its length.  Returns the number of bytes actually sent (or -1).

16 Internet Networking Data Transfer – Send over Datagram sockets (UDP)  Datagram sockets are not connected to a remote host  int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen);  Transmit data to through a socket. The destination (port and IP address) is specified in the call.  Used for UDP communication.

17 Internet Networking Data Transfer – Receive over stream sockets (UDP)  int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen);  Obtain data from the socket.  from is a pointer to a local struct sockaddr that will be filled with the IP address and port of the originating machine.  fromlen is a pointer to a local int that should be initialized to sizeof(struct sockaddr).  When the function returns, fromlen will contain the length of the address actually stored in from.  Returns the number of bytes actually sent.

18 Internet Networking Shut Down and Close  int shutdown(int sockfd, int how);  sockfd - socket file descriptor to shutdown  How:  0 - Further receives are disallowed  1 - Further sends are disallowed  2 - Further sends and receives are disallowed (like close())  close(sockfd);  sockfd - socket file descriptor to shutdown  Only close() frees the socket descriptor

19 Internet Networking Client-Server Model UDP Client sd=socket(…) sendto(sd,…) recvfrom(sd,…) close(sd) UDP Server sd=socket(…) bind(sd,…) recvfrom(sd,…) sendto(sd,…) close(sd)

20 Internet Networking Client-Server Model TCP Client sd=socket(…) connect(sd) send(sd,…) recv(sd,…) close(sd) TCP Server sd=socket(…) bind(sd,…) listen(sd,…) new_sd=accept(sd,…) fork() recv(new_sd,…) send(new_sd,…) close(new_sd)

21 Internet Networking Port Assignment  Ports 0 through 1023 are reserved.  In order to let the system to assign the port number, one should set the port number (e.g. in ‘bind’) to zero.