160 likes | 241 Views
IEG4180 Tutorial 3. Shaoquan Zhang. Announcement. The deadline of the project 1 is extended to 11pm, 12/02/2011 Follow the requirement of the marking scheme. Outline. Stream socket programming I/O modes in Unix. Stream Socket Programming. Stream socket.
E N D
IEG4180 Tutorial 3 Shaoquan Zhang
Announcement • The deadline of the project 1 is extended to 11pm, 12/02/2011 • Follow the requirement of the marking scheme
Outline • Stream socket programming • I/O modes in Unix
Stream Socket Programming • Stream socket Q: When is the three-way handshake happened? A: When the client calls the function connect(), the TCP connection is built before the accept(). The function accept() selects one from the established connection queue.
TCP Connection Backlog • TCP Connection Queue • For a given listening socket, the kernel maintains two queues for client sockets: An incomplete connection queue: three-way handshake is not completed yet; A completed connection queue: three-way handshake is completed. • listen(socket s, int backlog) backlog: the maximum of the sum of two queue lengths • Problem: SYN attack
Accept() • By default, when the completed queue is empty, the function will be blocked. • Methods: • poll() : accept() becomes non-blocking • select(): accept() is called only when the completed queue is not empty • Multithread • Message driven
Message-driven Accept() SOCKET s=socket(); bind(); WSAAsyncSelect(s, m_hWnd, wMsg, FD_ACCEPT); //turn the socket s into a message-driven socket //m_hWnd: handle to window that will process the message //wMsg: message identifier; FD_ACCEPT: events to handle listen(s, 5);
WindowProc() HRESULT CNetProbeDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { if(message==wMsg) { SOCKET s = (SOCKET)wParam; int event = WSAGETSELECTEVENT(lParam); switch(event){ case FD_ACCEPT: OnAccept(s); break; case FD_READ: OnRead(s); //receive data break; } } return CDialog::WindowProc(message, wParam, lParam); } message events to handle void CNetProbeDlg::OnAccept(SOCKET s) { SOCKET newsfd = accept(s,…); WSAAsyncSelect(newsfd, m_hWnd, wMsg, FD_READ); //turn the new socket into a message-driven one; handle the recv() }
Add WindowProc() class review -> dialog class property overrides
Unix I/O Modes • Blocking I/O • Non-blocking I/O • I/O multiplexing (select and poll) • Asynchronous I/O For a socket input operation, there are two steps: 1. wait data to arrive 2. copy data from kernel’s buffer to the process
Blocking I/O Acknowledgement: following pictures come from UNIX Networking Programming
Backup Host A Host B SYN, seq=n SYN, seq=m ACK, seq=n+1 ACK, seq=m+1