当我尝试将Socket设置为255.255.255.255时抛出异常 [英] An exception throws when I try to make a Socket to 255.255.255.255

查看:140
本文介绍了当我尝试将Socket设置为255.255.255.255时抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次用java编程网络。 我想在一个小型网络中使用它。我一直在寻找一种方式,以某种方式发送到整个网络中的所有节点。让他们知道我的存在。有人告诉我发送数据包到255.255.255.255所以网络中的任何人都会收到它。

It's my first time programming network in java. I want to use it in a small network. I was looking for a way to send to somehow broadcast to all nodes in the whole networking. To let them know of my existence. Someone told me send data packets to 255.255.255.255 so anyone in the network will receive it.

所以我写了这段代码:

try{
    Socket socket= new Socket("255.255.255.255", 3550);
}catch(Exception e){
    System.out.println("oops! " + e.getMessage());
}

但不幸的是,它打印出来:

But, unfortunately it prints:

oops! Permission denied 

当我将255.255.255.255改为192.168.1.3时,这是我的伴侣IP地址,它工作正常。此外,当我将255.255.255.255更改为192.168.1.255时,根据ifconfig是我的广播地址,我得到一个具有相同消息的异常。

When I change "255.255.255.255" to "192.168.1.3", which is my mate's IP address, it works fine. Also when I change "255.255.255.255" to "192.168.1.255", which according to ifconfig is my broadcast address, I get an Exception with the same message.

I在一个adhoc网络中。

我的操作系统是MAC OS X 10.6

我的配对是在Windows Vista Home Premium Service Pack 1中。

I'm in a adhoc network.
My OS is MAC OS X 10.6
My mate is in Windows Vista Home Premium Service Pack 1.

请简单说明,我是新手:)

提前致谢。

推荐答案

Socket()创建一个流( TCP )套接字。您无法广播流。您需要一个数据报套接字( UDP ),因此您应该使用更专业的类 DatagramSocket() 而不是。

Socket() creates a stream (TCP) socket. You can't broadcast a stream. You need a datagram socket (UDP), so you should use the more specialized class DatagramSocket() instead.

这篇关于当我尝试将Socket设置为255.255.255.255时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆