当前位置: 技术问答>java相关
一个多点发送的应用程序,可是本能接收???
来源: 互联网 发布时间:2015-07-29
本文导语: 我从书中找了一个多点发送的例子,发送方可以工作可是在接收时却有问题,请大家帮忙看看。 发送: import java.net.*; import java.io.*; class MultiCastSender{ private static final byte TTL = 1; private static fina...
我从书中找了一个多点发送的例子,发送方可以工作可是在接收时却有问题,请大家帮忙看看。
发送:
import java.net.*;
import java.io.*;
class MultiCastSender{
private static final byte TTL = 1;
private static final int DATAGRAM_BYTES = 512;
private int mcastPort;
private InetAddress mcastIP;
private BufferedReader input;
private MulticastSocket mcastSocket;
public static void main(String[] args) {
if (args.length != 2) {
System.out.print("Usage:MultiCastSender" + " nt can be one of 224.x.x.x" +"-239.x.x.xn");
System.exit(1);
}
MultiCastSender send = new MultiCastSender(args);
System.exit(0);
}
public MultiCastSender(String[] args) {
DatagramPacket mcastPacket;
String nextLine;
byte[] mcastBuffer;
byte[] lineData;
int sendLength;
input = new BufferedReader(new InputStreamReader(System.in));
try {
mcastIP = InetAddress.getByName(args[0]);
mcastPort = Integer.parseInt(args[1]);
mcastSocket = new MulticastSocket();
} catch(IOException excpt) {
System.err.println("Unable to obtain scoket :" +excpt);
System.exit(1);
}
try {
while((nextLine = input.readLine()) != null) {
mcastBuffer = new byte[DATAGRAM_BYTES];
if (nextLine.length() > mcastBuffer.length) {
sendLength = mcastBuffer.length;
}else{
sendLength = nextLine.length();
}
lineData = nextLine.getBytes();
for (int i = 0; i javac MultiCastReceiver.java
MultiCastReceiver.java:30: unreported exception java.io.IOException; must be cau
ght or declared to be thrown
MulticastSocket mcastSocket = new MulticastSocket(mcastPort); //3
0
^
MultiCastReceiver.java:32: unreported exception java.io.IOException; must be cau
ght or declared to be thrown
mcastSocket.joinGroup(mcastIP);
^
2 errors
这是为什么呢?
发送:
import java.net.*;
import java.io.*;
class MultiCastSender{
private static final byte TTL = 1;
private static final int DATAGRAM_BYTES = 512;
private int mcastPort;
private InetAddress mcastIP;
private BufferedReader input;
private MulticastSocket mcastSocket;
public static void main(String[] args) {
if (args.length != 2) {
System.out.print("Usage:MultiCastSender" + " nt can be one of 224.x.x.x" +"-239.x.x.xn");
System.exit(1);
}
MultiCastSender send = new MultiCastSender(args);
System.exit(0);
}
public MultiCastSender(String[] args) {
DatagramPacket mcastPacket;
String nextLine;
byte[] mcastBuffer;
byte[] lineData;
int sendLength;
input = new BufferedReader(new InputStreamReader(System.in));
try {
mcastIP = InetAddress.getByName(args[0]);
mcastPort = Integer.parseInt(args[1]);
mcastSocket = new MulticastSocket();
} catch(IOException excpt) {
System.err.println("Unable to obtain scoket :" +excpt);
System.exit(1);
}
try {
while((nextLine = input.readLine()) != null) {
mcastBuffer = new byte[DATAGRAM_BYTES];
if (nextLine.length() > mcastBuffer.length) {
sendLength = mcastBuffer.length;
}else{
sendLength = nextLine.length();
}
lineData = nextLine.getBytes();
for (int i = 0; i javac MultiCastReceiver.java
MultiCastReceiver.java:30: unreported exception java.io.IOException; must be cau
ght or declared to be thrown
MulticastSocket mcastSocket = new MulticastSocket(mcastPort); //3
0
^
MultiCastReceiver.java:32: unreported exception java.io.IOException; must be cau
ght or declared to be thrown
mcastSocket.joinGroup(mcastIP);
^
2 errors
这是为什么呢?
|
MulticastSocket mcastSocket = new MulticastSocket(mcastPort);可能会抛出java.io.IOException异常,你自己必须捕捉。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。