回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.

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.
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.
Socket Programming Computer Networks, Spring 2008 Xi Liu.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
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.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Introduction to Project 1 Web Client and Server Jan 2006.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
Yu-Chi Lai Lecture 3 Network Programming CS 640: Computer Networking.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
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.
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.
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
Operating Systems Chapter 9 Distributed Communication.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
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.
Computer Systems II CSC 2405 Network Programming.
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.
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.
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.
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.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
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.
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.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
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.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
Linux Networks TCP/IP Networking Layers BSD Socket Interface
TCP Sockets Programming
Socket Programming.
Socket Programming(1/2)
Sockets Programming Socket to me!.
Computer Networks, Spring 2013
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need to know anything about the client u even that it exists n The client should always know something about the server u at least where it is located Client process Server process 1. Client sends request 2. Server handles request 3. Server sends response 4. Client handles response Resource Note: clients and servers are processes running on hosts (can be the same or different hosts).

回到第一頁 Servers n Servers are long-running processes (daemons). u Created at boot-time (typically) by the init process (process 1) u Run continuously until the machine is turned off. n Each server waits for requests to arrive on a well-known port associated with a particular service. u Port 7: echo server u Port 23: telnet server u Port 25: mail server u Port 80: HTTP server n Other applications should choose between 1024 and See /etc/services for a comprehensive list of the services available on a Linux machine.

回到第一頁 Clients n Examples of client programs  Web browsers, ftp, telnet, ssh n How does a client find the server? u The IP address in the server socket address identifies the host u The (well-known) port in the server socket address identifies the service, and thus implicitly identifies the server process that performs that service. u Examples of well known ports F Port 7: Echo server F Port 23: Telnet server F Port 25: Mail server F Port 80: Web server

回到第一頁 Sockets n What is a socket? u To the kernel, a socket is an endpoint of communication. u To an application, a socket is a file descriptor that lets the application read/write from/to the network. F Remember: All Unix I/O devices, including networks, are modeled as files. n Clients and servers communicate with each by reading from and writing to socket descriptors. n The main distinction between regular file I/O and socket I/O is how the application “opens” the socket descriptors.

回到第一頁 Linux Socket n The socket interface is an extension of pipes. Sockets are used to communicate across networks.

回到第一頁 Client / Server Session ClientServer socket bind listen read writeread write Connection request read close EOF open_listenfd acceptconnect open_clientfd Overview

回到第一頁 Creating a Socket Domains include:

回到第一頁 Step 1 – Setup Socket Both client and server need to setup the socket – int socket(int domain, int type, int protocol); domain – AF_INET -- IPv4 (AF_INET6 for IPv6)‏ type – SOCK_STREAM -- TCP – SOCK_DGRAM -- UDP protocol – 0 For example, – int sockfd = socket(AF_INET, SOCK_STREAM, 0);

回到第一頁 Step 2 (Server) - Binding Only server need to bind – int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen); sockfd – file descriptor socket() returned my_addr – struct sockaddr_in for IPv4 struct sockaddr_in { short sin_family; // e.g. AF_INET unsigned short sin_port; // e.g. htons(3490)‏ struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // zero this if you want to }; struct in_addr { unsigned long s_addr; // load with inet_aton()‏ };

回到第一頁 struct sockaddr_in { unsigned short sin_family; /* address family (always AF_INET) */ unsigned short sin_port; /* port num in network byte order */ struct in_addr sin_addr; /* IP addr in network byte order */ }; Struct sockaddr_un{ sa_family_t sun_family; /*AF_UNIX */ char sun_path[] ; /* pathname */ }; Struct in_addr{ unsigned long int s_addr; };

回到第一頁 Step 3 (Server) - Listen Now we can listen – int listen(int sockfd, int backlog); sockfd – again, file descriptor socket() returned backlog – number of pending connections to queue For example, – listen(sockfd, 5);

回到第一頁 Step 4 (Server) - Accept Server must explicitly accept incoming connections – int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)‏ sockfd – again... file descriptor socket() returned addr – pointer to store client address, (struct sockaddr_in *) cast to (struct sockaddr *)‏ addrlen – pointer to store the returned size of addr, should be sizeof(*addr)‏ For example – int isock=accept(sockfd, (struct sockaddr_in *) &caddr, &clen);

回到第一頁 What about client? Client need not bind, listen, and accept All client need to do is to connect – int connect(int sockfd, const struct sockaddr *saddr, socklen_t addrlen); For example, – connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr));

回到第一頁 Network programming n socket(family, type, protocol) n bind(fd, uaddr, addrlen) n listen(fd, backlog) n accept(fd, sockaddr, skaddrlen)

回到第一頁 socket(family, type, protocol) n u sys_socket(...) u get an empty inode for socket structure u allocate a sock structure u return the file descriptor of the socket if success

回到第一頁 bind(fd, uaddr, addrlen) n To make a socket available for use by other processes, a server program needs to give the socket a name. n For AF_INET sockets, this means associating the socket with an IP port number. n n sys_bind(...) u bind a name to a socket u move the socket address to kernel space u address & port checking

回到第一頁 listen(fd, backlog) n To accept incoming connections on a socket, a server program must create a queue to store pending requests. n u sys_listen(...) u allow the protocol to do anything necessary u move a socket into listening state

回到第一頁 accept(fd, sockaddr, skaddrlen) n u sys_accept(...) u attempt to create a new socket u set up the link with the client u wake up the client u return the new fd

回到第一頁 connect(fd, uaddr, addrlen) n u sys_connect(...) u attempt to connect to a socket with the server address u move the address to kernel space

回到第一頁 Closing a Socket n You can terminate a socket connection at the server and client by calling close.

回到第一頁 Socket Programming Clients n Network Byte Ordering u Network is big-endian, host may be big- or little-endian u Functions work on 16-bit (short) and 32-bit (long) values u htons() / htonl() : convert host byte order to network byte order u ntohs() / ntohl(): convert network byte order to host byte order u Use these to convert network addresses, ports, … n Structure Casts u You will see a lot of ‘structure casts’ struct sockaddr_in serveraddr; /* fill in serveraddr with an address */ … /* Connect takes (struct sockaddr *) as its second argument */ connect(clientfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)); …

回到第一頁 Network Information

回到第一頁 Similarly, information concerning services and associated port numbers is availble through some service information functions.

回到第一頁 The address list needs to be cast to the appropriate address type and converted from network ordering to a printable string, using the inet_ntoa conversion.

回到第一頁 What is select()? Monitor multiple descriptors How does it work? – Setup sets of sockets to monitor – select(): blocking until something happens – “Something” could be Incoming connection: accept()‏ Clients sending data: read()‏ Pending data to send: write()‏ Timeout

回到第一頁 What is FD_SET()? #include #include void FD_ZERO(fd_set *fdset); void FD_CLR(int fd, fd_set *fdset); void FD_SET(int fd, fd_set *fdset); int FD_ISSET(int fd, fd_set *fdset); FD_ZERO initializes an fd_set to the empty set, FD_SET and FD_CLR set and clear elements of the set fdsetcorresponding to the file descriptor passed as fd FD_ISSET returns non-zero if the file descriptor referred to by fd is an element of the fd_set pointed to by fdset.

回到第一頁 prevent indefinite blocking The select function can also use a timeout value to prevent indefinite blocking. The timeout value is given using a struct timeval. This structure, defined in sys/time.h, has the following members: struct timeval { time_t tv_sec; /* seconds */ long tv_usec; /* microseconds */ }

回到第一頁 #include int main() { char buffer[128]; int result, nread; fd_set inputs, testfds; struct timeval timeout; FD_ZERO(&inputs); FD_SET(0,&inputs); while(1) { testfds = inputs; timeout.tv_sec = 2; timeout.tv_usec = ; result = select(FD_SETSIZE, &testfds, (fd_set *)NULL, (fd_set *)NULL, &timeout); switch(result) { case 0: printf("timeout\n"); break; case -1: perror("select"); exit(1); default: if(FD_ISSET(0,&testfds)) { ioctl(0,FIONREAD,&nread); if(nread == 0) { printf("keyboard done\n"); exit(0); } nread = read(0,buffer,nread); buffer[nread] = 0; printf("read %d from keyboard: %s", nread, buffer); } break; } } }

回到第一頁 ioctl Interfaces The FIONREAD: ioctl returns the number of data bytes (in all data messages queued) in the location pointed to by the arg parameter.