I am working on a key manager and i need programs A, B and KM to communicate one with another. What I want is for program A to host connections for B and KM and for B to connect to A and let KM connect to B.
What I want is for program A to host connections for B and KM and for B to connect to A and let KM connect to B. At the moment I create 2 sockets on 2 different ports in program A(one for B and one for KM), I have a socket+port in B which allows KM to connect to it and another port + socket which connects B to A, finally, in KM i connect to both A and B, each connection has a socket and port. My plan is to run A first, then B so that it can connect to A and start listening for KM and then have KM connect to both of them.
code for KM:
TCP_IP = "127.0.0.1"
TCP_PORT_A = 50021
TCP_PORT_B = 50031
sockA = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockA
sockA.connect((TCP_IP, TCP_PORT_A))
print("conn to A")
sockB = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockB.connect((TCP_IP, TCP_PORT_B))
print("conn to B")
code for A:
TCP_IP = "127.0.0.1"
TCP_PORT_REC_B = 50001
TCP_PORT_REC_KM = 50021
sockB = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockB.bind((TCP_IP, TCP_PORT_REC_B))
sockB.listen(1)
print('listening on ', (TCP_IP, TCP_PORT_REC_B))
connB, addrB = sockB.accept()
print("connection address B: ", addrB)
sockKM = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockKM.bind((TCP_IP, TCP_PORT_REC_KM))
sockKM.listen(1)
print('listening on ', (TCP_IP, TCP_PORT_REC_KM))
connKM, addrKM = sockKM.accept()
print("connection address K: ", addrKM)
code for B:
TCP_IP = "127.0.0.1"
TCP_PORT_A = 50001
TCP_PORT_KM = 50031
sockA = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockA.connect((TCP_IP, TCP_PORT_A))
print("connected to A")
sockKM = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockKM.bind((TCP_IP, TCP_PORT_KM))
print("listening")
sockKM.listen(1)
connKM, addrKM = sockKM.accept()
print("connection address k: ", addrKM)
I run A, then B, B says that it connected to A and starts listening, A doesn't confirm the connection, I run KM and i get:
sockA.connect((TCP_IP, TCP_PORT_A))
ConnectionRefusedError: [WinError 10061] No connection could be made
because the target machine actively refused it
Your problem is right here:
sockA = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Everyone of your sockets use SOCK_STREAM
except for this one. It's in "code for B". Also you have a line that just says sockA
in "code for KM".
EDIT: Should work the way you intended if you fix that problem. I just tested it myself.