Program Statement
Demonstrate FTP's two-connection architecture: a control connection on port 21 carrying commands/replies, plus a separate data connection negotiated via PASV.
/* mini_ftp.c — core fragment: control conn + PASV data conn */
send_cmd(ctrl, "USER anonymous\r\n"); read_reply(ctrl); /* 331 */
send_cmd(ctrl, "PASS guest@\r\n"); read_reply(ctrl); /* 230 */
send_cmd(ctrl, "PASV\r\n");
/* reply: 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2) */
sscanf(reply, "%*[^(](%d,%d,%d,%d,%d,%d)", &h1,&h2,&h3,&h4,&p1,&p2);
int port = p1 * 256 + p2; /* data port */
int data = tcp_connect_ip(h1,h2,h3,h4, port);
send_cmd(ctrl, "LIST\r\n"); read_reply(ctrl); /* 150 */
while ((n = read(data, buf, sizeof(buf))) > 0) write(1, buf, n);
close(data); read_reply(ctrl); /* 226 */
send_cmd(ctrl, "QUIT\r\n");
Record Task
Draw the two-connection diagram; explain active vs passive mode and why NAT killed active mode (the server cannot connect in to a client behind NAT, so the client must open the data connection out — passive).
Theory Link
Unit 1 — one application, multiple simultaneous connections.