0

I'm able to start up the server script and get it running no problem, but every time i try to connect with my client script it keeps saying connection refused. I tried running the script on both Linux and windows. For the client and server I tried changing the HOST variable by using just "localhost" as well as gethostbyname.

import socket
from time import ctime
from random import *
PORT = 2222
s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST = socket.gethostbyname("localhost")
ADDRESS=(HOST,PORT)

def random_integer_division():
    random.randint(1,500)
    x = random.randint(1,500)
    y = random.randint(1,500)
    print(x)
    print(y)
    if x>y:
        return(x//y)
    else:
        return(y//x)

s.bind(ADDRESS)

s.listen(7)


while True:
    print("Waiting for connection. . .")

    c, addr = s.accept()

#prints clients address
    print(f'... conneceted form: {addr}')

    c.send(bytes(f'Information returned form server: \n{str(random_integer_division())}'))
    print("CLient disconnected")


    c.close()
print("\nClient connection closed")


import socket
from codecs import decode
#HOST = "localhost"
PORT = 2222
HOST = socket.gethostbyname("localhost")
ADDRESS=(HOST,PORT)

#calling socket to create a bew sicket object
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.connect(ADDRESS)

dayAndTime = decode(s.recv(BUFSIZE), "ascii")

print(dayAndTIme)

s.close()

print("\nConnection to server closed")

1 Answers1

0

That means your server wasn't active when trying to connect with the client.

Your code has a few typos in it which are problematic but otherwise it just needs to be split into two separate files.

I split them and cleaned up a bit in the blocks below, which should work. I made some inline notes where I made changes. Be sure to run the server first, and then run the client in a separate terminal.

  1. client.py
import socket
PORT = 2222
HOST = socket.gethostbyname("localhost")
ADDRESS=(HOST,PORT)

#calling socket to create a new socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect(ADDRESS)

dayAndTime = s.recv(BUFSIZE).decode("utf-8")  # changed decode method

print(dayAndTime)  # you had a type here

s.close()

print("\nConnection to server closed")
  1. server.py
import socket
import random  # you had the wrong import here

PORT = 2222
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST = socket.gethostbyname("localhost")
ADDRESS=(HOST,PORT)

def random_integer_division():   # unneeded code here
    x = random.randint(1,500)
    y = random.randint(1,500)
    print(x)
    print(y)
    if x>y:
        return(x//y)
    else:
        return(y//x)

s.bind(ADDRESS)


while True:
    s.listen(7)   # moved this inside the while loop.
    print("Waiting for connection. . .")
    c, addr = s.accept()

    #prints clients address
    print(f'... conneceted form: {addr}')

    # changed the method used to encode string
    c.send(f'Information returned form server: \n{str(random_integer_division())}'.encode("utf-8")))
    print("Client Disconnected")
    c.close()

print("\nClient connection closed")

Alexander
  • 16,091
  • 5
  • 13
  • 29