java - 如何使示例 MulticastChat 可操作?
问题描述
到目前为止,我已经定义了端口号和 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();
}
推荐阅读
- verilog - Quartus 2 - 无输出依赖于输入/输出引脚卡住
- ios - 如何在 ios swift 中从原型单元获取数据到控制器并从中更新模型
- php - 如何在 Laravel 5.7 中限制数据输入到数据库
- sumologic - Sumologic 仪表板配置为代码
- c++ - 数据未正确发送到缓冲区?
- postgresql - 使用 gorm 更新 postgres 表
- python - 使用 Python 将某些属性从 xml 文件复制到另一个文件
- python - 在 python 中导入 VLC 模块时出现属性错误
- r - 删除有条件的重复项
- java - 为什么这个消费者抱怨编译时错误?