<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>logIt &#187; socket</title>
	<atom:link href="https://lakm.us/logit/tag/socket/feed/" rel="self" type="application/rss+xml" />
	<link>https://lakm.us/logit</link>
	<description>Log Around The Clock</description>
	<lastBuildDate>Sat, 06 Jun 2015 14:17:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Close Low Level Socket in Python</title>
		<link>https://lakm.us/logit/2012/02/close-level-socket-python/</link>
		<comments>https://lakm.us/logit/2012/02/close-level-socket-python/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 05:12:11 +0000</pubDate>
		<dc:creator>Arif</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[socket]]></category>

		<guid isPermaLink="false">http://lakm.us/logit/?p=390</guid>
		<description><![CDATA[Important to do in socket programming is to close the socket. Python.org says Close the socket. All future operations on the socket object will fail. The remote end will receive no more data (after queued data is flushed). Sockets are automatically closed when they are garbage-collected. I implemented this low level networking interface for a [...]]]></description>
				<content:encoded><![CDATA[<p>Important to do in socket programming is to close the socket. <a href="http://docs.python.org/library/socket.html" title="Python Doc: Low-level networking interface" target="_blank">Python.org</a> says</p>
<blockquote><p>Close the socket. All future operations on the socket object will fail. The remote end will receive no more data (after queued data is flushed). Sockets are automatically closed when they are garbage-collected.</p></blockquote>
<p>I implemented this low level networking interface for a server listening to remote command. The complete code can be found on <a href="https://github.com/bandono/zoom-camera-board/blob/2.2/server.py" title="Github: server.py" target="_blank">Github</a>, the following lines are just part to show when to close socket:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>43
44
45
46
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">        clientSocket,address =s.<span style="color: black;">accept</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        data = clientSocket.<span style="color: black;">recv</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1024</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Receive command:&quot;</span>+data
        clientSocket.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>What happened with the TCP state when the socket wasn&#8217;t closed: it stayed in <code>CLOSE_WAIT</code> state. The screenshot below shows a clip of the TCP conversation in <a href="http://www.wireshark.org/" title="www.wireshark.org/" target="_blank">Wireshark</a> sniff and the <code>netstat</code> output:</p>
<div class="wp-caption aligncenter" style="width: 460px"><img alt="" src="../../../../images/python-tcp-socket-without-close-server-wait.jpg" title="python-tcp-socket-without-close-server-wait.jpg" width="420" height="297" /><p class="wp-caption-text">Listening port state of a socket server written in Python</p></div>
]]></content:encoded>
			<wfw:commentRss>https://lakm.us/logit/2012/02/close-level-socket-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
