简体   繁体   English

如何在两个单独的程序上将两个套接字绑定到同一端口?

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

I have a server.java and a client.python file. 我有一个server.java和一个client.python文件。 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. 但是,当我尝试以下操作时,出现“ [Errno 10013]尝试以一种受其访问权限禁止的方式访问套接字”的错误。 Is there a way around this? 有没有解决的办法? Why is this happening? 为什么会这样呢?
client.py 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 服务器.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. IP地址+端口表示数据报通信的端点。 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. UDP不是多播协议....除非您绑定到多播IP地址。


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 应用程序A绑定到端口P1并将消息发送到端口P2
  • Application B binds to port P2 and sends messages to port P1. 应用程序B绑定到端口P2并将消息发送到端口P1。

There is no need for applications A and B bind to the same end-point; 无需将应用程序A和B绑定到同一端点; ie the same port ... to do what I think you are trying to do. 即相同的端口...做我想你想做的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java DatagramSockets,我可以将两个不同的套接字连接到同一本地端口但在不同的本地地址吗? - With Java DatagramSockets, can I connect two different sockets to the same local port but on different local addresses? 系统中的两个不同的 UDP 套接字可以绑定同一个端口吗? - Can two different UDP socket in a system bind same port? 如何启动 2 个在同一端口上侦听的 Java Sockets? - How can I initiate 2 Java Sockets that listen on the same port? 套接字 - 在没有端口转发的情况下连接同一网络上的两台计算机? - Sockets - Connect two computers on the same network with no port forwarding? 当两个单独的模式具有相同的名称空间时,如何告诉JAXB在单独的程序包中生成类? - How can I tell JAXB to generate classes in separate packages when two separate schemas have the same namespace? 如何通过两个程序共享这个相同的lib实例 - how to share this same instance of lib by two programs 我如何将两个子字符串绑定到一个? - How i can bind two substring to one? 我可以在单线程中使用两个套接字吗? - Can I use two sockets in single thread? 如何将两个对象字段作为同一类中的单独方法进行比较 - how can I compare two object fields as a separate method within the same class 如何将两个单独的项目(客户端和服务器端)部署到同一Google App Engine项目? - How can i deploy two separate projects (client and server side) to the same Google App Engine project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM