我正在为一个类的聊天客户端工作,并遇到一些我似乎无法找到的问题。在ChatWindow和EchoServer中,所有的系统打印行都工作正常,除了一个,它永远不会“服务器响应”。每次它尝试发送到服务器时,它都会按照正确的方式打印出应该发送的内容,但服务器从不接收任何内容。有没有人有任何想法,我在这里遇到什么?请注意,我没有从编译器或运行时收到任何错误。Java聊天客户端和服务器没有收到
还是新的到stackoverflow,请让我知道,如果有什么我可以添加到这里来帮助。
编辑1:仍然有相同的问题,但在EchoServer中的循环中添加以修复连接到客户端后立即关闭服务器的错误。
编辑2:我发现套接字正在关闭我,这就是为什么服务器断开连接,我现在无法弄清楚为什么套接字可能会关闭。我的断点,我在进入新创建的线程后立即在构造函数中找到一个封闭的套接字。
编辑3:进行了必要的修理,代码现在正常工作。
// Simple server to receive communication from a client on the same computer and echo it back
public class EchoServer {
public static void main(String[] args) throws IOException {
try (ServerSocket s = new ServerSocket(4688)) {
// wait for client connection
try (Socket incoming = s.accept()) {
System.out.println("client connected");
try (Scanner in = new Scanner(incoming.getInputStream())) {
PrintWriter out = new PrintWriter(incoming.getOutputStream());
out.println("Hello! Enter BYE to exit.");
out.flush();
System.out.println("sent message to client");
// echo client input
boolean done = false;
while (true) {
while (!done && in.hasNextLine()) {
String line = in.nextLine();
System.out.println(line);
out.println("Echo: " + line);
out.flush();
if (line.trim().equals("BYE")) done = true;
}
if(done) break;
}
}
}
}
}
}
// Client main class. runs next class
public class ChatProgram {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame frame = new ChatWindow();
frame.setTitle("Chat Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(ChatProgram.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}
// accepts user input from a textField when button is pressed.
// gives it to the server and prints it in it's own textArea
//
public class ChatWindow extends JFrame {
private String username = "";
private Scanner in;
private PrintWriter out;
private Socket s;
public ChatWindow() throws IOException {
initComponents(); // this initializes the gui
try {
s = new Socket("localhost", 4688);
System.out.println("Connected to server");
in = new Scanner(s.getInputStream());
out = new PrintWriter(s.getOutputStream());
class PollServer implements Runnable {
@Override
public void run(){
while(true){
if(in.hasNextLine()){
System.out.println("server responded");
String input = in.nextLine();
PrintToWindow(input);
}
}
}
}
catch(IOException io) {
//I'll do something with this
}
Thread t1 = new Thread(new PollServer());
t1.start();
}
}
// Connect will be used later
public void Connect() {
SendToServer("connect " + username);
}
// Disconnect will be used later
public void Disconnect() {
SendToServer("Disconnect " + username);
}
// Handles all outbound messages to the server
public void SendToServer(String clientOut) {
out.println(clientOut);
PrintToWindow(clientOut);
System.out.println("sending to server: " + clientOut); // Program hits this and prints correctly every time
out.flush();
}
// Handles adding anything to the client's textArea. Will be implementing Synchronized later
public void PrintToWindow(String clientIn) {
textArea.append(clientIn + "\n");
}
}
我不前尽可能确保负面影响(尽管JVM无论如何优化它都不会令我感到惊讶),但是您不需要像这样在构造函数中定义“PollServer”类。如果你将它提取到一个单独的类,它会使代码更清洁。 – 2014-11-24 02:07:57
不幸的是只有那里作为我的项目的需求 – 2014-11-24 02:15:50
然后采取不同的课程。那太糟了。 – csmckelvey 2014-11-24 02:34:59