1

I am trying to create a client to connect to a server with given url address.

I used this way

host_ip = socket.gethostbyname('HOST_NAME')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('127.0.0.1', 0))
sock.connect((host_ip, 8080))

but it printed the following error

OSError: [Errno 22] Invalid argument

Can someone explain me why is wrong and give me a solution?

1
  • Why are you binding the socket? This is used if you are the server. Commented Mar 25, 2017 at 16:37

1 Answer 1

2

You don't have to bind your socket, this is done server-side.

Here's the example code from the documentation for socket :

import socket

HOST = 'your-url.net'    # The remote host
PORT = 8080              # The same port as used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    data = s.recv(1024)
print('Received', repr(data))

This is a simple snippet, which connects to the server, sends some data, and prints the response.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.