Sop, Fan Reference: Daniel Spangenberger Computer Networks PPT-4 Socket Programming.

Slides:



Advertisements
Similar presentations
Socket Programming Computer Networks, Spring 2008 Your TAs.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Concurrent Servers May 31, 2006 Topics Limitations of iterative servers Process-based concurrent servers Event-based concurrent servers Threads-based concurrent.
I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
Daemon Processes Long lived utility processes Often started at system boot and ended when system shuts down Run in the background with no controlling terminal.
Today’s topic Issues about sending structures with TCP. Server design alternatives: concurrent server and multiplexed server. I/O multiplexing.
Computer Networks Sockets. Sockets and the OS F An end-point for Internet connection –What the application “plugs into” –OS provides Application Programming.
Socket Programming Computer Networks, Spring 2008 Xi Liu.
Advanced Sockets Amit Mondal TA, Intro to Networking Jan 22, 2009 Recital 3 Introduction to Networking Instructor: Prof. Aleksandar Kuzmanovic.
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.
Sockets and concurrency Spring 2010, Recitation 3 Your Awesome TAs.
I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
1 Data Communications and Networking Socket Programming Part II: Design of Server Software Reference: Internetworking with TCP/IP, Volume III Client-Server.
I/O Multiplexing Capability of tell the kernel that wants to be notified when one or more I/O conditions are ready. For example, I/O data is available.
Computer Networks Sockets. Outline F Socket basics F Socket details.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
Select The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O. int select( int nfds, fd_set*
IP Multiplexing Ying Zhang EECS 489 W07.
Daniel Spangenberger Computer Networks, Fall 2007.
CS4516: Medical Examiner Client/Server Evan Frenn 1.
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
A PPLICATION L AYER Dr. Nawaporn Wisitpongphan Derived from Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose,
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.
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Networking Concepts, Socket Programming Spring 2012.
1 Internet Socket programming Behzad Akbari. 2 Sharif University of Technology, Kish Island Campus What is an API? API – stands for Application Programming.
Concurrent Servers April 25, 2002 Topics Limitations of iterative servers Process-based concurrent servers Threads-based concurrent servers Event-based.
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 :
TELE 402 Lecture 4: I/O multi … 1 Overview Last Lecture –TCP socket and Client-Server example –Source: Chapters 4&5 of Stevens’ book This Lecture –I/O.
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.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
1 I/O Multiplexing We often need to be able to monitor multiple descriptors:We often need to be able to monitor multiple descriptors: –a generic TCP client.
CSCE 515: Computer Network Programming Select Wenyuan Xu Department of Computer Science and Engineering.
Socket Programming Lab 1 1CS Computer Networks.
I/O Multiplexing. What is I/O multiplexing? When an application needs to handle multiple I/O descriptors at the same time –E.g. file and socket descriptors,
CONCURRENT PROGRAMMING Lecture 5. 2 Assignment 1  Having trouble?  Resolve it as early as possible!  Assignment 2:  Handling text-based protocol 
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
UNIX Internet Socket API
Threads versus Events CSE451 Andrew Whitaker. This Class Threads vs. events is an ongoing debate  So, neat-and-tidy answers aren’t necessarily available.
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
回到第一頁 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.
Carnegie Mellon Proxy & Networking : Introduction to Computer Systems – Recitation H April 11, 2011.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
@Yuan Xue CS 283Computer Networks Spring 2013 Instructor: Yuan Xue.
– 1 – , Fall 2003 Network Programming Introduction Sept. 1, 2004 Topics Programmer's view of the Internet Sockets interface Writing clients and servers.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
More Project 1 and HW 1 stuff! Athula Balachandran Wolfgang Richter
I/O Multiplexing.
Concurrent Servers December 7, 2000
Socket programming Péter Verhás August 2002
CS 1652 Jack Lange University of Pittsburgh
Pertemuan 7 I/O Multiplexing
Review: TCP Client-Server Interaction
Imam Ahmad Trinugroho, ST., MMSI
Concurrent Servers Topics Limitations of iterative servers
Advanced Sockets Introduction to Networking
Lecture 4 Socket Programming Issues
Lecture 11 Overview.
TCP Sockets Programming
Socket Programming.
TCP/IP Socket Programming in C
Computer Networks, Spring 2013
Concurrent Programming December 2, 2004
I/O Multiplexing We often need to be able to monitor multiple descriptors: a generic TCP client (like telnet) need to be able to handle unexpected situations,
Presentation transcript:

Sop, Fan Reference: Daniel Spangenberger Computer Networks PPT-4 Socket Programming

Concurrency Review  Why should we use …?  Our experiences? Socket Programming related  System requirement(robustness…)  How to cooperate with socket API? Example  How to modify the example to fit further requirement?

Clients User 1 (goes to lunch) Server connect() accept() fgets() User 2 read() connect() Blocked! Blocks!

Processes  Uses fork()  Easy to understand(actually we have implemented one version!)  A lot to consider about causing complexity(zombie, syscall…) Threads  Natural concurrency (new thread per connection)  Easier to understand (you know it already)  Complexity is increased (possible race conditions) Use non-blocking I/O  Uses select()  Explicit control flow (no race conditions!)  Explicit control flow more complicated though

Fork()  Use Pid to verify different process  Assign different task flow accordingly Signal & waitpid(…)  Tracing child processes, kill the zombies Other process control methods?

pthread_create  Create thread according to detailed settings Pthread(series: join, detach, cancel…)  Imply different polices. Other thread control methods?

Monitor sockets with select()  int select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout); So what’s an fd_set ?  Bit vector with FD_SETSIZE bits maxfd – Max file descriptor + 1 readfs – Bit vector of read descriptors to monitor writefds – Bit vector of write descriptors to monitor exceptfds – Read the manpage, set to NULL timeout – How long to wait with no activity before returning, NULL for eternity

void FD_ZERO(fd_set *fdset);  Clears all the bits void FD_SET(int fd, fd_set *fdset);  Sets the bit for fd void FD_CLR(int fd, fd_set *fdset);  Clears the bit for fd int FD_ISSET(int fd, fd_set *fdset);  Checks whether fd’s bit is set

Requirements  Mass users  User Experience Incidents  Server/Network/Client breakdown?  Hacking?  …

Server Client(s) socket() connect() write() read() close() socket() bind() listen() select() write() read() close() read() Hacking! Hacking!!! Exceptions here! Server breakdown! FD_ISSET(sfd) accept() check_clients() main loop … …

Socket API offers variable settings to meet different demands. (methods settings) Programming concurrency with different detailed settings. Exception/ error cases handling

Address re-use Non-blocking int sock, opts; sock = socket(…); // getting the current options setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts)); int sock, opts; sock = socket(…); // getting the current options setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts)); // getting current options if (0 > (opts = fcntl(sock, F_GETFL))) printf(“Error…\n”); // modifying and applying opts = (opts | O_NONBLOCK); if (fcntl(sock, F_SETFL, opts)) printf(“Error…\n”); bind(…); // getting current options if (0 > (opts = fcntl(sock, F_GETFL))) printf(“Error…\n”); // modifying and applying opts = (opts | O_NONBLOCK); if (fcntl(sock, F_SETFL, opts)) printf(“Error…\n”); bind(…);

An easy model.

struct sockaddr_in saddr, caddr; int sockfd, clen, isock; unsigned short port = 80; if (0 > (sockfd=socket(AF_INET, SOCK_STREAM, 0))) printf(“Error creating socket\n”); memset(&saddr, '\0', sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(INADDR_ANY); saddr.sin_port = htons(port); if (0 > (bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr))) printf(“Error binding\n”); if (listen(sockfd, 5) < 0) { // listen for incoming connections printf(“Error listening\n”); clen = sizeof(caddr) struct sockaddr_in saddr, caddr; int sockfd, clen, isock; unsigned short port = 80; if (0 > (sockfd=socket(AF_INET, SOCK_STREAM, 0))) printf(“Error creating socket\n”); memset(&saddr, '\0', sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(INADDR_ANY); saddr.sin_port = htons(port); if (0 > (bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr))) printf(“Error binding\n”); if (listen(sockfd, 5) < 0) { // listen for incoming connections printf(“Error listening\n”); clen = sizeof(caddr)

// Setup your read_set with FD_ZERO and the server socket descriptor while (1) { pool.ready_set = &pool.read_set; pool.nready = select(pool.maxfd+1, &pool.ready_set, &pool.write_set, NULL, NULL); if (FD_ISSET(sockfd, &pool.ready_set)) { if (0 > (isock = accept(sockfd, (struct sockaddr *) &caddr, &clen))) printf(“Error accepting\n”); add_client(isock, &caddr, &pool); } check_clients(&pool); } // close it up down here // Setup your read_set with FD_ZERO and the server socket descriptor while (1) { pool.ready_set = &pool.read_set; pool.nready = select(pool.maxfd+1, &pool.ready_set, &pool.write_set, NULL, NULL); if (FD_ISSET(sockfd, &pool.ready_set)) { if (0 > (isock = accept(sockfd, (struct sockaddr *) &caddr, &clen))) printf(“Error accepting\n”); add_client(isock, &caddr, &pool); } check_clients(&pool); } // close it up down here

Your suggestions?

Architecture

A struct something like this: typedef struct s_pool { int maxfd; // largest descriptor in sets fd_set read_set; // all active read descriptors fd_set write_set; // all active write descriptors fd_set ready_set;// descriptors ready for reading int nready;// return of select() int clientfd[FD_SETSIZE];// max index in client array // might want to write this read_buf client_read_buf[FD_SETSIZE]; // what else might be helpful for project 1? } pool; typedef struct s_pool { int maxfd; // largest descriptor in sets fd_set read_set; // all active read descriptors fd_set write_set; // all active write descriptors fd_set ready_set;// descriptors ready for reading int nready;// return of select() int clientfd[FD_SETSIZE];// max index in client array // might want to write this read_buf client_read_buf[FD_SETSIZE]; // what else might be helpful for project 1? } pool;

Basic Commands  NICK  USER  QUIT Channel Commands  JOIN  PART  LIST Advanced Commands  PRIVMSG  WHO

Server Client(s) socket() connect() write() read() close() socket() bind() listen() select() write() read() close() read() EOF Connection Request Client / Server Session(s) FD_ISSET(sfd) accept() check_clients() main loop

Strong I/O skills will be a great assistant. Read more about this field if interested  Books as ‘UNIX Network Programming – The Sockets Networking API’ will be a good tutorial

Deadline: Checkpoint: Detailed information will be put on the FTP: ftp:// /classes/08/102 计算机网络 /PROJECT/project 1