我试图创建一个简单的套接字程序,在其中可以启用两个服务器套接字之间的来回通信。第一次迭代成功运行,然后可能发生一组消息传递。当涉及第二轮消息传递时,我得到了错误。 我觉得IP地址存在一些错误,但我无法解决。
我看过这里,但是找不到解决方法
OSError: [Errno 99] Cannot assign requested address - py
Python - socket.error: Cannot assign requested address
任何帮助深表感谢
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()
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()
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
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