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