简体   繁体   English

将datagramsocket绑定到inetaddress

[英]bind datagramsocket to inetaddress

When I use 当我使用

   serverSocket = serverChannel.socket();
   serverSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));

for tcp-based sockets, I get given the address 192.168.0.2, but when I use udp: 对于基于tcp的套接字,我得到的地址为192.168.0.2,但是当我使用udp时:

serverSocket = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 0));

I always get a null or 0.0.0.0 address binding. 我总是得到一个null或0.0.0.0地址绑定。 What is going on here exactly? 到底是怎么回事? I want the socket to be bound to 192.168.0.2 so that my other servers can communicate with it. 我希望将套接字绑定到192.168.0.2,以便我的其他服务器可以与其通信。

Runs for me. 为我跑。 What get you with this: 是什么让您如此:

InetSocketAddress in = new InetSocketAddress(InetAddress.
                getLocalHost(), 0);
System.err.println(in);
serverSocket = new DatagramSocket(in);

What is your OS ? 您的操作系统是什么?

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

相关问题 将DatagramSocket绑定到特定的InetAddress但任意端口 - Bind DatagramSocket to specific InetAddress but arbitrary port 如何将ServerSocket绑定到InetAddress? - How to bind a ServerSocket to an InetAddress? 如何将DatagramSocket绑定到Openshift中的特定端口 - How to bind DatagramSocket to particular port in Openshift Java DatagramSocket 绑定到两个连续的开放端口 - Java DatagramSocket bind to two consecutive open ports java.net.DatagramSocket绑定错误 - java.net.DatagramSocket Bind error 如何在绑定DatagramSocket类远程套接字地址 - How to bind remote socket address in DatagramSocket Android:java.net.DatagramSocket.bind:无效的参数异常 - Android: java.net.DatagramSocket.bind: Invalid Argument Exception 创建Java DatagramSocket对象时,“已使用的地址无法绑定”。 - “address already in use cannot bind” when creating a Java DatagramSocket object. java.net.BindException:绑定失败:尝试为UDP连接创建DatagramSocket时EACCES(权限被拒绝) - java.net.BindException: bind failed: EACCES (Permission denied) when trying to create DatagramSocket for UDP connection 有没有一种方法可以将DatagramSocket绑定到特定NIC上的任何端口,或者如果我指定了NIC,则必须指定端口? - Is there a way to bind a DatagramSocket to any port on a specific nic or if I specify the nic I have to specify the port?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM