CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail.

Slides:



Advertisements
Similar presentations
Review: Name/Address conversion: –What does the domain name system do? –Which system calls use the domain name system? –How does it work? –How does getservbyname.
Advertisements

Socket Programming 101 Vivek Ramachandran.
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Nonblocking I/O Blocking vs. non-blocking I/O
Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
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.
Today’s topic Issues about sending structures with TCP. Server design alternatives: concurrent server and multiplexed server. I/O multiplexing.
Generic Transport Service Primitives Listen –notify Transport layer a call is expected Connect –establish Transport layer connection Send (or Write) Receive.
TDC561 Network Programming Camelia Zlatea, PhD Week 3: Unix Asynchronous Events; Signals and Alarms API.
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Threads CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
CS Lecture 17 Outline Named pipes Signals Lecture 17
04/16/2010CSCI 315 Operating Systems Design1 I/O Systems Notice: The slides for this lecture have been largely based on those accompanying an earlier edition.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
1 Nonblocking I/O Nonblocking reads and writes Buffers enabling overlapped nonblocking I/O Nonblocking connect.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Non-blocking I/O int flags; int fd; /* file descripter */ void main() { fd = open(“myfile.txt”, R_ONLY); if ((flags = fcntl(fd, F_GETFL, 0)) < 0) /* first.
Lecture 8 UDP Sockets & I/O Multiplexing
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*
An adapted reference model for networked communication.
Communication. Asynchronous RPC (1) a)The interconnection between client and server in a traditional RPC b)The interaction using asynchronous RPC 2-12.
IP Multiplexing Ying Zhang EECS 489 W07.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
ECE 4110 – Internetwork Programming Client-Server Model.
Signal-Driven I/O Concepts and steps for using signal-driven I/O
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.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
 Wind River Systems, Inc Chapter - 13 Network Programming.
More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c.
Nonblocking I/O Blocking vs. non-blocking I/O Nonblocking input, output, accept, and connect Readings –UNP Ch16 1.
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.
Chapter 2 Applications and Layered Architectures Sockets.
Network Programming Eddie Aronovich mail:
3.1 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8 th Edition Chapter 3: Processes.
TELE 402 Lecture 10: Unix domain … 1 Overview Last Lecture –Daemon processes and advanced I/O functions This Lecture –Unix domain protocols and non-blocking.
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.
I/O Multiplexing. TCP Echo Client: I/O operation is sequential !! tcpcliserv/tcpcli01.c: lib/str_cli.c: TCP Client TCP Server stdin stdout fgets fputs.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Socket Programming Lab 1 1CS Computer Networks.
Project 2: Socket Programming. Overview Sockets Working with sockets Client-Server example Project 1 Hints.
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,
Distributed Computing Systems
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
PA3: Improving Performance with I/O Multiplexing Part 1-1: Nov. 7, Part 1-2: Nov. 10 Part 2-1: Nov. 17, Part 2-2: Nov.20.
Single Process, Concurrent, Connection-Oriented Servers (TCP) (Chapter 12)
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
CSCI 330 UNIX and Network Programming Unit XVI: TCP Server Programming.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
I/O Multiplexing Chap 6. I/O Models  Blocking I/O Model  Nonblocking I/O Model  I/O Multiplexing Model  Signal Driven I/O Model  Asynchronous I/O.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Ioctl Operations. ioctl Function Interface Configuration  Netstat, ifconfig command 에서 사용.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
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 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Socket Option.
CSCI 315 Operating Systems Design
Lecture 4 Socket Programming Issues
Lecture 11 Overview.
TCP Sockets Programming
Advanced UNIX programming
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:

CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail

Unit Overview TCP programming socket behavior blocking vs. non-blocking signal handler 2CSCI UNIX and Network Programming

Socket system calls 3 PrimitiveMeaning socketCreate a new communication endpoint bindAttach a local address to a socket listenAnnounce willingness to accept connections acceptBlock caller until a connection request arrives connectActively attempt to establish a connection writeSend(write) some data over the connection readReceive(read) some data over the connection closeRelease the connection server client CSCI UNIX and Network Programming

TCP socket programming sockets can operate in one of two modes: blocking or non-blocking default: blocking mode calls may block, i.e. wait for something before they return blocking is a special state in which a process is waiting for I/O process is removed from the scheduler queue when the I/O completes, the process is woken up non-blocking mode calls returns immediately, producing full, partial or no result 4CSCI UNIX and Network Programming

Blocking mode: calls that can block accept blocks until a connection is present connect blocks until connection is established read blocks if no data is available write blocks when data does not fit into send buffer of the socket 5CSCI UNIX and Network Programming

Illustration: blocking mode // loop to write a single character char c = 'x'; while (true) { if (write(sock, &c, 1) < 1) { perror("write"); exit(EXIT_FAILURE); } 6CSCI UNIX and Network Programming

Illustration: dummy server 7CSCI UNIX and Network Programming

Illustration: blocking mode 8CSCI UNIX and Network Programming

What to do when call blocks ? Answer:wait or: set alarm i.e. schedule signal to sent in future how: set alarm timeout program and install signal handler 9CSCI UNIX and Network Programming

System call: alarm 10CSCI UNIX and Network Programming

System call: alarm unsigned int alarm(unsigned int seconds) arranges for a SIGALRM signal to be delivered to the calling process in seconds seconds any previously set alarm is canceled if seconds is zero, no new alarm is scheduled returns the number of seconds remaining until any previously scheduled alarm was due to be delivered returns zero if there was no previously scheduled alarm 11CSCI UNIX and Network Programming

Illustration: blocking mode 12CSCI UNIX and Network Programming

System call: signal 13CSCI UNIX and Network Programming

System call: signal typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler) sets the disposition of the signal signum to handler handler can be: SIG_IGN to ignore signal SIG_DFL to restore default address of a programmer-defined function returns the previous value of the signal handler 14CSCI UNIX and Network Programming

Signal handler: detail void alarm_action(int s) { cerr << "write blocked after " << count << " characters\n"; exit(EXIT_SUCCESS); } // set signal handler on ALARM signal(SIGALRM, alarm_action); 15CSCI UNIX and Network Programming

Illustration: blocking mode w/signal 16CSCI UNIX and Network Programming

TCP socket: non-blocking mode calls returns immediately, producing full, partial or no result socket can be in blocking or non-blocking mode determined by flag associated with socket descriptor flag can be set via fcntl system call 17CSCI UNIX and Network Programming

Non-Blocking mode: call behavior accept when there are connections to accept, accept behaves as usual if there are no pending connections, returns −1 with errno set to EWOULDBLOCK connect when connection to a listening server can be established, connect behaves as usual if connection cannot be established, connect returns −1 with errno set to EINPROGRESS 18CSCI UNIX and Network Programming

Non-Blocking mode: call behavior read when there is data to read, read behaves as usual if there is no data, read returns −1 with errno EWOULDBLOCK write when buffer space is available, write behaves as usual if buffer becomes full, write returns number of bytes written if buffer is full, write returns -1 with errno EWOULDBLOCK 19CSCI UNIX and Network Programming

System call: fcntl 20CSCI UNIX and Network Programming

System call: fcntl int fcntl(int fd, int cmd,... /* arg */ ) performs operation cmd on the open descriptor fd cmd to set non-blocking mode for socket: F_SETFL, O_NONBLOCK returns -1 on error 21CSCI UNIX and Network Programming

Illustration: non-blocking write 22CSCI UNIX and Network Programming

Summary TCP programming socket behavior blocking vs. non-blocking signal handler 23CSCI UNIX and Network Programming