Socket Programming and I/O Multiplexing in CPE 401/601, Slides of Computer Networks

Various issues in socket programming and i/o multiplexing in the context of cpe 401/601. Topics include timeout handling, connected udp mode, i/o multiplexing, and client/server programming. The document also discusses different approaches to handling multiple descriptors and monitoring multiple input sources.

Typology: Slides

2012/2013

Uploaded on 04/25/2013

avanti
avanti 🇮🇳

4.4

(11)

112 documents

1 / 50

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 4
Socket Programming Issues
slides are modified from Dave Hollinger
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32

Partial preview of the text

Download Socket Programming and I/O Multiplexing in CPE 401/601 and more Slides Computer Networks in PDF only on Docsity!

Lecture 4

Socket Programming Issues

slides are modified from Dave Hollinger Docsity.com

Debugging

  • Debugging can be difficult
  • Write routines to print out sockaddrs
  • Use trace, strace, ptrace, truss, etc
  • Include code that can handle unexpected situations

CPE 401/601 Lecture 4 : SocketProgramming Issues (^) Docsity.com 2

UDP Connected mode

  • A UDP socket can be used in a call to connect()
  • This simply tells the O.S. the address of the peer
  • No handshake is made to establish that the peer exists
  • No data of any kind is sent on the network as a result of calling connect() on a UDP socket CPE 401/601 Lecture 4 : SocketProgramming Issues (^) Docsity.com 4

Connected UDP

  • Once a UDP socket is connected:
    • can use sendto() with a null dest address
    • can use write() and send()
    • can use read() and recv()
      • only datagrams from the peer will be returned
    • Asynchronous errors will be returned to the process

CPE 401/601 Lecture 4 : SocketProgramming Issues 5

OS Specific, some won’t do this!

Docsity.com

Back to UDP connect()

  • Connect() is typically used with UDP when communication is with a single peer only
  • It is possible to disconnect and connect the same socket to a new peer - More efficient to send multiple datagrams to the same user
  • Many UDP clients use connect()
  • Some servers (TFTP)

CPE 401/601 Lecture 4 : SocketProgramming Issues (^) Docsity.com 7

I/O MULTIPLEXING

Docsity.com

Example - generic TCP client

  • Input from standard input should be sent to a TCP socket
  • Input from a TCP socket should be sent to standard output
  • How do we know when to check for input from each source?

CPE 401/601 Lecture 4 : I/O Multiplexing 10

STDIN

TCP SOCKET^ STDOUT Docsity.com

Options

  • Use multiple processes/threads
  • Use nonblocking I/O
    • use fcntl() to set O_NONBLOCK
  • Use alarm and signal handler to interrupt slow system calls
  • Use functions that support checking of multiple input sources at the same time CPE 401/601 Lecture 4 : I/O Multiplexing (^) Docsity.com 11

Non blocking I/O

while (! done) { if ( (n=read(STDIN_FILENO,…)<0)) if (errno != EWOULDBLOCK) / ERROR / else write(tcpsock,…)

if ( (n=read(tcpsock,…)<0)) if (errno != EWOULDBLOCK) / ERROR / else write(STDOUT_FILENO,…) }

CPE 401/601 Lecture 4 : I/O Multiplexing (^) Docsity.com 13

The problem with nonblocking I/O

  • Using blocking I/O allows the OS to put your process to sleep when nothing is happening - Once input arrives, the OS will wake up your process and read() (or whatever) will return
  • With nonblocking I/O, the process will chew up all available processor time!!!

CPE 401/601 Lecture 4 : I/O Multiplexing (^) Docsity.com 14

“Alarming” Issues

  • What will happen to the response time?
  • What is the ‘right’ value for MAX_TIME?

CPE 401/601 Lecture 4 : I/O Multiplexing (^) Docsity.com 16

Select()

  • The select() system call allows us to use blocking I/O on a set of descriptors - file, socket, …
  • We can ask select to notify us when data is available for reading on either STDIN or a socket

CPE 401/601 Lecture 4 : I/O Multiplexing (^) Docsity.com 17

struct timeval

struct timeval { long tv_sec; / seconds / long tv_usec; / microseconds / } struct timeval max = {1,0};

  • To return immediately after checking descriptors - set timeout as {0, 0}
  • To wait until I/O is ready
    • set timeout as a NULL pointer CPE 401/601 Lecture 4 : I/O Multiplexing (^) Docsity.com 19

fd_set

  • Operations you can use with an fd_set:
    • Clear all bits in fd_set *void FD_ZERO(fd_set fdset);
    • Turn on the bit for fd in fd_set *void FD_SET(int fd, fd_set fdset);
    • Turn off the bit for fd in fd_set *void FD_CLR(int fd, fd_set fdset);
    • Check whether the bit for fd in fd_set is on *int FD_ISSET(int fd, fd_set fdset);

CPE 401/601 Lecture 4 : I/O Multiplexing (^) Docsity.com 20