简体   繁体   中英

How can I bind two sockets to the same port on two separate programs?

I have a server.java and a client.python file. When I try the following, however, I get a "[Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions" error. Is there a way around this? Why is this happening?
client.py

 sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
 sockRecv = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
 sock.bind((socket.gethostname(),4000))
 sockRecv.bind((socket.gethostname(),4000 + 1))

server.java

recvSocket = new DatagramSocket(4000);
sendSocket = new DatagramSocket(4000 + 1);

What your code is doing doesn't make sense, to me.

The IP address + port represents an end-point for datagram communication. If two applications were able to bind to the same end-point, which of them would receive the packets sent to the end-point? One of them? Both of them?

UDP is not a multi-cast protocol .... unless you bind to a multicast IP address.


Based on hints in your code (names of variables) I think you are trying to set up message passing between two applications on the same host. If so you should do this:

  • Application A binds to port P1 and sends messages to port P2
  • Application B binds to port P2 and sends messages to port P1.

There is no need for applications A and B bind to the same end-point; ie the same port ... to do what I think you are trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM