hyoungseok kim hyoungseok kim - 15 days ago 10
Python Question

how to solve socket.error: [Errno 10061] in python with server and my computer

I want to make the tcp communication with my computer and server(ubuntu).

The server ip is 203.246.114.176 and it's port 30000 is open.

Its okay that server is running the server.py

server.py

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 30000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
connection, client_address = sock.accept()

try:
print >>sys.stderr, 'connection from', client_address

# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
print >>sys.stderr, 'received "%s"' % data
if data:
print >>sys.stderr, 'sending data back to the client'
connection.sendall(data)
else:
print >>sys.stderr, 'no more data from', client_address
break

finally:
# Clean up the connection
connection.close()


And my computer has client.py

client.py

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('203.246.114.176', 30000)
print >>sys.stderr, 'connecting to %s port %s' % server_address
sock.connect(server_address)

try:

# Send data
message = 'This is the message. It will be repeated.'
print >>sys.stderr, 'sending "%s"' % message
sock.sendall(message)

# Look for the response
amount_received = 0
amount_expected = len(message)

while amount_received < amount_expected:
data = sock.recv(16)
amount_received += len(data)
print >>sys.stderr, 'received "%s"' % data

finally:
print >>sys.stderr, 'closing socket'
sock.close()


When I run at server, the output is like this.

starting up on localhost port 30000
waiting for a connection

enter image description here

and the ufw status is like this.

To Action From
-- ------ ----
22 ALLOW Anywhere
5979/tcp ALLOW Anywhere
5901 ALLOW Anywhere
8080 ALLOW Anywhere
5978/tcp ALLOW Anywhere
5980/tcp ALLOW Anywhere
31415 ALLOW Anywhere
5981 ALLOW Anywhere
5982 ALLOW Anywhere
5983 ALLOW Anywhere
5984 ALLOW Anywhere
5985 ALLOW Anywhere
22/tcp ALLOW Anywhere
2222/tcp ALLOW Anywhere
77/tcp ALLOW Anywhere
21 ALLOW Anywhere
3306 ALLOW Anywhere
30000/tcp ALLOW Anywhere
30001/tcp ALLOW Anywhere
30002/tcp ALLOW Anywhere
30003/tcp ALLOW Anywhere
30004/tcp ALLOW Anywhere
30005/tcp ALLOW Anywhere
30006/tcp ALLOW Anywhere
30007/tcp ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
5979/tcp (v6) ALLOW Anywhere (v6)
5901 (v6) ALLOW Anywhere (v6)
8080 (v6) ALLOW Anywhere (v6)
5978/tcp (v6) ALLOW Anywhere (v6)
5980/tcp (v6) ALLOW Anywhere (v6)
31415 (v6) ALLOW Anywhere (v6)
5981 (v6) ALLOW Anywhere (v6)
5982 (v6) ALLOW Anywhere (v6)
5983 (v6) ALLOW Anywhere (v6)
5984 (v6) ALLOW Anywhere (v6)
5985 (v6) ALLOW Anywhere (v6)
22/tcp (v6) ALLOW Anywhere (v6)
2222/tcp (v6) ALLOW Anywhere (v6)
77/tcp (v6) ALLOW Anywhere (v6)
21 (v6) ALLOW Anywhere (v6)
3306 (v6) ALLOW Anywhere (v6)
30000/tcp (v6) ALLOW Anywhere (v6)
30001/tcp (v6) ALLOW Anywhere (v6)
30002/tcp (v6) ALLOW Anywhere (v6)
30003/tcp (v6) ALLOW Anywhere (v6)
30004/tcp (v6) ALLOW Anywhere (v6)
30005/tcp (v6) ALLOW Anywhere (v6)
30006/tcp (v6) ALLOW Anywhere (v6)
30007/tcp (v6) ALLOW Anywhere (v6)


enter image description here

But my computer show this output.

C:\Users\user\Documents\notepad>python client.py
Traceback (most recent call last):
File "client.py", line 7, in <module>
s.connect((host, port))
File "C:\Python27\lib\socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10061]


What is the matter ??

And what should I do?

help plz.

Answer

You have an error in server in this line:

server_address = ('localhost', 30000)

localhost is usually 127.0.0.1, so You are not binding it to 203.246.114.176 as You think. Try using

server_address = ('203.246.114.176', 30000)

for this exact IP address, or

server_address = ('0.0.0.0', 30000)

to listen on all active interfaces.

Comments