Socket Programming.

Slides:



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

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
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
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.
Lecture 8 UDP Sockets & I/O Multiplexing
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.
1 Programming with TCP/IP Ram Dantu. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot.
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.
Assignment 3 A Client/Server Application: Chatroom.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
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.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
Remote Shell CS230 Project #4 Assigned : Due date :
1 COMP/ELEC 429/556 Introduction to Computer Networks Creating a Network Application Some slides used with permissions from Edward W. Knightly, T. S. Eugene.
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.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
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 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.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
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.
UNIX Sockets Outline UNIX sockets CS 640.
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 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Review: TCP Client-Server Interaction
Imam Ahmad Trinugroho, ST., MMSI
Transport layer API: Socket Programming
Socket Programming What is a socket? Using sockets Types (Protocols)
UNIX Sockets Outline Homework #1 posted by end of day
Lecture 4 Socket Programming Issues
Socket Programming in C
Chapter 2 Application Layer
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming(1/2)
Socket Programming What is a socket? Using sockets Types (Protocols)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Socket Programming What is a socket? Using sockets Types (Protocols)
Sockets.
Presentation transcript:

Socket Programming

Interprocess Communication Within a single system Pipes, FIFOs Message Queues Semaphores, Shared Memory Across different systems BSD Sockets Transport Layer Interface (TLI) Reference Unix Network Programming by Richard Stevens Computer Science, FSU

BSD Socket API Introduced in 1981 BSD 4.1 UNIX Sockets is just a convenient interface for the processes on different hosts to communicate. Just like if you want to write to the hard disk, you can operate the hard disk directly, but it is much more convenient to open a file and write to the file using the interface provided by the system. Computer Science, FSU

Socket A 5-tuple associated with a socket {protocol, local IP address, local port, remote IP address, remote port} Complete socket is like a file descriptor Both send() and recv() through same socket

Sockets: Conceptual View You can think of the socket layer as another layer between application and transport layers. Or socket layer as the interface to transport layer services. Socekt is an interface between app. Layer and the tcp layer. It is much more convenient to use it than to use the real IP address and port number. Computer Science, FSU

Connection-Oriented Application Server gets ready to service clients Creates a socket Binds an address (IP interface, port number) to the socket Server’s address should be made known to clients Client contacts the server Connects to the server Client has to supply the address of the server when using connect() Accepts connection requests from clients Further communication is specific to application Computer Science, FSU

Creating a socket int socket(int family, int service, int protocol) family: symbolic name for protocol family AF_INET, AF_UNIX type: symbolic name for type of service SOCK_STREAM, SOCK_DGRAM, SOCK_RAW protocol: further info in case of raw sockets typically set to 0 Returns socket descriptor The socket function call simply allocates a socket structure the contents of which get filled in by other calls such as bind(), connect(). This socket() call returns a socket descriptor which is used to refer to the socket for any subsequent operation on the socket. Computer Science, FSU

Binding Socket with an Address int bind(int sd, struct sockaddr *addr, int len) sd: socket descriptor returned by socket() addr: pointer to sockaddr structure containing address to be bound to socket len: length of address structure Returns 0 if success, -1 otherwise Computer Science, FSU

Specifying Socket Address struct sockaddr_in { short sin_family; /* set to AF_INET */ u_short sin_port; /* 16 bit port number */ struct in_addr sin_addr; /* 32 bit host address */ char sin_zero[8]; /* not used */ }; struct in_addr { u_long s_addr; /* 32 bit host address */ Computer Science, FSU

Bind Example int sd; struct sockaddr_in ma; sd = socket(AF_INET, SOCK_STREAM, 0); ma.sin_family = AF_INET; ma.sin_port = htons(5100); ma.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sd, (struct sockaddr *) &ma, sizeof(ma)) != -1) … In this example, we are assuming that server is being bound to port 5100. The function htons() converts a short integer (port is a 16 bit number) from host format to network (standard) format. The special value for IP address INADDR_ANY says that the server is willing to accept requests addressed to any of its machine’s IP addresses. This option conveniently covers the case of the server machine having multiple interfaces each with an IP address. Similar to htons, the function htonl converts a long integer (IP address is a 32 bit number) from host format to network format. Computer Science, FSU

Connecting to Server int connect(int sd, struct sockaddr *addr, int len) sd: socket descriptor returned by socket() addr: pointer to sockaddr structure containing server’s address (IP address and port) len: length of address structure Returns 0 if success, -1 otherwise Computer Science, FSU

Connect Example int sd; struct sockaddr_in sa; sd = socket(AF_INET, SOCK_STREAM, 0); sa.sin_family = AF_INET; sa.sin_port = htons(5100); sa.sin_addr.s_addr = inet_addr(“128.101.34.78”); if (connect(sd, (struct sockaddr *) &sa, sizeof(sa)) != -1) … For connect(), we have to pass the whereabouts the server so that we can connect to the server. Here we are assuming that server is running on a machine with IP address 128.101.34.78 and bound to port 5100. The function inet_addr is used to convert IP address from dotted decimal string form to 32 bit integer (network byte order). Once the connect() is successful, the socket descriptor sd can be used just like a file descriptor to read from and write to the server. Computer Science, FSU

Connection Acceptance by Server int accept(int sd, struct sockaddr *from, int *len) sd: socket descriptor returned by socket() from: pointer to sockaddr structure which gets filled with client’s address len: length of address structure Blocks until connection requested or error returns a new socket descriptor on success For accept() you pass a structure and it gets filled with the details of the client when the accept() call returns. Note that for the third argument you have to pass a pointer to an integer so that the accept() call can store the length of the structure that got filled. The accept() call blocks until there is a connection request from a client. It is important to remember that accept() call returns a new socket descriptor (unlike connect() call). This new socket descriptor can be used by the server to communicate with the specific client that requested the connection. Computer Science, FSU

Connection-oriented Server int sd, cd, calen; struct sockaddr_in ma, ca; sd = socket(AF_INET, SOCK_STREAM, 0); ma.sin_family = AF_INET; ma.sin_port = htons(5100); ma.sin_addr.s_addr = htonl(INADDR_ANY); bind(sd, (struct sockaddr *) &ma, sizeof(ma)); listen(sd, 5); calen = sizeof(ca); cd = accept(sd, (struct sockaddr *) &ca, &calen); …read and write to client treating cd as file descriptor… As pointed out in the previous slide, accept() call returns a new socket descriptor which in this case is cd. This cd can be used to read from/write to the client. The original socket descriptor sd can continue to be passed to any future accept() calls for accepting more connections. Computer Science, FSU

More on Socket Descriptor socket() fills the protocol component local IP address/port filled by bind() remote IP address/port by accept() in case of server in case of client both local and remote by connect() accept() returns a new complete socket Original one can be used to accept more connections Computer Science, FSU

Streams and Datagrams Connection-oriented reliable byte stream SOCK_STREAM based on TCP No message boundaries Multiple write() may be consumed by one read() Connectionless unreliable datagram SOCK_DGRAM based on UDP Message boundaries are preserved Each sendto() corresponds to one recvfrom() Computer Science, FSU

Input/Output Multiplexing Polling Nonblocking option using fcntl()/ioctl() Waste of computer resources Asynchronous I/O Generates a signal on an input/output event Expensive to catch signals Wait for multiple events simultaneously Using select() system call Process sleeps till an event happens Suppose you want to read from a keyboard and write that to a server and also display on the screen whatever is written by the sever. So you have to worry about input from either keyboard or socket. You can not read one at a time. There may be input from the a source while you are blocked reading from other source. One solution is to use fcntl() and ioctl() calls to make I/O on a stream non-blocking. In that case a read would return immediately either with some input or 0 bytes if nothing to be read. We can then poll the descriptors in a round robin fashion. This approach of periodic polling wastes cpu resources. Instead it is better if our process is notified whenever there is some input on any of the sources. Using fcntl() and ioctl() calls, it is possible to make the system generate a SIGIO signal whenever there is something to be read on a file descriptor. A signal handler can be setup to be called to service that signal. Then within the signal handler, we can probe all the descriptors to find the descriptor with some input. A better alternative is to wait on multiple descriptors simultaneously. Using select() call we can wait till some event happens or specified time expires. Computer Science, FSU

Select System Call int select(int maxfdp1, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) maxfdp1: largest numbered file descriptor + 1 readfds: check if ready for reading writefds: check if ready for writing exceptfds: check for exceptional conditions timeout: specifies how long to wait for events Computer Science, FSU

Timeout in Select Wait indefinitely till there is an event Pass NULL to the timeout argument Don’t wait beyond a fixed amount of time Pass pointer to a timeval structure specifying the number of seconds and microseconds. Just poll without blocking Pass pointer to a timeval structure specifying the number of seconds and microseconds as 0 Computer Science, FSU

Working with File Descriptor Set Set is represented by a bit mask Keep a descriptor in/out the set, turn on/off corresponding bit Using FD_ZERO, FD_SET and FD_CLR Use FD_ISSET to check for membership Example: Make descriptors 1 and 4 members of the readset fd_set readset; FD_ZERO(&readset); FD_SET(1, &readset); FD_SET(4, &readset); Check if 4 is a member of readset FD_ISSET(4, &readset); Computer Science, FSU

Return Values from Select Arguments readfds etc are value-result Pass set of descriptors you are interested in Select modifies the descriptor set Keeps the bit on if an event on the descriptor Turns the bit off if no event on the descriptor On return, test the descriptor set Using FD_ISSET Computer Science, FSU

Select Example fd_set readset; FD_ZERO(&readset); FD_SET(0, &readset); select(5, &readset, NULL, NULL, NULL); if (FD_ISSET(0, &readset) { /* something to be read from 0 */ } if (FD_ISSET(4, &readset) { /* something to be read from 4 */ } Note that the first argument is 5 (largest descriptor + 1) because the largest descriptor we are interested in is 4. Computer Science, FSU

Servers and Services Mapping between names and addresses (DNS) Host name to address: gethostbyname() Host address to name: gethostbyaddr() Computer Science, FSU

send() and recv() Once the sockets have been set up, the bytes can be sent and receive using send() and recv(). send(): ssize_t send(int socket, const void *buffer, size_t length, int flags); Returns the number of bytes sent. -1 if error. To send, fill the buffer to length, and call send(). recv(): ssize_t recv(int socket, void *buffer, size_t length, int flags); Returns the number of bytes received. If returns 0, means the connection is down. -1 if error. When the socket fd is set, call recv(), and check the return value and buffer.