我正在尝试研究多客户端聊天服务器。由于我是Java的新手,我无法在Net Bean中运行这两个.java文件。我有两个java项目,并将这些文件放在它们下面。服务器项目成功运行,但客户端项目显示:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at chatClient.main(chatClient.java:73)
客户项目
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
class chatClient extends Frame implements Runnable {
Socket soc;
TextField tf;
TextArea ta;
Button btnSend, btnClose;
String sendTo;
String LoginName;
Thread t = null;
DataOutputStream dout;
DataInputStream din;
chatClient(String LoginName, String chatwith) throws Exception {
super(LoginName);
this.LoginName = LoginName;
sendTo = chatwith;
tf = new TextField(50);
ta = new TextArea(50, 50);
btnSend = new Button("Send");
btnClose = new Button("Close");
soc = new Socket("127.0.0.1", 5217);
din = new DataInputStream(soc.getInputStream());
dout = new DataOutputStream(soc.getOutputStream());
dout.writeUTF(LoginName);
t = new Thread(this);
t.start();
}
void setup() {
setSize(600, 400);
setLayout(new GridLayout(2, 1));
add(ta);
Panel p = new Panel();
p.add(tf);
p.add(btnSend);
p.add(btnClose);
add(p);
show();
}
public boolean action(Event e, Object o) {
if (e.arg.equals("Send")) {
try {
dout.writeUTF(sendTo + " " + "DATA" + " " + tf.getText().toString());
ta.append("\n" + LoginName + " Says:" + tf.getText().toString());
tf.setText("");
} catch (Exception ex) {
}
} else if (e.arg.equals("Close")) {
try {
dout.writeUTF(LoginName + " LOGOUT");
System.exit(1);
} catch (Exception ex) {
}
}
return super.action(e, o);
}
public static void main(String args[]) throws Exception {
chatClient Client1 = new chatClient(args[0],args[1]);
Client1.setup();
}
public void run() {
while (true) {
try {
ta.append("\n" + sendTo + " Says :" + din.readUTF());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
服务器项目
import java.net.*;
import java.util.*;
import java.io.*;
class chatServer {
static Vector ClientSockets;
static Vector LoginNames;
chatServer() throws Exception {
ServerSocket soc = new ServerSocket(5217);
ClientSockets = new Vector();
LoginNames = new Vector();
while (true) {
Socket CSoc = soc.accept();
AcceptClient obClient = new AcceptClient(CSoc);
}
}
public static void main(String args[]) throws Exception {
chatServer ob = new chatServer();
}
class AcceptClient extends Thread {
Socket ClientSocket;
DataInputStream din;
DataOutputStream dout;
AcceptClient(Socket CSoc) throws Exception {
ClientSocket = CSoc;
din = new DataInputStream(ClientSocket.getInputStream());
dout = new DataOutputStream(ClientSocket.getOutputStream());
String LoginName = din.readUTF();
System.out.println("User Logged In :" + LoginName);
LoginNames.add(LoginName);
ClientSockets.add(ClientSocket);
start();
}
public void run() {
while (true) {
try {
String msgFromClient = new String();
msgFromClient = din.readUTF();
StringTokenizer st = new StringTokenizer(msgFromClient);
String Sendto = st.nextToken();
String MsgType = st.nextToken();
int iCount = 0;
if (MsgType.equals("LOGOUT")) {
for (iCount = 0; iCount < LoginNames.size(); iCount++) {
if (LoginNames.elementAt(iCount).equals(Sendto)) {
LoginNames.removeElementAt(iCount);
ClientSockets.removeElementAt(iCount);
System.out.println("User " + Sendto + " Logged Out ...");
break;
}
}
} else {
String msg = "";
while (st.hasMoreTokens()) {
msg = msg + " " + st.nextToken();
}
for (iCount = 0; iCount < LoginNames.size(); iCount++) {
if (LoginNames.elementAt(iCount).equals(Sendto)) {
Socket tSoc = (Socket) ClientSockets.elementAt(iCount);
DataOutputStream tdout = new DataOutputStream(tSoc.getOutputStream());
tdout.writeUTF(msg);
break;
}
}
if (iCount == LoginNames.size()) {
dout.writeUTF("I am offline");
} else {
}
}
if (MsgType.equals("LOGOUT")) {
break;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
提前致谢。
答案 0 :(得分:1)
如果你看一下你客户的构造函数,你会发现它需要两个参数:
chatClient(String LoginName, String chatwith)
登录并与谁聊天。
我认为您没有指定它们,因此以下行会抛出异常,因为没有任何异常且args
为空:
chatClient Client1 = new chatClient(args[0],args[1]);
答案 1 :(得分:0)
从命令行传递的java参数实际上是String数组。如果你没有在命令行中键入任何内容(或者等效的NetBeans,你将得到一个ArrrayIndexOutOfBoundsException。因此你得到一个例外,因为你试图将一个字符串数组传递给chatClient方法,而实际上没有任何参数。