0

I am trying to test socket communication on my laptop using python. However, I'm not sure why the connection is not being established? I keep getting error that the target machine is actively refusing connection. I am trying to use the same computer to run both the client and the server portion. The server is running fine but the client is the one not connecting. I think I have the hostname wrong (127.0.0.1) but not sure what Im supposed to be using? I also tried changing the server hostname to (0.0.0.0) and the IPV4 address for the hostname the client was to connect to but that didn't work either. Any help would be appreciated!

My code(server portion):

import socket

comms_socket =socket.socket()
comms_socket.bind(('127.0.0.1', 50000))
comms_socket.listen(10)
connection, address = comms_socket.accept()

while True:
    print(connection.recv(4096).decode("UTF-8"))
    send_data = input("Reply: ")
    connection.send(bytes(send_data, "UTF-8"))

Client portion:

import socket

comms_socket = socket.socket()
comms_socket.connect(('127.0.0.1',50000))

while True:

    send_data = input("Message: ")
    comms_socket.send(bytes(send_data, "UTF-8"))
    print(comms_socket.recv(4096).decode("UTF-8"))
Fuhua Song
  • 3
  • 1
  • 2
  • Cannot reproduce your problem, your code works fine on my machine. (windows 7, python 3.5). Check if you have some form of firewall installed that is preventing your code to open a socket. Also, what Os are you running this on. – Irmen de Jong Aug 15 '16 at 17:55
  • This code as is works fine for me too. BTW, the usual pattern is to have the server listen to any address (`INADDR_ANY`) which, in python, is indicated by an empty string (`""`). Then it will receive messages via the localhost interface (`127.0.0.1`) or any other interface with an IP address. The client always has to provide an explicit address in the `connect`. – Gil Hamilton Aug 15 '16 at 18:09
  • Im using windows 10, python 3.5. I think it's just IDLE that was giving me the problem when I was running it on the python shell. When I run directly, the program worked. Thanks for the help though – Fuhua Song Aug 15 '16 at 18:43

1 Answers1

1

Your code won't work with python 2.* , because of the differences in input(), raw_input(), bytes, etc. in python 3.* vs python 2.* . You'd have to minimally make the following changes to get it working with python 2.*. Otherwise, use python 3 to run your code:

Server program:

import socket

comms_socket =socket.socket()
comms_socket.bind(('127.0.0.1', 7000))
comms_socket.listen(10)
connection, address = comms_socket.accept()

while True:
    print(connection.recv(4096).decode("UTF-8"))
    send_data = raw_input("Reply: ") # Use raw_input() instead of input()
    connection.send(send_data.encode("UTF-8")) 

Client program:

import socket


comms_socket = socket.socket()
comms_socket.connect(('127.0.0.1',7000))

while True:

    send_data = raw_input("Message: ")
    comms_socket.send(send_data.encode("UTF-8"))
    print(comms_socket.recv(4096).decode("UTF-8"))

If you want to use bytes as intended in your specific usecase, you should use bytesarray instead in python 2.6 or higher. Check this: the bytes type in python 2.7 and PEP-358

Community
  • 1
  • 1
Abhishek Balaji R
  • 665
  • 10
  • 25
  • im using python 3, I was originally using the python shell from IDLE to run them but it didn't work. However, when I straight up run them, they do work. Maybe it's something with IDLE? – Fuhua Song Aug 15 '16 at 18:41