0

I am attempting to connect a simple server and client from two computers on the same network. Both the client and server cannot 'find' each other, as they do not move past .connect() and .accept() respectively. What am I doing wrong?

(Windows 10)

Server:

import socket

HOST = socket.gethostname()    #Returns: "WASS104983"
#I have also tried socket.gethostbyname(socket.gethostname)), returning: "25.38.252.147"
PORT = 50007

sock = socket.socket()
sock.bind((HOST, PORT))
sock.listen(5)

print("Awaiting connection... ")

(clnt, addr) = sock.accept()

print("Client connected")
…

and Client:

import socket

HOST = "WASS104983"    #Or "25.38.252.147", depending on the servers setup
PORT = 50007

sock = socket.socket()

print("Attempting connection... ")

sock.connect((HOST, PORT))

print("Connected")
…

I have gotten this to work before so I am not sure why it's not now.

I know there are a few questions of this calibre, but none seem to cover my problem.

Also, a wifi extender should not interfere with local transmissions should it?

1 Answer 1

1

I have always seen servers setup as such:

import socket
import threading

bind_ip = '0.0.0.0'
bind_port = 9999

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5)

print("[*] Listening on {}:{}".format(bind_ip, bind_port))


def handle_client(client_socket):
    request = client_socket.recv(1024)
    print('received: {}'.format(request))
    client_socket.send(b'ACK!')
    client_socket.close()


while True:
    client, addr = server.accept()
    print("[*] Accepted connection from: {}:{}".format(addr[0], addr[1]))
    client_handler = threading.Thread(target=handle_client, args=(client,))
    client_handler.start()*

Where I think an important distinction from your post may be that the server accepting connections is within an infinite loop. Have you tried this?

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

8 Comments

Indeed I have. From what I understand a loop should only be required when the .accept() method needs to be called multiple times (obviously), being either if it is set to time out or when accepting multiple client connections. I haven't set a timeout for mine so it shouldn't move past that line until it has found a client to connect (which it doesn't because "Client connected" is never printed to console). I have also tried using the "0.0.0.0" hostname but it raises an error saying that it's not valid. Not sure :/
I think the problem is likely your IP then. It looks like you are using a public IP, but if they are on the same network a private IP would likely use an IP like 192.168.0-255.0-255 or 10.0.0.0-255 (there are specific ranges like these that are specified for private internal networks). I would recommend opening command prompt and running ipconfig (windows) or ifconfig (linux for future viewers) and look for your local ipv4 address and use that
Yeah I did try that as well (Sorry for not stating this before) to no avail.
Hmm. You state that I have gotten this to work before. What changed since then?
I have very little idea, that's the problem. Indeed, I have connected two computers over the same networks and over different networks before. This simple server is part of a much larger, more complex project but when trying to get it to connect I resorted to basics to find the problem. During the larger project I messed around a little with port forwarding and the firewall so I might have screwed something up. P.S. I appreciate your persistence.
|

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.