<div class="xblock xblock-public_view xblock-public_view-vertical" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@vertical+block@Reading_7_Objectives" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="vertical" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<h2 class="hd hd-2 unit-title">Reading 7 Objectives</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_3d3b925cd14c">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_3d3b925cd14c" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="html" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/b2188f0a95a7a9062748278f7d5a584f/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/syntax-highlighting.css" rel="stylesheet" type="text/css" />
<h4>Software in 6.005</h4>
<table class="table table-striped no-markdown">
<tbody>
<tr>
<th width="33%">Safe from bugs</th>
<th>Easy to understand</th>
<th>Ready for change</th>
</tr>
<tr>
<td>
Correct today and correct in the unknown future.
</td>
<td>
Communicating clearly with future programmers, including future you.
</td>
<td>
Designed to accommodate change without rewriting.
</td>
</tr>
</tbody>
</table>
<h4>Objectives</h4>
<p>In this reading we examine <em>client/server communication</em> over the network using the <em>socket</em> abstraction.</p>
<p>Network communication is inherently concurrent, so building clients and servers will require us to reason about their concurrent behavior and to implement them with thread safety. We must also design the <em>wire protocol</em> that clients and servers use to communicate, just as we design the operations that clients of an ADT use to work with it.</p>
<p>Some of the operations with sockets are <em>blocking</em>: they block the progress of a thread until they can return a result. Blocking makes writing some code easier, but it also foreshadows a new class of concurrency bugs we'll soon contend with in depth: deadlocks.</p>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@vertical+block@vertical-client-server" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="vertical" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<h2 class="hd hd-2 unit-title">Client/server design pattern</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_121b614a64e7">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_121b614a64e7" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="html" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/b2188f0a95a7a9062748278f7d5a584f/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/syntax-highlighting.css" rel="stylesheet" type="text/css" />
<h2 id="clientserver_design_pattern">Client/server design pattern</h2>
<div data-outline="clientserver_design_pattern">
<p>In this reading (and in the problem set) we explore the <strong>client/server design pattern</strong> for communication with message passing.</p>
<p>In this pattern there are two kinds of processes: clients and servers. A client initiates the communication by connecting to a server. The client sends requests to the server, and the server sends replies back. Finally, the client disconnects. A server might handle connections from many clients concurrently, and clients might also connect to multiple servers.</p>
<p>Many Internet applications work this way: web browsers are clients for web servers, an email program like Outlook is a client for a mail server, etc.</p>
<p>On the Internet, client and server processes are often running on different machines, connected only by the network, but it doesn't have to be that way — the server can be a process running on the same machine as the client.</p>
</div>
<h2 id="network_sockets">Network sockets</h2>
<div data-outline="network_sockets">
<h3 id="ip_addresses">IP addresses</h3>
<div data-outline="ip_addresses">
<p>A network interface is identified by an <a href="http://en.wikipedia.org/wiki/IP_address">IP address</a>. IPv4 addresses are 32-bit numbers written in four 8-bit parts. For example (as of this writing):</p>
<ul>
<li>
<p><code>18.9.22.69</code> is the IP address of a MIT web server. Every address whose <a href="http://en.wikipedia.org/wiki/List_of_assigned_/8_IPv4_address_blocks">first octet is <code>18</code></a> is on the MIT network.</p>
</li>
<li>
<p><code>18.9.25.15</code> is the address of a MIT incoming email handler.</p>
</li>
<li>
<p><code>173.194.123.40</code> is the address of a Google web server.</p>
</li>
<li>
<p><code>127.0.0.1</code> is the <a href="http://en.wikipedia.org/wiki/Loopback">loopback</a> or <a href="http://en.wikipedia.org/wiki/Localhost">localhost</a> address: it always refers to the local machine. Technically, any address whose first octet is <code>127</code> is a loopback address, but <code>127.0.0.1</code> is standard.</p>
</li>
</ul>
<p>You can <a href="https://www.google.com/search?q=my+ip">ask Google for your current IP address</a>. In general, as you carry around your laptop, every time you connect your machine to the network it can be assigned a new IP address.</p>
</div>
<h3 id="hostnames">Hostnames</h3>
<div data-outline="hostnames">
<p><a href="http://en.wikipedia.org/wiki/Hostname">Hostnames</a> are names that can be translated into IP addresses. A single hostname can map to different IP addresses at different times; and multiple hostnames can map to the same IP address. For example:</p>
<ul>
<li>
<p><code>web.mit.edu</code> is the name for MIT's web server. You can translate this name to an IP address yourself using <code>dig</code>, <code>host</code>, or <code>nslookup</code> on the command line, e.g.:</p>
<pre class="no-markdown">$ <b>dig +short web.mit.edu</b>
18.9.22.69
</pre></li>
<li>
<p><code>dmz-mailsec-scanner-4.mit.edu</code> is the name for one of MIT's spam filter machines responsible for handling incoming email.</p>
</li>
<li>
<p><code>google.com</code> is exactly what you think it is. Try using one of the commands above to find <code>google.com</code>'s IP address. What do you see?</p>
</li>
<li>
<p><code>localhost</code> is a name for <code>127.0.0.1</code>. When you want to talk to a server running on your own machine, talk to <code>localhost</code>.</p>
</li>
</ul>
<p>Translation from hostnames to IP addresses is the job of the <a href="http://en.wikipedia.org/wiki/Domain_Name_System">Domain Name System (DNS)</a>. It's super cool, but not part of our discussion today.</p>
</div>
<h3 id="port_numbers">Port numbers</h3>
<div data-outline="port_numbers">
<p>A single machine might have multiple server applications that clients wish to connect to, so we need a way to direct traffic on the same network interface to different processes.</p>
<p>Network interfaces have multiple <a href="http://en.wikipedia.org/wiki/Port_(computer_networking)">ports</a> identified by a 16-bit number from 0 (which is reserved, so we effectively start at 1) to 65535.</p>
<p>A server process binds to a particular port — it is now <strong>listening</strong> on that port. Clients have to know which port number the server is listening on. There are some <a href="http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Well-known_ports">well-known ports</a> which are reserved for system-level processes and provide standard ports for certain services. For example:</p>
<ul>
<li>Port 22 is the standard SSH port. When you connect to <code>athena.dialup.mit.edu</code> using SSH, the software automatically uses port 22.</li>
<li>Port 25 is the standard email server port.</li>
<li>Port 80 is the standard web server port. When you connect to the URL <code>http://web.mit.edu</code> in your web browser, it connects to <code>18.9.22.69</code> on port 80.</li>
</ul>
<p>When the port is not a standard port, it is specified as part of the address. For example, the URL <code>http://128.2.39.10:9000</code> refers to port 9000 on the machine at <code>128.2.39.10</code>.</p>
<p>When a client connects to a server, that outgoing connection also uses a port number on the client's network interface, usually chosen at random from the available <em>non</em>-well-known ports.</p>
</div>
<h3 id="network_sockets_2">Network sockets</h3>
<div data-outline="network_sockets_2">
<p>A <a href="http://en.wikipedia.org/wiki/Network_socket"><strong>socket</strong></a> represents one end of the connection between client and server.</p>
<ul>
<li>
<p>A <strong>listening socket</strong> is used by a server process to wait for connections from remote clients.</p>
<p>In Java, use <a href="http://docs.oracle.com/javase/8/docs/api/?java/net/ServerSocket.html"><code>ServerSocket</code></a> to make a listening socket, and use its <a href="http://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html#accept--"><code>accept</code></a> method to listen to it.</p>
</li>
<li>
<p>A <strong>connected socket</strong> can send and receive messages to and from the process on the other end of the connection. It is identified by both the local IP address and port number plus the remote address and port, which allows a server to differentiate between concurrent connections from different IPs, or from the same IP on different remote ports.</p>
<p>In Java, clients use a <a href="http://docs.oracle.com/javase/8/docs/api/?java/net/Socket.html"><code>Socket</code></a> constructor to establish a socket connection to a server. Servers obtain a connected socket as a <code>Socket</code> object returned from <code>ServerSocket.accept</code>.</p>
</li>
</ul>
</div>
</div>
<h2 id="io">I/O</h2>
<div data-outline="io">
<h3 id="buffers">Buffers</h3>
<div data-outline="buffers">
<p>The data that clients and servers exchange over the network is sent in chunks. These are rarely just byte-sized chunks, although they might be. The sending side (the client sending a request or the server sending a response) typically writes a large chunk (maybe a whole string like “HELLO, WORLD!” or maybe 20 megabytes of video data). The network chops that chunk up into packets, and each packet is routed separately over the network. At the other end, the receiver reassembles the packets together into a stream of bytes.</p>
<p>The result is a bursty kind of data transmission — the data may already be there when you want to read them, or you may have to wait for them to arrive and be reassembled.</p>
<p>When data arrive, they go into a <strong>buffer</strong>, an array in memory that holds the data until you read it.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@vertical+block@vertical_Questions_2da285ae4674" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="vertical" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@Client_server_socket_buffer">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@Client_server_socket_buffer" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="html" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/b2188f0a95a7a9062748278f7d5a584f/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/syntax-highlighting.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/c9cee8830885f59e75b8782f44026226/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-client-server-network-stuffer">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-client-server-network-stuffer" data-graded="True" data-has-score="True" data-runtime-version="1" data-block-type="problem" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_07-client-server-network-stuffer" class="problems-wrapper" role="group"
aria-labelledby="07-client-server-network-stuffer-problem-title"
data-problem-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-client-server-network-stuffer" data-url="/courses/course-v1:MITx+6.005.2x+1T2017/xblock/block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-client-server-network-stuffer/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="3"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="07-client-server-network-stuffer-problem-title" aria-describedby="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-client-server-network-stuffer-problem-progress" tabindex="-1">
Client server network stuffer
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-client-server-network-stuffer-problem-progress"></div>
<div class="problem">
<div>
<p>You're developing a new web server program on your own laptop. You start the server running on port 8080.</p>
<p>Fill in the blanks for the URL you should visit in your web browser to talk to your server:</p>
<pre>
__A__://__B__:__C__
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="inputtype option-input ">
<label class="problem-group-label" for="input_07-client-server-network-stuffer_2_1" id="label_07-client-server-network-stuffer_2_1">__A__</label>
<select name="input_07-client-server-network-stuffer_2_1" id="input_07-client-server-network-stuffer_2_1" aria-describedby="status_07-client-server-network-stuffer_2_1">
<option value="option_07-client-server-network-stuffer_2_1_dummy_default">Select an option</option>
<option value="80"> 80</option>
<option value="8080"> 8080</option>
<option value="http"> http</option>
<option value="localhost"> localhost</option>
<option value="loopback"> loopback</option>
<option value="web-server"> web-server</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_07-client-server-network-stuffer_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_07-client-server-network-stuffer_2_1"/>
</div></div>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="inputtype option-input ">
<label class="problem-group-label" for="input_07-client-server-network-stuffer_3_1" id="label_07-client-server-network-stuffer_3_1">__B__</label>
<select name="input_07-client-server-network-stuffer_3_1" id="input_07-client-server-network-stuffer_3_1" aria-describedby="status_07-client-server-network-stuffer_3_1">
<option value="option_07-client-server-network-stuffer_3_1_dummy_default">Select an option</option>
<option value="80"> 80</option>
<option value="8080"> 8080</option>
<option value="http"> http</option>
<option value="localhost"> localhost</option>
<option value="loopback"> loopback</option>
<option value="web-server"> web-server</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_07-client-server-network-stuffer_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_07-client-server-network-stuffer_3_1"/>
</div></div>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="inputtype option-input ">
<label class="problem-group-label" for="input_07-client-server-network-stuffer_4_1" id="label_07-client-server-network-stuffer_4_1">__C__</label>
<select name="input_07-client-server-network-stuffer_4_1" id="input_07-client-server-network-stuffer_4_1" aria-describedby="status_07-client-server-network-stuffer_4_1">
<option value="option_07-client-server-network-stuffer_4_1_dummy_default">Select an option</option>
<option value="80"> 80</option>
<option value="8080"> 8080</option>
<option value="http"> http</option>
<option value="localhost"> localhost</option>
<option value="loopback"> loopback</option>
<option value="web-server"> web-server</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_07-client-server-network-stuffer_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_07-client-server-network-stuffer_4_1"/>
</div></div>
<div class="solution-span">
<span id="solution_07-client-server-network-stuffer_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="Client server network stuffer" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_07-client-server-network-stuffer" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_07-client-server-network-stuffer">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-client-server-network-stuffer-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-client-server-network-stuffer-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-client-server-network-stuffer-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-address-hostname-socket-buffer">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-address-hostname-socket-buffer" data-graded="True" data-has-score="True" data-runtime-version="1" data-block-type="problem" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_07-address-hostname-socket-buffer" class="problems-wrapper" role="group"
aria-labelledby="07-address-hostname-socket-buffer-problem-title"
data-problem-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-address-hostname-socket-buffer" data-url="/courses/course-v1:MITx+6.005.2x+1T2017/xblock/block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-address-hostname-socket-buffer/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="07-address-hostname-socket-buffer-problem-title" aria-describedby="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-address-hostname-socket-buffer-problem-progress" tabindex="-1">
Address hostname socket buffer
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-address-hostname-socket-buffer-problem-progress"></div>
<div class="problem">
<div>
<p>A connected socket is identified by:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_07-address-hostname-socket-buffer_2_1">
<fieldset aria-describedby="status_07-address-hostname-socket-buffer_2_1">
<div class="field">
<input type="checkbox" name="input_07-address-hostname-socket-buffer_2_1[]" id="input_07-address-hostname-socket-buffer_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="07-address-hostname-socket-buffer_2_1-choice_0-label" for="input_07-address-hostname-socket-buffer_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_07-address-hostname-socket-buffer_2_1"> local IP address
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-address-hostname-socket-buffer_2_1[]" id="input_07-address-hostname-socket-buffer_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="07-address-hostname-socket-buffer_2_1-choice_1-label" for="input_07-address-hostname-socket-buffer_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_07-address-hostname-socket-buffer_2_1"> remote IP address
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-address-hostname-socket-buffer_2_1[]" id="input_07-address-hostname-socket-buffer_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="07-address-hostname-socket-buffer_2_1-choice_2-label" for="input_07-address-hostname-socket-buffer_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_07-address-hostname-socket-buffer_2_1"> local hostname
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-address-hostname-socket-buffer_2_1[]" id="input_07-address-hostname-socket-buffer_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="07-address-hostname-socket-buffer_2_1-choice_3-label" for="input_07-address-hostname-socket-buffer_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_07-address-hostname-socket-buffer_2_1"> remote hostname
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-address-hostname-socket-buffer_2_1[]" id="input_07-address-hostname-socket-buffer_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="07-address-hostname-socket-buffer_2_1-choice_4-label" for="input_07-address-hostname-socket-buffer_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_07-address-hostname-socket-buffer_2_1"> local port number
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-address-hostname-socket-buffer_2_1[]" id="input_07-address-hostname-socket-buffer_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="07-address-hostname-socket-buffer_2_1-choice_5-label" for="input_07-address-hostname-socket-buffer_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_07-address-hostname-socket-buffer_2_1"> remote port number
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-address-hostname-socket-buffer_2_1[]" id="input_07-address-hostname-socket-buffer_2_1_choice_6" class="field-input input-checkbox" value="choice_6"/><label id="07-address-hostname-socket-buffer_2_1-choice_6-label" for="input_07-address-hostname-socket-buffer_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_07-address-hostname-socket-buffer_2_1"> local buffer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-address-hostname-socket-buffer_2_1[]" id="input_07-address-hostname-socket-buffer_2_1_choice_7" class="field-input input-checkbox" value="choice_7"/><label id="07-address-hostname-socket-buffer_2_1-choice_7-label" for="input_07-address-hostname-socket-buffer_2_1_choice_7" class="response-label field-label label-inline" aria-describedby="status_07-address-hostname-socket-buffer_2_1"> remote buffer
</label>
</div>
<span id="answer_07-address-hostname-socket-buffer_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_07-address-hostname-socket-buffer_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_07-address-hostname-socket-buffer_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="Address hostname socket buffer" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_07-address-hostname-socket-buffer" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_07-address-hostname-socket-buffer">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-address-hostname-socket-buffer-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-address-hostname-socket-buffer-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-address-hostname-socket-buffer-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@vertical+block@Streams_Blocking" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="vertical" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<h2 class="hd hd-2 unit-title">Streams & Blocking</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_e540c506656c">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_e540c506656c" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="html" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/b2188f0a95a7a9062748278f7d5a584f/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/syntax-highlighting.css" rel="stylesheet" type="text/css" />
<h3 id="streams">Streams</h3>
<div data-outline="streams">
<p>The data going into or coming out of a socket is a <a href="http://en.wikipedia.org/wiki/Stream_(computing)"><strong>stream</strong></a> of bytes.</p>
<p>In Java, <a href="http://docs.oracle.com/javase/8/docs/api/?java/io/InputStream.html"><code>InputStream</code></a> objects represent sources of data flowing into your program. For example:</p>
<ul>
<li>Reading from a file on disk with a <a href="http://docs.oracle.com/javase/8/docs/api/?java/io/FileInputStream.html"><code>FileInputStream</code></a></li>
<li>User input from <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#in"><code>System.in</code></a></li>
<li>Input from a network socket</li>
</ul>
<p><a href="http://docs.oracle.com/javase/8/docs/api/?java/io/OutputStream.html"><code>OutputStream</code></a> objects represent data sinks, places we can write data to. For example:</p>
<ul>
<li><a href="http://docs.oracle.com/javase/8/docs/api/?java/io/FileOutputStream.html"><code>FileOutputStream</code></a> for saving to files</li>
<li><a href="http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#out"><code>System.out</code></a> is a <a href="http://docs.oracle.com/javase/8/docs/api/?java/io/PrintStream.html"><code>PrintStream</code></a>, an <code>OutputStream</code> that prints readable representations of various types</li>
<li>Output to a network socket</li>
</ul>
<div class="handout-solo alert alert-warning">
<p>In the Java Tutorials, read:</p>
<ul>
<li><a href="http://docs.oracle.com/javase/tutorial/essential/io/streams.html" class="alert-link">I/O Streams</a> up to and including <em>I/O from the Command Line</em> (8 pages)</li>
</ul>
</div>
<p>With sockets, remember that the <em>output</em> of one process is the <em>input</em> of another process. If Alice and Bob have a socket connection, Alice has an output stream that flows to Bob's input stream, and <em>vice versa</em>.</p>
</div>
</div>
<h2 id="blocking">Blocking</h2>
<div data-outline="blocking">
<p><strong>Blocking</strong> means that a thread waits (without doing further work) until an event occurs. We can use this term to describe methods and method calls: if a method is a <strong>blocking method</strong>, then a call to that method can <strong>block</strong>, waiting until some event occurs before it returns to the caller.</p>
<p>Socket input/output streams exhibit blocking behavior:</p>
<ul>
<li>When an incoming socket's buffer is empty, calling <code>read</code> blocks until data are available.</li>
<li>When the destination socket's buffer is full, calling <code>write</code> blocks until space is available.</li>
</ul>
<p>Blocking is very convenient from a programmer's point of view, because the programmer can write code as if the <code>read</code> (or <code>write</code>) call will always work, no matter what the timing of data arrival. If data (or for <code>write</code>, space) is already available in the buffer, the call might return very quickly. But if the read or write can't succeed, the call <strong>blocks</strong>. The operating system takes care of the details of delaying that thread until <code>read</code> or <code>write</code> <em>can</em> succeed.</p>
<p>Blocking happens throughout concurrent programming, not just in <a href="http://en.wikipedia.org/wiki/Input/output">I/O</a> (communication into and out of a process, perhaps over a network, or to/from a file, or with the user on the command line or a GUI, …). Concurrent modules don't work in lockstep, like sequential programs do, so they typically have to wait for each other to catch up when coordinated action is required.</p>
<p>We'll see in the next reading that this waiting gives rise to the second major kind of bug (the first was race conditions) in concurrent programming: <strong>deadlock</strong>, where modules are waiting for each other to do something, so none of them can make any progress. But that's for next time.</p>
</div>
<h2 id="using_network_sockets">Using network sockets</h2>
<div data-outline="using_network_sockets">
<p>Make sure you've read about <a href="#streams">streams</a> at the Java Tutorial link above, then read about network sockets:</p>
<ul>
<li><a href="http://docs.oracle.com/javase/tutorial/networking/sockets/index.html" class="alert-link">All About Sockets</a> (4 pages)</li>
</ul>
<p>This reading describes everything you need to know about creating server- and client-side sockets and writing to and reading from their I/O streams.</p>
<h4>On the second page</h4>
<p>The example uses a syntax we haven't seen: the <a href="http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html">try-with-resources</a> statement. This statement has the form:</p>
<div><pre><code class="language-java hljs"><span class="hljs-keyword">try</span> (
<span class="hljs-comment">// create new objects here that require cleanup after being used,</span>
<span class="hljs-comment">// and assign them to variables</span>
) {
<span class="hljs-comment">// code here runs with those variables</span>
<span class="hljs-comment">// cleanup happens automatically after the code completes</span>
} <span class="hljs-keyword">catch</span>(...) {
<span class="hljs-comment">// you can include catch clauses if the code might throw exceptions</span>
}</code></pre></div>
<h4>On the last page</h4>
<p>Notice how both <code>ServerSocket.accept()</code> and <code>in.readLine()</code> are <em>blocking</em>. This means that the server will need a <em>new thread</em> to handle I/O with each new client. While the client-specific thread is working with that client (perhaps blocked in a read or a write), another thread (perhaps the main thread) is blocked waiting to <code>accept</code> a new connection.</p>
<p>Unfortunately, their multithreaded Knock Knock Server implementation creates that new thread by <em>subclassing <code>Thread</code></em>. That's <em>not</em> the recommended strategy. Instead, create a new class that implements <code>Runnable</code>, or <a href="/courses/course-v1:MITx+6.005.2x+1T2017/jump_to_id/vertical-anonymous-class">use an anonymous <code>Runnable</code></a> that calls a method where that client connection will be handled until it's closed. Don't use <strike class="no-markdown"><code>extends Thread</code></strike>. And while subclassing was popular when the Java API was designed, we don't discuss or recommend it at all because it has many downsides.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@vertical+block@vertical_Questions_84a719c3d3aa" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="vertical" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@Network_sockets">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@Network_sockets" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="html" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/b2188f0a95a7a9062748278f7d5a584f/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/syntax-highlighting.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/c9cee8830885f59e75b8782f44026226/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-65">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-65" data-graded="True" data-has-score="True" data-runtime-version="1" data-block-type="problem" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_07-network-sockets-65" class="problems-wrapper" role="group"
aria-labelledby="07-network-sockets-65-problem-title"
data-problem-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-65" data-url="/courses/course-v1:MITx+6.005.2x+1T2017/xblock/block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-65/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="07-network-sockets-65-problem-title" aria-describedby="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-65-problem-progress" tabindex="-1">
Network sockets 1
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-65-problem-progress"></div>
<div class="problem">
<div>
<p>Alice has a connected socket with Bob. How does she send a message to Bob?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_07-network-sockets-65_2_1">
<fieldset aria-describedby="status_07-network-sockets-65_2_1">
<div class="field">
<input type="radio" name="input_07-network-sockets-65_2_1" id="input_07-network-sockets-65_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="07-network-sockets-65_2_1-choice_0-label" for="input_07-network-sockets-65_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_07-network-sockets-65_2_1"> write to her socket's input stream
</label>
</div>
<div class="field">
<input type="radio" name="input_07-network-sockets-65_2_1" id="input_07-network-sockets-65_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="07-network-sockets-65_2_1-choice_1-label" for="input_07-network-sockets-65_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_07-network-sockets-65_2_1"> write to her socket's output stream
</label>
</div>
<div class="field">
<input type="radio" name="input_07-network-sockets-65_2_1" id="input_07-network-sockets-65_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="07-network-sockets-65_2_1-choice_2-label" for="input_07-network-sockets-65_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_07-network-sockets-65_2_1"> write to Bob's socket's input stream
</label>
</div>
<div class="field">
<input type="radio" name="input_07-network-sockets-65_2_1" id="input_07-network-sockets-65_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="07-network-sockets-65_2_1-choice_3-label" for="input_07-network-sockets-65_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_07-network-sockets-65_2_1"> write to Bob's socket's output stream
</label>
</div>
<span id="answer_07-network-sockets-65_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_07-network-sockets-65_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_07-network-sockets-65_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="Network sockets 1" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_07-network-sockets-65" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_07-network-sockets-65">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-network-sockets-65-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-network-sockets-65-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-network-sockets-65-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-66">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-66" data-graded="True" data-has-score="True" data-runtime-version="1" data-block-type="problem" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_07-network-sockets-66" class="problems-wrapper" role="group"
aria-labelledby="07-network-sockets-66-problem-title"
data-problem-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-66" data-url="/courses/course-v1:MITx+6.005.2x+1T2017/xblock/block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-66/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="07-network-sockets-66-problem-title" aria-describedby="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-66-problem-progress" tabindex="-1">
Network sockets 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-network-sockets-66-problem-progress"></div>
<div class="problem">
<div>
<p>Which of these is it necessary for a client to know in order to connect to and communicate with a server?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_07-network-sockets-66_2_1">
<fieldset aria-describedby="status_07-network-sockets-66_2_1">
<div class="field">
<input type="checkbox" name="input_07-network-sockets-66_2_1[]" id="input_07-network-sockets-66_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="07-network-sockets-66_2_1-choice_0-label" for="input_07-network-sockets-66_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_07-network-sockets-66_2_1"> server IP address
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-network-sockets-66_2_1[]" id="input_07-network-sockets-66_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="07-network-sockets-66_2_1-choice_1-label" for="input_07-network-sockets-66_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_07-network-sockets-66_2_1"> server hostname
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-network-sockets-66_2_1[]" id="input_07-network-sockets-66_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="07-network-sockets-66_2_1-choice_2-label" for="input_07-network-sockets-66_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_07-network-sockets-66_2_1"> server port number
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-network-sockets-66_2_1[]" id="input_07-network-sockets-66_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="07-network-sockets-66_2_1-choice_3-label" for="input_07-network-sockets-66_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_07-network-sockets-66_2_1"> server process name
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-network-sockets-66_2_1[]" id="input_07-network-sockets-66_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="07-network-sockets-66_2_1-choice_4-label" for="input_07-network-sockets-66_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_07-network-sockets-66_2_1"> wire protocol
</label>
</div>
<span id="answer_07-network-sockets-66_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_07-network-sockets-66_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_07-network-sockets-66_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="Network sockets 2" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_07-network-sockets-66" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_07-network-sockets-66">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-network-sockets-66-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-network-sockets-66-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-network-sockets-66-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-3" data-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-echo-echo-echo-echo">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-echo-echo-echo-echo" data-graded="True" data-has-score="True" data-runtime-version="1" data-block-type="problem" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_07-echo-echo-echo-echo" class="problems-wrapper" role="group"
aria-labelledby="07-echo-echo-echo-echo-problem-title"
data-problem-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-echo-echo-echo-echo" data-url="/courses/course-v1:MITx+6.005.2x+1T2017/xblock/block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-echo-echo-echo-echo/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="07-echo-echo-echo-echo-problem-title" aria-describedby="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-echo-echo-echo-echo-problem-progress" tabindex="-1">
Echo echo echo echo
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-echo-echo-echo-echo-problem-progress"></div>
<div class="problem">
<div>
<p>In the <a href="http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/networking/sockets/examples/EchoClient.java">EchoClient</a> example, which of these might <em>block</em>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_07-echo-echo-echo-echo_2_1">
<fieldset aria-describedby="status_07-echo-echo-echo-echo_2_1">
<div class="field">
<input type="checkbox" name="input_07-echo-echo-echo-echo_2_1[]" id="input_07-echo-echo-echo-echo_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="07-echo-echo-echo-echo_2_1-choice_0-label" for="input_07-echo-echo-echo-echo_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_07-echo-echo-echo-echo_2_1"> echoSocket.getInputStream()
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-echo-echo-echo-echo_2_1[]" id="input_07-echo-echo-echo-echo_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="07-echo-echo-echo-echo_2_1-choice_1-label" for="input_07-echo-echo-echo-echo_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_07-echo-echo-echo-echo_2_1"> new BufferedReader(new InputStreamReader(...))
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-echo-echo-echo-echo_2_1[]" id="input_07-echo-echo-echo-echo_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="07-echo-echo-echo-echo_2_1-choice_2-label" for="input_07-echo-echo-echo-echo_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_07-echo-echo-echo-echo_2_1"> userInput = stdIn.readLine()
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-echo-echo-echo-echo_2_1[]" id="input_07-echo-echo-echo-echo_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="07-echo-echo-echo-echo_2_1-choice_3-label" for="input_07-echo-echo-echo-echo_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_07-echo-echo-echo-echo_2_1"> in.readLine()
</label>
</div>
<span id="answer_07-echo-echo-echo-echo_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_07-echo-echo-echo-echo_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p>And in <a href="http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/networking/sockets/examples/EchoServer.java">EchoServer</a>, which of these might <em>block</em>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_07-echo-echo-echo-echo_3_1">
<fieldset aria-describedby="status_07-echo-echo-echo-echo_3_1">
<div class="field">
<input type="checkbox" name="input_07-echo-echo-echo-echo_3_1[]" id="input_07-echo-echo-echo-echo_3_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="07-echo-echo-echo-echo_3_1-choice_0-label" for="input_07-echo-echo-echo-echo_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_07-echo-echo-echo-echo_3_1"> new ServerSocket(...)
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-echo-echo-echo-echo_3_1[]" id="input_07-echo-echo-echo-echo_3_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="07-echo-echo-echo-echo_3_1-choice_1-label" for="input_07-echo-echo-echo-echo_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_07-echo-echo-echo-echo_3_1"> Socket clientSocket = serverSocket.accept()
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-echo-echo-echo-echo_3_1[]" id="input_07-echo-echo-echo-echo_3_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="07-echo-echo-echo-echo_3_1-choice_2-label" for="input_07-echo-echo-echo-echo_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_07-echo-echo-echo-echo_3_1"> inputLine = in.readLine()
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-echo-echo-echo-echo_3_1[]" id="input_07-echo-echo-echo-echo_3_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="07-echo-echo-echo-echo_3_1-choice_3-label" for="input_07-echo-echo-echo-echo_3_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_07-echo-echo-echo-echo_3_1"> e.getMessage()
</label>
</div>
<span id="answer_07-echo-echo-echo-echo_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_07-echo-echo-echo-echo_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_07-echo-echo-echo-echo_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="Echo echo echo echo" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_07-echo-echo-echo-echo" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_07-echo-echo-echo-echo">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-echo-echo-echo-echo-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-echo-echo-echo-echo-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-echo-echo-echo-echo-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-4" data-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-block-block-block-block">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-block-block-block-block" data-graded="True" data-has-score="True" data-runtime-version="1" data-block-type="problem" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_07-block-block-block-block" class="problems-wrapper" role="group"
aria-labelledby="07-block-block-block-block-problem-title"
data-problem-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-block-block-block-block" data-url="/courses/course-v1:MITx+6.005.2x+1T2017/xblock/block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-block-block-block-block/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="07-block-block-block-block-problem-title" aria-describedby="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-block-block-block-block-problem-progress" tabindex="-1">
Block block block block
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-block-block-block-block-problem-progress"></div>
<div class="problem">
<div>
<p>Since <code>BufferedReader.readLine()</code> is a <em>blocking</em> method, which of these is true:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_07-block-block-block-block_2_1">
<fieldset aria-describedby="status_07-block-block-block-block_2_1">
<div class="field">
<input type="checkbox" name="input_07-block-block-block-block_2_1[]" id="input_07-block-block-block-block_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="07-block-block-block-block_2_1-choice_0-label" for="input_07-block-block-block-block_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_07-block-block-block-block_2_1"> When a thread calls <code>readLine</code>, all other threads <em>block</em> until <code>readLine</code> returns
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-block-block-block-block_2_1[]" id="input_07-block-block-block-block_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="07-block-block-block-block_2_1-choice_1-label" for="input_07-block-block-block-block_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_07-block-block-block-block_2_1"> When a thread calls <code>readLine</code>, that thread <em>blocks</em> until <code>readLine</code> returns
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-block-block-block-block_2_1[]" id="input_07-block-block-block-block_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="07-block-block-block-block_2_1-choice_2-label" for="input_07-block-block-block-block_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_07-block-block-block-block_2_1"> When a thread calls <code>readLine</code>, the call can be <em>blocked</em> and an exception is thrown
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-block-block-block-block_2_1[]" id="input_07-block-block-block-block_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="07-block-block-block-block_2_1-choice_3-label" for="input_07-block-block-block-block_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_07-block-block-block-block_2_1"> <code>BufferedReader</code> has its own thread for <code>readLine</code>, which runs a <em>block</em> of code passed in by the client
</label>
</div>
<span id="answer_07-block-block-block-block_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_07-block-block-block-block_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
</div>
<div class="action">
<input type="hidden" name="problem_id" value="Block block block block" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_07-block-block-block-block" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_07-block-block-block-block">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-block-block-block-block-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-block-block-block-block-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-block-block-block-block-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@vertical+block@Wire_protocols" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="vertical" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<h2 class="hd hd-2 unit-title">Wire protocols</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_18f3677ff356">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_18f3677ff356" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="html" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/b2188f0a95a7a9062748278f7d5a584f/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/syntax-highlighting.css" rel="stylesheet" type="text/css" />
<h2 id="wire_protocols">Wire protocols</h2>
<div data-outline="wire_protocols">
<p>Now that we have our client and server connected up with sockets, what do they pass back and forth over those sockets?</p>
<p>A <strong>protocol</strong> is a set of messages that can be exchanged by two communicating parties. A <strong>wire protocol</strong> in particular is a set of messages represented as byte sequences, like <code>hello world</code> and <code>bye</code> (assuming we've agreed on a way to encode those characters into bytes).</p>
<p>Many Internet applications use simple ASCII-based wire protocols. You can use a program called Telnet to check them out.
<h3 id="telnet_client">Telnet client</h3><div data-outline="telnet_client"><p><code>telnet</code> is a utility that allows you to make a direct network connection to a listening server and communicate with it via a terminal interface.
Linux and Mac OS X should have <code>telnet</code> installed by default.</p><p>Windows users should first check if telnet is installed by running the command <code>telnet</code> on the command line.</p><ul>
<li><p>If you do not have <code>telnet</code>, you can install it via Control Panel → Programs and Features → Turn Windows features on/off → Telnet client.
However, this version of <code>telnet</code> may be very hard to use.
If it does not show you what you’re typing, you will need to <a href="https://technet.microsoft.com/en-us/library/c.aspx">turn on the <code>localecho</code> option</a>.</p></li>
<li><p>A better alternative is PuTTY: <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html">download <code>putty.exe</code></a>.
To connect using PuTTY, enter a hostname and port, select Connection type: Raw, and Close window on exit: Never.
The last option will prevent the window from disappearing as soon as the server closes its end of the connection.</p></li>
</ul>
<p>Let’s look at some examples of wire protocols:</p>
<h3 id="http">HTTP</h3>
<div data-outline="http">
<p><a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol">Hypertext Transfer Protocol (HTTP)</a> is the language of the World Wide Web. We already know that port 80 is the well-known port for speaking HTTP to web servers, so let's talk to one on the command line.</p>
<p class="no-markdown">
You'll be using Telnet on the problem set, so try these out now. User input is shown in <b>green</b>, and for input to the telnet connection, newlines (pressing enter) are shown with <b>↵</b>:
</p><pre class="no-markdown">$ <b>telnet www.eecs.mit.edu 80</b>
Trying 18.62.0.96...
Connected to eecsweb.mit.edu.
Escape character is '^]'.
<b>GET /↵</b>
<!DOCTYPE html>
<i>... lots of output ...</i>
<title>Homepage | MIT EECS</title>
<i>... lots more output ...</i>
</pre>
<p>The <code>GET</code> command gets a web page. The <code>/</code> is the path of the page you want on the site. So this command fetches the page at <code>http://www.eecs.mit.edu:80/</code>. Since 80 is the default port for HTTP, this is equivalent to visiting <a href="http://www.eecs.mit.edu/">http://www.eecs.mit.edu/</a> in your web browser. The result is HTML code that your browser renders to display the EECS homepage.</p>
<p>Internet protocols are defined by <a href="http://en.wikipedia.org/wiki/Request_for_Comments">RFC specifications</a> (RFC stands for “request for comment”, and some RFCs are eventually adopted as standards).
<a href="http://tools.ietf.org/html/rfc1945">RFC 1945</a> defined HTTP version 1.0, and was superseded by HTTP 1.1 in <a href="http://tools.ietf.org/html/rfc2616">RFC 2616</a>. So for many web sites, you might need to speak HTTP 1.1 if you want to talk to them. For example:</p><pre class="no-markdown">$ <b>telnet web.mit.edu 80</b>
Trying 18.9.22.69...
Connected to web.mit.edu.
Escape character is '^]'.
<b>GET /aboutmit/ HTTP/1.1↵
Host: web.mit.edu↵
↵</b>
HTTP/1.1 200 OK
Date: Tue, 31 Mar 2015 15:14:22 GMT
<i>... more headers ...</i>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<i>... more HTML ...</i>
<title>MIT — About</title>
<i>... lots more HTML ...</i>
</pre>
<p>This time, your request must end with a blank line. HTTP version 1.1 requires the client to specify some extra information (called headers) with the request, and the blank line signals the end of the headers.</p>
<p>You will also more than likely find that telnet does not exit after making this request — this time, the server keeps the connection open so you can make another request right away. To quit Telnet manually, type the escape character (probably <code>Ctrl</code>-<code>]</code>) to bring up the <code>telnet></code> prompt, and type <code>quit</code>.</p>
</div>
<h3 id="smtp">SMTP</h3>
<div data-outline="smtp">
<p><a href="http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol">Simple Mail Transfer Protocol (SMTP)</a> is the protocol for sending email (different protocols are used for client programs that retrieve email from your inbox). Because the email system was designed in a time before spam, modern email communication is fraught with traps and heuristics designed to prevent abuse. But we can still try to speak SMTP. Recall that the well-known SMTP port is 25, and <code>dmz-mailsec-scanner-4.mit.edu</code> was the name of a MIT email handler.</p>
<p>You'll need to fill in <em>your-IP-address-here</em> and <em>your-username-here</em>, and the ↵ indicate newlines for clarity. This will only work if you're on MITnet, and even then your mail might be rejected for looking suspicious:</p><pre class="no-markdown">$ <b>telnet dmz-mailsec-scanner-4.mit.edu 25</b>
Trying 18.9.25.15...
Connected to dmz-mailsec-scanner-4.mit.edu.
Escape character is '^]'.
220 dmz-mailsec-scanner-4.mit.edu ESMTP Symantec Messaging Gateway
<b>HELO <i>your-IP-address-here</i>↵</b>
250 2.0.0 dmz-mailsec-scanner-4.mit.edu says HELO to <i>your-ip-address</i>:<i>port</i>
<b>MAIL FROM: <<i>your-username-here</i>@mit.edu>↵</b>
250 2.0.0 MAIL FROM accepted
<b>RCPT TO: <<i>your-username-here</i>@mit.edu>↵</b>
250 2.0.0 RCPT TO accepted
<b>DATA↵</b>
354 3.0.0 continue. finished with "\r\n.\r\n"
<b>From: <<i>your-username-here</i>@mit.edu>↵
To: <<i>your-username-here</i>@mit.edu>↵
Subject: testing↵
This is a hand-crafted artisanal email.↵
.↵</b>
250 2.0.0 OK 99/00-11111-sec-22222222
<b>QUIT↵</b>
221 2.3.0 dmz-mailsec-scanner-4.mit.edu closing connection
Connection closed by foreign host.
</pre>
<p>SMTP is quite chatty in comparison to HTTP, providing some human-readable instructions like <code>continue. finished with "\r\n.\r\n"</code> to tell us how to terminate our message content.</p>
</div>
<h3 id="designing_a_wire_protocol">Designing a wire protocol</h3>
<div data-outline="designing_a_wire_protocol">
<p>When designing a wire protocol, apply the same rules of thumb you use for designing the operations of an abstract data type:</p>
<ul>
<li>
<p>Keep the number of different messages <strong>small</strong>. It's better to have a few commands and responses that can be combined rather than many complex messages.</p>
</li>
<li>
<p>Each message should have a well-defined purpose and <strong>coherent</strong> behavior.</p>
</li>
<li>
<p>The set of messages must be <strong>adequate</strong> for clients to make the requests they need to make and for servers to deliver the results.</p>
</li>
</ul>
<p>Just as we demand representation independence from our types, we should aim for <strong>platform-independence</strong> in our protocols. HTTP can be spoken by any web server and any web browser on any operating system. The protocol doesn't say anything about how web pages are stored on disk, how they are prepared or generated by the server, what algorithms the client will use to render them, etc.</p>
<p>We can also apply the three big ideas in this class:</p>
<ul>
<li>
<p><strong>Safe from bugs</strong></p>
<ul>
<li>
<p>The protocol should be easy for clients and servers to generate and parse. Simpler code for reading and writing the protocol (whether written with a parser generator like ParserLib or ANTLR, with regular expressions, etc.) will have fewer opportunities for bugs.</p>
</li>
<li>
<p>Consider the ways a broken or malicious client or server could stuff garbage data into the protocol to break the process on the other end.</p>
<p>Email spam is one example: when we spoke SMTP above, the mail server asked <em>us</em> to say who was sending the email, and there's nothing in SMTP to prevent us from lying outright. We've had to build systems on top of SMTP to try to stop spammers who lie about <code>From:</code> addresses.</p>
<p>Security vulnerabilities are a more serious example. For example, protocols that allow a client to send requests with arbitrary amounts of data require careful handling on the server to avoid running out of buffer space, <a href="http://en.wikipedia.org/wiki/Buffer_overflow">or worse</a>.</p>
</li>
</ul>
</li>
<li>
<p><strong>Easy to understand</strong>: for example, choosing a text-based protocol means that we can debug communication errors by reading the text of the client/server exchange. It even allows us to speak the protocol “by hand” as we saw above.</p>
</li>
<li>
<p><strong>Ready for change</strong>: for example, HTTP includes the ability to specify a version number, so clients and servers can agree with one another which version of the protocol they will use. If we need to make changes to the protocol in the future, older clients or servers can continue to work by announcing the version they will use.</p>
</li>
</ul>
<p><a href="http://en.wikipedia.org/wiki/Serialization"><strong>Serialization</strong></a> is the process of transforming data structures in memory into a format that can be easily stored or transmitted (not the same as <a href="/courses/course-v1:MITx+6.005.2x+1T2017/jump_to_id/vertical-serializability">serializability from the Thread Safety reading</a>). Rather than invent a new format for serializing your data between clients and servers, use an existing one. For example, <a href="http://en.wikipedia.org/wiki/JSON">JSON (JavaScript Object Notation)</a> is a simple, widely-used format for serializing basic values, arrays, and maps with string keys.</p>
</div>
<h3 id="specifying_a_wire_protocol">Specifying a wire protocol</h3>
<div data-outline="specifying_a_wire_protocol">
<p>In order to precisely define for clients & servers what messages are allowed by a protocol, use a grammar.</p>
<p>For example, here is a very small part of the HTTP 1.1 request grammar from <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html">RFC 2616 section 5</a>:</p><pre><code class="hljs css"><span class="hljs-rule"><span class="hljs-attribute">request </span>:<span class="hljs-value">:= request-line
((general-header | request-header | entity-header) CRLF)*
CRLF
message-body?
request-line ::= method SPACE request-uri SPACE http-version CRLF
method ::= <span class="hljs-string">"OPTIONS"</span> | <span class="hljs-string">"GET"</span> | <span class="hljs-string">"HEAD"</span> | <span class="hljs-string">"POST"</span> | ...
...
</span></span></code></pre>
<p>Using the grammar, we can see that in this example request from earlier:</p><pre><code class="hljs http"><span class="hljs-request">GET <span class="hljs-string">/aboutmit/</span> HTTP/1.1</span>
<span class="hljs-attribute">Host</span>: <span class="hljs-string">web.mit.edu</span>
</code></pre>
<ul>
<li><code>GET</code> is the <code>method</code>: we're asking the server to get a page for us.</li>
<li><code>/aboutmit/</code> is the <code>request-uri</code>: the description of what we want to get.</li>
<li><code>HTTP/1.1</code> is the <code>http-version</code>.</li>
<li><code>Host: web.mit.edu</code> is some kind of header — we would have to examine the rules for each of the <code>...-header</code> options to discover which one.</li>
<li>And we can see why we had to end the request with a blank line: since a single <code>request</code> can have multiple headers that end in CRLF (newline), we have another CRLF at the end to finish the <code>request</code>.</li>
<li>We don't have any <code>message-body</code> — and since the server didn't wait to see if we would send one, presumably that only applies for other kinds of requests.</li>
</ul>
<p>The grammar is not enough: it fills a similar role to method signatures when defining an ADT. We still need the specifications:</p>
<ul>
<li>
<p><strong>What are the preconditions of a message?</strong> For example, if a particular field in a message is a string of digits, is any number valid? Or must it be the ID number of a record known to the server?</p>
<p>Under what circumstances can a message be sent? Are certain messages only valid when sent in a certain sequence?</p>
</li>
<li>
<p><strong>What are the postconditions?</strong> What action will the server take based on a message? What server-side data will be mutated? What reply will the server send back to the client?</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@vertical+block@vertical_Questions_7d18732d67ec" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="vertical" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@Wire_protocols">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@Wire_protocols" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="html" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/b2188f0a95a7a9062748278f7d5a584f/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/syntax-highlighting.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/c9cee8830885f59e75b8782f44026226/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-67">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-67" data-graded="True" data-has-score="True" data-runtime-version="1" data-block-type="problem" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_07-wire-protocols-67" class="problems-wrapper" role="group"
aria-labelledby="07-wire-protocols-67-problem-title"
data-problem-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-67" data-url="/courses/course-v1:MITx+6.005.2x+1T2017/xblock/block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-67/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="07-wire-protocols-67-problem-title" aria-describedby="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-67-problem-progress" tabindex="-1">
Wire protocols 1
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-67-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following tools could you use to speak HTTP with a web server?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_07-wire-protocols-67_2_1">
<fieldset aria-describedby="status_07-wire-protocols-67_2_1">
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-67_2_1[]" id="input_07-wire-protocols-67_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="07-wire-protocols-67_2_1-choice_0-label" for="input_07-wire-protocols-67_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-67_2_1"> Chrome on your laptop
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-67_2_1[]" id="input_07-wire-protocols-67_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="07-wire-protocols-67_2_1-choice_1-label" for="input_07-wire-protocols-67_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-67_2_1"> Chrome on your phone
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-67_2_1[]" id="input_07-wire-protocols-67_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="07-wire-protocols-67_2_1-choice_2-label" for="input_07-wire-protocols-67_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-67_2_1"> SSH client
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-67_2_1[]" id="input_07-wire-protocols-67_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="07-wire-protocols-67_2_1-choice_3-label" for="input_07-wire-protocols-67_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-67_2_1"> Telnet client
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-67_2_1[]" id="input_07-wire-protocols-67_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="07-wire-protocols-67_2_1-choice_4-label" for="input_07-wire-protocols-67_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-67_2_1"> A web browser written in Java
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-67_2_1[]" id="input_07-wire-protocols-67_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="07-wire-protocols-67_2_1-choice_5-label" for="input_07-wire-protocols-67_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-67_2_1"> A web browser you wrote in Java
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-67_2_1[]" id="input_07-wire-protocols-67_2_1_choice_6" class="field-input input-checkbox" value="choice_6"/><label id="07-wire-protocols-67_2_1-choice_6-label" for="input_07-wire-protocols-67_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-67_2_1"> java.net.HttpURLConnection, which has HTTP-specific features
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-67_2_1[]" id="input_07-wire-protocols-67_2_1_choice_7" class="field-input input-checkbox" value="choice_7"/><label id="07-wire-protocols-67_2_1-choice_7-label" for="input_07-wire-protocols-67_2_1_choice_7" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-67_2_1"> java.net.Socket, the socket class discussed in this reading
</label>
</div>
<span id="answer_07-wire-protocols-67_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_07-wire-protocols-67_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_07-wire-protocols-67_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="Wire protocols 1" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_07-wire-protocols-67" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_07-wire-protocols-67">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-wire-protocols-67-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-wire-protocols-67-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-wire-protocols-67-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-68">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-68" data-graded="True" data-has-score="True" data-runtime-version="1" data-block-type="problem" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_07-wire-protocols-68" class="problems-wrapper" role="group"
aria-labelledby="07-wire-protocols-68-problem-title"
data-problem-id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-68" data-url="/courses/course-v1:MITx+6.005.2x+1T2017/xblock/block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-68/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="07-wire-protocols-68-problem-title" aria-describedby="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-68-problem-progress" tabindex="-1">
Wire protocols 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.2x+1T2017+type@problem+block@07-wire-protocols-68-problem-progress"></div>
<div class="problem">
<div>
<p>Here is the wire protocol for the problem set that will be released next week.</p>
<p>Messages from the client to the server:</p>
<blockquote class="message-user pull-margin">
<pre><code>
MESSAGE ::= ( LOOK | DIG | FLAG | DEFLAG | HELP_REQ | BYE ) NEWLINE
LOOK ::= "look"
DIG ::= "dig" SPACE X SPACE Y
FLAG ::= "flag" SPACE X SPACE Y
DEFLAG ::= "deflag" SPACE X SPACE Y
HELP_REQ ::= "help"
BYE ::= "bye"
NEWLINE ::= "
" | "
" "
"?
X ::= INT
Y ::= INT
SPACE ::= " "
INT ::= "-"? [0-9]+</code></pre>
</blockquote>
<p>Using this grammar, which of these is a valid request from the client? In the choices below, we use &#8629; to represent a newline.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_07-wire-protocols-68_2_1">
<fieldset aria-describedby="status_07-wire-protocols-68_2_1">
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-68_2_1[]" id="input_07-wire-protocols-68_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="07-wire-protocols-68_2_1-choice_0-label" for="input_07-wire-protocols-68_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-68_2_1"> look
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-68_2_1[]" id="input_07-wire-protocols-68_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="07-wire-protocols-68_2_1-choice_1-label" for="input_07-wire-protocols-68_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-68_2_1"> dig seven seven&#8629;
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-68_2_1[]" id="input_07-wire-protocols-68_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="07-wire-protocols-68_2_1-choice_2-label" for="input_07-wire-protocols-68_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-68_2_1"> help!&#8629;
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-68_2_1[]" id="input_07-wire-protocols-68_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="07-wire-protocols-68_2_1-choice_3-label" for="input_07-wire-protocols-68_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-68_2_1"> flag 007 9999&#8629;
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-68_2_1[]" id="input_07-wire-protocols-68_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="07-wire-protocols-68_2_1-choice_4-label" for="input_07-wire-protocols-68_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-68_2_1"> DEFLAG 1 1&#8629;
</label>
</div>
<span id="answer_07-wire-protocols-68_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_07-wire-protocols-68_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_07-wire-protocols-68_solution_1"/>
</div><p>Messages from the server to the client:</p>
<blockquote class="message-server pull-margin">
<pre><code>
MESSAGE ::= BOARD | BOOM | HELP | HELLO
BOARD ::= LINE+
LINE ::= (SQUARE SPACE)* SQUARE NEWLINE
SQUARE ::= "-" | "F" | COUNT | SPACE
SPACE ::= " "
NEWLINE ::= "
" | "
" "
"?
COUNT ::= [1-8]
BOOM ::= "BOOM!" NEWLINE
HELP ::= [^
]+ NEWLINE
HELLO ::= "Welcome to Minesweeper. Players: " N " including you. Board: "
X " columns by " Y " rows. Type 'help' for help." NEWLINE
N ::= INT
X ::= INT
Y ::= INT
INT ::= "-"? [0-9]+
</code></pre>
</blockquote>
<p>Which of these is a valid response from the server?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_07-wire-protocols-68_3_1">
<fieldset aria-describedby="status_07-wire-protocols-68_3_1">
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-68_3_1[]" id="input_07-wire-protocols-68_3_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="07-wire-protocols-68_3_1-choice_0-label" for="input_07-wire-protocols-68_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-68_3_1"> - - -&#8629;- - -&#8629;- - -&#8629;
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-68_3_1[]" id="input_07-wire-protocols-68_3_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="07-wire-protocols-68_3_1-choice_1-label" for="input_07-wire-protocols-68_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-68_3_1"> - - - &#8629;- - - &#8629;- - - &#8629;
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-68_3_1[]" id="input_07-wire-protocols-68_3_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="07-wire-protocols-68_3_1-choice_2-label" for="input_07-wire-protocols-68_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-68_3_1"> 1 2 3&#8629;4 5 6&#8629;7 8 9&#8629;
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-68_3_1[]" id="input_07-wire-protocols-68_3_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="07-wire-protocols-68_3_1-choice_3-label" for="input_07-wire-protocols-68_3_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-68_3_1"> You don't need help, Multiplayer Minesweeper is simple and intuitive!&#8629;
</label>
</div>
<div class="field">
<input type="checkbox" name="input_07-wire-protocols-68_3_1[]" id="input_07-wire-protocols-68_3_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="07-wire-protocols-68_3_1-choice_4-label" for="input_07-wire-protocols-68_3_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_07-wire-protocols-68_3_1"> Available commands:&#8629;look, dig, flag, deflag, help, bye&#8629;Have fun!&#8629;
</label>
</div>
<span id="answer_07-wire-protocols-68_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_07-wire-protocols-68_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_07-wire-protocols-68_solution_2"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="Wire protocols 2" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_07-wire-protocols-68" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_07-wire-protocols-68">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-wire-protocols-68-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-wire-protocols-68-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="07-wire-protocols-68-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@vertical+block@vertical-testing-clientserver" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="vertical" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<h2 class="hd hd-2 unit-title">Testing client/server code</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_72e5d2ce47a7">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_72e5d2ce47a7" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="html" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/b2188f0a95a7a9062748278f7d5a584f/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/syntax-highlighting.css" rel="stylesheet" type="text/css" />
<h2 id="testing_clientserver_code">Testing client/server code</h2>
<div data-outline="testing_clientserver_code">
<p>Remember that <a href="/courses/course-v1:MITx+6.005.2x+1T2017/jump_to_id/vertical-message-passing#concurrency_is_hard_to_test_and_debug">concurrency is hard to test and debug</a>. We can't reliably reproduce race conditions, and the network adds a source of latency that is entirely beyond our control. You need to design for concurrency and argue carefully for the correctness of your code.</p>
<h3 id="separate_network_code_from_data_structures_and_algorithms">Separate network code from data structures and algorithms</h3>
<div data-outline="separate_network_code_from_data_structures_and_algorithms">
<p>Most of the ADTs in your client/server program don't need to rely on networking. Make sure you specify, test, and implement them as separate components that are safe from bugs, easy to understand, and ready for change — in part because they don't involve any networking code.</p>
<p>If those ADTs will need to be used concurrently from multiple threads (for example, threads handling different client connections), our next reading will discuss your options. Otherwise, use the <a href="/courses/course-v1:MITx+6.005.2x+1T2017/jump_to_id/vertical-thread-safety">thread safety strategies of confinement, immutability, and existing threadsafe data types</a>.</p>
</div>
<h3 id="separate_socket_code_from_stream_code">Separate socket code from stream code</h3>
<div data-outline="separate_socket_code_from_stream_code">
<p>A function or module that needs to read from and write to a socket may only need access to the input/output streams, not to the socket itself. This design allows you to test the module by connecting it to streams that don't come from a socket.</p>
<p>Two useful Java classes for this are <a href="http://docs.oracle.com/javase/8/docs/api/?java/io/ByteArrayInputStream.html"><code>ByteArrayInputStream</code></a> and <a href="http://docs.oracle.com/javase/8/docs/api/?java/io/ByteArrayOutputStream.html"><code>ByteArrayOutputStream</code></a>. Suppose we want to test this method:</p><pre class="pull-margin"><p><code>void upperCaseLine(BufferedReader input, PrintWriter output) throws IOException</code>
<em>requires</em>: input and output are open
<em>effects</em>: attempts to read a line from input and attempts to write that line, in upper case, to output</p></pre>
<p>The method is normally used with a socket:</p>
<div class="pull-margin"><pre><code class="language-java hljs">Socket sock = ...
<span class="hljs-comment">// read a stream of characters from the socket input stream</span>
BufferedReader in = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> InputStreamReader(sock.getInputStream()));
<span class="hljs-comment">// write characters to the socket output stream</span>
PrintWriter out = <span class="hljs-keyword">new</span> PrintWriter(sock.getOutputStream(), <span class="hljs-keyword">true</span>);
upperCaseLine(in, out);</code></pre></div>
<p>If the case conversion is a function we implement, it should already be specified, tested, and implemented separately. But now we can now also test the read/write behavior of <code>upperCaseLine</code>:</p>
<div class="pull-margin"><pre><code class="language-java hljs"><span class="hljs-comment">// fixed input stream of "dog" (line 1) and "cat" (line 2)</span>
String inString = <span class="hljs-string">"dog\ncat\n"</span>;
ByteArrayInputStream inBytes = <span class="hljs-keyword">new</span> ByteArrayInputStream(inString.getBytes());
ByteArrayOutputStream outBytes = <span class="hljs-keyword">new</span> ByteArrayOutputStream();
<span class="hljs-comment">// read a stream of characters from the fixed input string</span>
BufferedReader in = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> InputStreamReader(inBytes));
<span class="hljs-comment">// write characters to temporary storage</span>
PrintWriter out = <span class="hljs-keyword">new</span> PrintWriter(outBytes, <span class="hljs-keyword">true</span>);
upperCaseLine(in, out);
<span class="hljs-comment">// check that it read the expected amount of input</span>
assertEquals(<span class="hljs-string">"expected input line 2 remaining"</span>, <span class="hljs-string">"cat"</span>, in.readLine());
<span class="hljs-comment">// check that it wrote the expected output</span>
assertEquals(<span class="hljs-string">"expected upper case of input line 1"</span>, <span class="hljs-string">"DOG\n"</span>, outBytes.toString());</code></pre></div>
<p>In this test, <code>inBytes</code> and <code>outBytes</code> are <a href="//courses.edx.org/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-unit-testing-and-stubs#unit_testing_and_stubs"><strong>test stubs</strong></a>. To isolate and test just <code>upperCaseLine</code>, we replace the components it normally depends on (input/output streams from a socket) with components that satisfy the same spec but have canned behavior: an input stream with fixed input, and an output stream that stores the output in memory.</p>
<p>Testing strategies for more complex modules might use a <strong>mock object</strong> to simulate the behavior of a real client or server by producing entire canned sequences of interaction and asserting the correctness of each message received from the other component.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@vertical+block@Reading_7_Summary" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="vertical" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<h2 class="hd hd-2 unit-title">Reading 7 Summary</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_1d4eff685ab4">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-request-token="70df6590e99811efb7c60e0e3c45b88f" data-usage-id="block-v1:MITx+6.005.2x+1T2017+type@html+block@html_1d4eff685ab4" data-graded="True" data-has-score="False" data-runtime-version="1" data-block-type="html" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.2x+1T2017" data-runtime-class="LmsRuntime">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/b2188f0a95a7a9062748278f7d5a584f/asset-v1:MITx+6.005.2x+1T2017+type@asset+block/syntax-highlighting.css" rel="stylesheet" type="text/css" />
<h2 id="summary">Summary</h2>
<div data-outline="summary">
<p>In the <em>client/server design pattern</em>, concurrency is inevitable: multiple clients and multiple servers are connected on the network, sending and receiving messages simultaneously, and expecting timely replies. A server that <em>blocks</em> waiting for one slow client when there are other clients waiting to connect to it or to receive replies will not make those clients happy. At the same time, a server that performs incorrect computations or returns bogus results because of concurrent modification to shared mutable data by different clients will not make anyone happy.</p>
<p>All the challenges of making our multi-threaded code <strong>safe from bugs</strong>, <strong>easy to understand</strong>, and <strong>ready for change</strong> apply when we design network clients and servers. These processes run concurrently with one another (if on different machines), and any server that wants to talk to multiple clients concurrently (or a client that wants to talk to multiple servers) must manage that multi-threaded communication.</p>
</div>
<div class="license">This reading was collaboratively authored with contributions from: Saman Amarasinghe, Adam Chlipala, Srini Devadas, Michael Ernst, Max Goldman, John Guttag, Daniel Jackson, Rob Miller, Martin Rinard, and Armando Solar-Lezama. This work is licensed under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a>.</div>
</div>
</div>
</div>
</div>