Rony Cohen Rony Cohen - 1 year ago 90
Python Question

My socket always connect to the server even the server offline

I create a simply client and a server using python. In order to create my server I do a port forwarding. But I have a problem that I don't success to resolve, in fact no matter if the server is online or offline. My socket gives me a message of success. Where does this failure come from ?

This is my code in the client:

import socket
from threading import Thread

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("", 8350))
print "Client connection success"
except socket.error:
print "Client error"

def send_to_server():
msg = raw_input("Client:")

def recv_from_server():
response_server = client_socket.recv(1024)
print response_server

def main():
while True:
send_thread = Thread(target=send_to_server,)
recv_thread = Thread(target=recv_from_server,)



if __name__ == "__main__":

Answer Source

As you are connecting to the intermediate node using socket which in turn forwarding you request to target node in the same network, the connection to your intermediate node will be successful even if your target node is down.

So since your client's client_socket.connect() won't be informed about whether the socket connection to target node from intermediate node was successful or not, it will always return success as it was successfully connected to the intermediate node.

But a simple hack to find out whether you were actually connected to target node would be to check whether you are actually receiving the expected response within a specified amount of time after sending the request (Assuming that for every request being sent by client, the server will be sending some response).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download