如何使示例 MulticastChat 可操作?
How to get the example MulticastChat operational?
到目前为止,我已经定义了端口号和inetaddress,但我仍然不明白为什么我无法获得下面的示例来呈现GUI?如果社区中有人可以指出我所犯的错误,我将不胜感激。谢谢。
下面是 GUI 格式的多播完整程序的源代码,它使用对等网络实现,通过使用 TCP 协议传递消息,并使用同步方法实现多线程,将消息发送到所有连接的对等点迄今。
/* Multicast Peer to Peer chat application that uses TCP protocol to chat
* to two users at a time or adding more clients could be developed into
* a group chat communicating amongst multiple members of the same chat.
*
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class MulticastChat implements Runnable, WindowListener, ActionListener {
protected InetAddress group;
protected int port = 2343;
public MulticastChat (InetAddress group, int port) {
this.group = group;
this.port = port;
initAWT();
}
protected Frame frame;
protected TextArea output;
protected TextField input;
private static final Logger logger =
Logger.getLogger(MulticastChat.class.getName());
protected void initAWT () {
frame = new Frame
("MulticastChat [" + group.getHostAddress () + ":" + port + "]");
frame.addWindowListener (this);
output = new TextArea ();
output.setEditable (false);
input = new TextField ();
input.addActionListener (this);
frame.setLayout (new BorderLayout ());
frame.add (output, "Center");
frame.add (input, "South");
frame.pack ();
}
protected Thread listener;
public synchronized void start() throws IOException {
if (listener == null) {
initNet ();
listener = new Thread (this);
listener.start ();
frame.setVisible (true);
}
}
protected MulticastSocket socket;
protected DatagramPacket outgoing, incoming;
protected void initNet () throws IOException {
socket = new MulticastSocket (port);
socket.setTimeToLive (5);
socket.joinGroup (group);
outgoing = new DatagramPacket (new byte[1], 1, group, port);
incoming = new DatagramPacket (new byte[65508], 65508);
}
public synchronized void stop () throws IOException {
frame.setVisible (false);
if (listener != null) {
listener.interrupt ();
listener = null;
try {
socket.leaveGroup (group);
} finally {
socket.close ();
}
}
}
@Override
public void windowOpened (WindowEvent event) {
input.requestFocus ();
}
@Override
public void windowClosing (WindowEvent event) {
try {
stop ();
} catch (IOException ex) {
logger.log(Level.WARNING,"WindowClosing IOException",ex);
}
}
@Override
public void windowClosed (WindowEvent event) {}
@Override
public void windowIconified (WindowEvent event) {}
@Override
public void windowDeiconified (WindowEvent event) {}
@Override
public void windowActivated (WindowEvent event) {}
@Override
public void windowDeactivated (WindowEvent event) {}
@Override
public void actionPerformed (ActionEvent event) {
try {
byte[] utf = event.getActionCommand ().getBytes ("UTF8");
outgoing.setData (utf);
outgoing.setLength (utf.length);
socket.send (outgoing);
input.setText ("");
} catch (IOException ex) {
handleIOException (ex);
}
}
protected synchronized void handleIOException (IOException ex) {
if (listener != null) {
output.append (ex + "\n");
input.setVisible (false);
frame.validate ();
if (listener != Thread.currentThread ())
listener.interrupt ();
listener = null;
try {
socket.leaveGroup (group);
} catch (IOException ignored) {
}
socket.close ();
}
}
@Override
public void run () {
try {
while (!Thread.interrupted ()) {
incoming.setLength (incoming.getData ().length);
socket.receive (incoming);
String message = new String
(incoming.getData (), 0, incoming.getLength (), "UTF8");
output.append (message + "\n");
}
} catch (IOException ex) {
handleIOException (ex);
}
}
// Unfortunately the example given didn't have any comments for me to try
// to understand how to get the example operating.
public static void main (String[] args) throws IOException {
if ((args.length != 1) || (!args[0].contains(":"))){
throw new IllegalArgumentException
("Syntax: MulticastChat <group>:<port>");
} else {
int idx = args[0].indexOf (":");
InetAddress group = InetAddress.getByName (args[0].substring (0, idx));
int port = Integer.parseInt (args[0].substring (idx + 1));
MulticastChat chat = new MulticastChat (group, port);
chat.start();
}
}
}
这是出现的错误:
Exception in thread "main" java.lang.IllegalArgumentException: Syntax: MulticastChat <group>:<port>
at MulticastChat.main(MulticastChat.java:167)
按如下方式更改您的主要方法
public static void main(String[] args) throws IOException {
// Define default host name
String host = "228.5.6.7";
// Define default port
int port = 8804;
if ((args.length != 1) || (!args[0].contains(":"))) {
// throw new IllegalArgumentException("Syntax: MulticastChat <group>:<port>");
} else {
int idx = args[0].indexOf(":");
host = args[0].substring(0, idx);
port = Integer.parseInt(args[0].substring(idx + 1));
}
InetAddress group = InetAddress.getByName(host);
MulticastChat chat = new MulticastChat(group, port);
chat.start();
}
到目前为止,我已经定义了端口号和inetaddress,但我仍然不明白为什么我无法获得下面的示例来呈现GUI?如果社区中有人可以指出我所犯的错误,我将不胜感激。谢谢。
下面是 GUI 格式的多播完整程序的源代码,它使用对等网络实现,通过使用 TCP 协议传递消息,并使用同步方法实现多线程,将消息发送到所有连接的对等点迄今。
/* Multicast Peer to Peer chat application that uses TCP protocol to chat
* to two users at a time or adding more clients could be developed into
* a group chat communicating amongst multiple members of the same chat.
*
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class MulticastChat implements Runnable, WindowListener, ActionListener {
protected InetAddress group;
protected int port = 2343;
public MulticastChat (InetAddress group, int port) {
this.group = group;
this.port = port;
initAWT();
}
protected Frame frame;
protected TextArea output;
protected TextField input;
private static final Logger logger =
Logger.getLogger(MulticastChat.class.getName());
protected void initAWT () {
frame = new Frame
("MulticastChat [" + group.getHostAddress () + ":" + port + "]");
frame.addWindowListener (this);
output = new TextArea ();
output.setEditable (false);
input = new TextField ();
input.addActionListener (this);
frame.setLayout (new BorderLayout ());
frame.add (output, "Center");
frame.add (input, "South");
frame.pack ();
}
protected Thread listener;
public synchronized void start() throws IOException {
if (listener == null) {
initNet ();
listener = new Thread (this);
listener.start ();
frame.setVisible (true);
}
}
protected MulticastSocket socket;
protected DatagramPacket outgoing, incoming;
protected void initNet () throws IOException {
socket = new MulticastSocket (port);
socket.setTimeToLive (5);
socket.joinGroup (group);
outgoing = new DatagramPacket (new byte[1], 1, group, port);
incoming = new DatagramPacket (new byte[65508], 65508);
}
public synchronized void stop () throws IOException {
frame.setVisible (false);
if (listener != null) {
listener.interrupt ();
listener = null;
try {
socket.leaveGroup (group);
} finally {
socket.close ();
}
}
}
@Override
public void windowOpened (WindowEvent event) {
input.requestFocus ();
}
@Override
public void windowClosing (WindowEvent event) {
try {
stop ();
} catch (IOException ex) {
logger.log(Level.WARNING,"WindowClosing IOException",ex);
}
}
@Override
public void windowClosed (WindowEvent event) {}
@Override
public void windowIconified (WindowEvent event) {}
@Override
public void windowDeiconified (WindowEvent event) {}
@Override
public void windowActivated (WindowEvent event) {}
@Override
public void windowDeactivated (WindowEvent event) {}
@Override
public void actionPerformed (ActionEvent event) {
try {
byte[] utf = event.getActionCommand ().getBytes ("UTF8");
outgoing.setData (utf);
outgoing.setLength (utf.length);
socket.send (outgoing);
input.setText ("");
} catch (IOException ex) {
handleIOException (ex);
}
}
protected synchronized void handleIOException (IOException ex) {
if (listener != null) {
output.append (ex + "\n");
input.setVisible (false);
frame.validate ();
if (listener != Thread.currentThread ())
listener.interrupt ();
listener = null;
try {
socket.leaveGroup (group);
} catch (IOException ignored) {
}
socket.close ();
}
}
@Override
public void run () {
try {
while (!Thread.interrupted ()) {
incoming.setLength (incoming.getData ().length);
socket.receive (incoming);
String message = new String
(incoming.getData (), 0, incoming.getLength (), "UTF8");
output.append (message + "\n");
}
} catch (IOException ex) {
handleIOException (ex);
}
}
// Unfortunately the example given didn't have any comments for me to try
// to understand how to get the example operating.
public static void main (String[] args) throws IOException {
if ((args.length != 1) || (!args[0].contains(":"))){
throw new IllegalArgumentException
("Syntax: MulticastChat <group>:<port>");
} else {
int idx = args[0].indexOf (":");
InetAddress group = InetAddress.getByName (args[0].substring (0, idx));
int port = Integer.parseInt (args[0].substring (idx + 1));
MulticastChat chat = new MulticastChat (group, port);
chat.start();
}
}
}
这是出现的错误:
Exception in thread "main" java.lang.IllegalArgumentException: Syntax: MulticastChat <group>:<port>
at MulticastChat.main(MulticastChat.java:167)
按如下方式更改您的主要方法
public static void main(String[] args) throws IOException {
// Define default host name
String host = "228.5.6.7";
// Define default port
int port = 8804;
if ((args.length != 1) || (!args[0].contains(":"))) {
// throw new IllegalArgumentException("Syntax: MulticastChat <group>:<port>");
} else {
int idx = args[0].indexOf(":");
host = args[0].substring(0, idx);
port = Integer.parseInt(args[0].substring(idx + 1));
}
InetAddress group = InetAddress.getByName(host);
MulticastChat chat = new MulticastChat(group, port);
chat.start();
}