【发布时间】:2012-02-27 05:08:25
【问题描述】:
我正在尝试用 Java 构建一个聊天服务器-客户端程序。但问题是,在代码中的某个点之后,它会停止执行,对于我来说,我无法弄清楚为什么。我在这里附上代码。我是多线程和套接字编程的新手,所以错误可能很明显,但我完全错过了。
public class ChatClient implements Runnable
{ private Socket socket = null;
private Thread thread1 = null;
private ObjectOutputStream streamOut = null;
private ChatClientThread client = null;
private Message sendMsg = null;
private String username = null;
private DataInputStream console = null;
private Scanner s = new Scanner(System.in);
String text;
public ChatClient(String serverName, int serverPort)
{ System.out.println("Establishing connection. Please wait ...");
try
{ socket = new Socket(serverName, serverPort);
System.out.println("Connected: " + socket);
System.out.println("Enter your username:");
username = s.nextLine();
start();
}
catch(UnknownHostException uhe)
{ System.out.println("Host unknown: " + uhe.getMessage()); }
catch(IOException ioe)
{ System.out.println("Unexpected exception: " + ioe.getMessage()); }
}
public void run()
{ while (thread1 != null)
{ try
{ sendMsg = new Message();
sendMsg.setMsg(s.nextLine());
System.out.println(sendMsg.getMsg()+ " check");
streamOut.writeObject(sendMsg);
streamOut.flush();
}
catch(IOException ioe)
{ System.out.println("Sending error: " + ioe.getMessage());
stop();
}
}
}
public void handle(String user, Message msg)
{ System.out.println("1");
if (msg.getMsg().equals(".bye"))
{ System.out.println("Good bye. Press RETURN to exit ...");
stop();
}
else
System.out.println(msg.getMsg());
System.out.println("Msg received");
}
public void start() throws IOException
{
//console = new DataInputStream(System.in);
System.out.println("1");
streamOut = new ObjectOutputStream(socket.getOutputStream());
System.out.println("3");
if (thread1 == null)
{ client = new ChatClientThread(this, socket, username);
System.out.println("Started new ChatClientThread");
thread1 = new Thread(this);
thread1.start();
}
else
System.out.println("This code is stupid.");
}
public void stop()
{ if (thread1 != null)
{ thread1.stop();
thread1 = null;
}
try
{ if (console != null) console.close();
if (streamOut != null) streamOut.close();
if (socket != null) socket.close();
}
catch(IOException ioe)
{ System.out.println("Error closing ..."); }
//client.close();
client.stop();
}
public static void main(String args[])
{ ChatClient client = null;
//if (args.length != 2)
// System.out.println("Usage: java ChatClient host port");
//else
client = new ChatClient("localhost", 2008);
}
}
所以它的工作方式是,它从 main 函数开始,进入构造函数,接受用户名和所有内容,然后进入 start()。我假设 start 有效,因为它打印 1 和 3,但之后我继续输入文本,但它不会继续到下一点(我知道,因为它不打印“Started new ChatClientThread”)。 任何帮助,将不胜感激。我已经在这段代码上工作了好几个小时,但我无法弄清楚为什么执行会停在那里。
更新
我编辑了 ChatClient.start 代码
public void start() throws IOException
{
//console = new DataInputStream(System.in);
System.out.println("1");
streamOut = new ObjectOutputStream(socket.getOutputStream());
System.out.println("3");
if (thread1 == null)
{
System.out.println("Started new ChatClientThread");
client = new ChatClientThread(this, socket, username);
System.out.println("Started new ChatClientThread");
thread1 = new Thread(this);
thread1.start();
}
else
System.out.println("This code is stupid.");
}
我现在知道它确实运行了 ChatClientThread 的构造函数:
public ChatClientThread(ChatClient _client, Socket _socket, String uname)
{ System.out.println("Constructor started");
client = _client;
socket = _socket;
username = uname;
System.out.println("1");
open();
System.out.println("2");
start();
System.out.println("3");
}
打印 1,继续 ChatClientThread.open:
public void open()
{ try
{ streamIn = new ObjectInputStream(socket.getInputStream());
}
catch(IOException ioe)
{ System.out.println("Error getting input stream: " + ioe);
client.stop();
}
}
但这里又卡住了。它不会继续打印 2,所以我假设它不会移动到 ChatClientThread.start 代码段。
【问题讨论】:
-
ChatClientThread 的代码在哪里?
-
作为参考,这是一个有效的example。
-
另外,从构造函数启动线程是不好的做法。构建您的客户端,然后启动它。但我不明白为什么你需要一个单独的线程。为什么不在主线程中做所有事情。你没有在 Thread.stop() 上看到过大的弃用警告吗?
-
Joe Daley - 我会添加它。我得到了一些新东西。 JB - 嗯,我正在将通过聊天发送文本的代码转换为发送对象的代码。发送文本与停止一起工作,所以我不知道用什么代替。 ://
标签: java multithreading sockets