I have tried to create a simple socket program where I can enable back and forth communication between two server sockets. The first iteration runs successfully and then there is one set of message passing that is possible. When it comes to second round of message passing I get the error. I feel there is some mistake in the IP Address but I could not resolve it.
I have looked here but could not find a solution
OSError: [Errno 99] Cannot assign requested address - py
Python - socket.error: Cannot assign requested address
Any help is deeply appreciated
This is Server 1:
import socket
import requests
host = "127.0.0.1"
#ip address
port_other_send = 5007
#Other's port while sending
port_own_send = 5006
#Our port for sending
port_other_recieve = 5009
#other port for recieving
port_own_recieve = 5008
#our port for recieving
#s = socket.socket()
#s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#s.bind(("", port_own))
#binds the socket element to the IP address and the Port
def main():
send()
def send():
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port_own_send))
s.connect((host,port_other_recieve))
message = input("Type message to be sent ")
while message != "q":
s.send(message.encode('utf-8'))
break
receive()
def receive():
print("This works yo")
socketva = socket.socket()
socketva.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
socketva.bind((host, port_own_recieve))
socketva.listen()
c,addr=socketva.accept()
x = True
while x ==True:
print("Connection from",(addr))
data = c.recv(1024)
print ("Data recieved ", str(data))
response= ("Data recieved")
c.send(data)
x = False
c.close
send()
if __name__ == "__main__":
main()
This is server 2:
import socket
import flask
host = "127.0.0.1"
#ip address
port_other_send= 5006
#Other's port while sending
port_own_send= 5007
#Our port for sending
port_other_recieve = 5008
#other port for recieving
port_own_recieve=5009
#our port for recieving
#binds the socket element to the IP address and the Port
def main():
receive()
def receive():
socketva = socket.socket()
socketva.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
socketva.bind((host, port_own_recieve))
socketva.listen()
c,addr=socketva.accept()
x = True
while x== True:
print("Connection from",(addr))
data = c.recv(1024)
print ("Data recieved: ",data)
response= ("Data recieved")
c.send(data)
x= False
c.close
send()
def send():
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port_own_send))
s.connect(("",port_other_recieve))
message = input("Enter reply message ")
while message != "q":
s.send(message.encode('utf-8'))
receive()
if __name__ == "__main__":
main()
Server 1 Output:
Type message to be sent hi
This works yo
Connection from ('127.0.0.1', 5007)
Data recieved b'hihihihihihihihihihihihihihihihihi'
Traceback (most recent call last):
File "Server_socket.py", line 100, in <module>
main()
File "Server_socket.py", line 57, in main
send()
File "Server_socket.py", line 68, in send
receive()
File "Server_socket.py", line 87, in receive
send()
File "Server_socket.py", line 63, in send
s.connect((host,port_other_recieve))
OSError: [Errno 99] Cannot assign requested address
Server 2 Output:
Connection from ('127.0.0.1', 5006)
Data recieved: b'hi'
Enter reply message hi
Traceback (most recent call last):
File "Server_socket2.py", line 54, in <module>
main()
File "Server_socket2.py", line 23, in main
receive()
File "Server_socket2.py", line 41, in receive
send()
File "Server_socket2.py", line 50, in send
s.send(message.encode('utf-8'))
ConnectionResetError: [Errno 104] Connection reset by peer