0

I want to create a chat program between two machines. I am using machine one which has the IP address 192.168.0.5, I can successfully send a message to machine two 192.168.0.2, and then send a message in response from machine two, to machine 1.

However I encountered a problem on the second send attempt from either machine, (noticing that before sending the second time I wait for a response from the initial send) claiming that the IP address is already in use or connection is refused, how can this be changed so that a defined number of choices can be sent?

I appreciate that the following code is not the most efficient way of sending and receiving multiple messages, that would be some description of for loop. For example for sendAndRecieve in range(0,5).

The two machines are linked using ethernet cables running through a switch and the code is run simultaneously.

Machine 1 code:

#Sending first message

host = "192.168.0.5"
port = 4446
from socket import *
s = socket(AF_INET, SOCK_STREAM)
s.bind((host,port))
s.listen(1)
print("listening")
q,addr = s.accept(1024)
data = "This is the first message I am sending"
data = data.encode("utf-8")
q.send(data)
s.close

#Recieving response message 1

while True:
    try:
        host = "192.168.0.2"
        port = 4446
        from socket import*
        s = socket(AF_INET, SOCK_STREAM)
        s.connect((host,port))
        msg = s.recv(1024)
        msg = msg.decode("utf-8")
        print(msg)
        s.close()
    except:
        pass

#Sending second message this is where the problem happens

host = "192.168.0.5"
port = 4446
from socket import *
s = socket(AF_INET, SOCK_STREAM)
s.bind((host,port))
s.listen(1)
print("listening")
q,addr = s.accept(1024)
data = "This is the first message I am sending"
data = data.encode("utf-8")
q.send(data)
s.close

Machine 2 code:

#Recieving message 1

while True:
    try:
        host = "192.168.0.5"
        port = 4446
        from socket import*
        s = socket(AF_INET, SOCK_STREAM)
        s.connect((host,port))
        msg = s.recv(1024)
        msg = msg.decode("utf-8")
        print(msg)
        s.close()
    except:
        pass

#Sending first message

host = "192.168.0.2"
port = 4446
from socket import *
s = socket(AF_INET, SOCK_STREAM)
s.bind((host,port))
s.listen(1)
print("listening")
q,addr = s.accept(1024)
data = "This is the first message I am sending"
data = data.encode("utf-8")
q.send(data)
s.close

#Recieving response message 1 this is where the problem happens

while True:
    try:
        host = "192.168.0.25"
        port = 4446
        from socket import*
        s = socket(AF_INET, SOCK_STREAM)
        s.connect((host,port))
        msg = s.recv(1024)
        msg = msg.decode("utf-8")
        print(msg)
        s.close()
    except:
        pass
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Wesley Wood
  • 7
  • 3
  • 5

1 Answers1

0

Reading through your code, I don't see how Machine 2's while loop will ever stop trying to receive data from Machine 1 (there's no break in the loop other than if an error is encountered). Machine 1 does continue on after Machine 2 connects the first time, but then tries to connect to Machine 2 while Machine 2 is trying to connect to Machine 1. That's probably the cause of the error that you're seeing, and why you only see the first message sent/received.

gordosac
  • 26
  • 3