Only one usage of each socket address is normally permitted Python
<p>I wrote a basic program in to create a socket with a server and a client. But the problem is that when I run the code, it gives me an error saying that only one usage of each socket address is normally permitted. So I think the problem is due to the port, I changed the port and it still don't work. How do I get this to work?</p>

<p>This is my code :</p>

<p>Server</p>

<pre><code>import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind(('localhost',3200))
sock.listen(1) 
print "Server is ready to receive data..."
client, address = sock.accept()
msg = client.recv(1024) 
print msg
</code></pre>

<p>Client</p>

<pre><code>import socket
connection_to_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
connection_to_server.bind(('localhost',3200))
msg = raw_input("Please enter a content :")
connection_to_server.send(msg) 
</code></pre>

<p>Thanks for your help !</p>

<p>I think there's a fundamental misunderstanding of how sockets work here.</p>

<p>The <a href="https://docs.python.org/2/library/socket.html#socket.socket.bind" rel="nofollow"><code>socket.bind()</code></a> call is used to bind to a particular port on a particular interface, the pair specified using a network address (bind to port <code>8080</code> on <code>127.0.0.1)</code>. You need to do this on the server side before you can start reading incoming data i.e "listening" on a particular socket. Only the server needs to do this. The client will then use <a href="https://docs.python.org/2/library/socket.html#socket.socket.connect" rel="nofollow"><code>socket.connect</code></a> to connect to this socket.</p>

<p>As spectras pointed out in the comments, a bind is necessary when you need to communicate through a particular interface/port combination, which is almost always necessary for the server, but not always for the client. The client and server can't <em>both</em> have access/bind to the same port on the same interface, it makes little sense to do so.</p>

<p>Your client and server both try to start listening on the same socket, which is as the error message suggests, not allowed.</p>

<p>You should go through the <a href="https://docs.python.org/2/howto/sockets.html" rel="nofollow">Socket Programming HOWTO</a> before proceeding further.</p>

python sockets