Ad

Getting Error Non-blocking (10035) Error When Trying To Connect To Server

- 1 answer

I am trying to simply send a list from one computer to another.

I have my server set up on one computer, where the IP address is 192.168.0.101 The code for the server:

import socket
import pickle
import time
import errno


HEADERSIZE = 20
HOST = socket.gethostbyname(socket.gethostname())
PORT = 65432

print(HOST)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)

while True:
    conn, adrs = s.accept()
    print(f"Connection with {adrs} has been established")

    conn.setblocking(1)

    try:
        data = conn.recv(HEADERSIZE)
        if not data:
            print("connection closed")
            conn.close()
            break
        else:
            print("Received %d bytes: '%s'" % (len(data), pickle.loads(data)))
    except socket.error as e:
        if e.args[0] == errno.EWOULDBLOCK:
            print('EWOULDBLOCK')
            time.sleep(1)  # short delay, no tight loops
        else:
            print(e)
            break

The client is on another computer. The code:

import socket
import pickle


HOST = '192.168.0.101'
PORT = 65432

def send_data(list):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(10)
    print(".")
    print(s.connect_ex((HOST, PORT)))
    print(".")
    data = pickle.dumps(list)

    print(len(data))

    s.send(data)

    s.close()

send_data([1,1,1])

The outputted error number of connect_ex is 10035. I read a lot about the error, but all I found was about the server side. To me, it looks like the problem is with the client and that it is unable to make a connection to 192.168.0.101. But then, I don't understand why the error I get is about non-blocking.

What is it that I am doing wrong that I am unable to send data?

Ad

Answer

First of all, how user207421 suggested, change the timeout to a longer duration.

Also, as stated here Socket Programming in Python raising error socket.error:< [Errno 10060] A connection attempt failed I was trying to run my server and connect to a private IP address.

The fix is: on the server side, in the s.bind, to leave the host part empty

HOST = ''
PORT = 65432
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))

And on the client side, use the public IP of the PC where the server is running (I got it from ip4.me)

HOST = 'THE PUBLIC IP' #not going to write it
PORT = 65432
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, PORT))
Ad
source: stackoverflow.com
Ad