gpt4 book ai didi

java - 客户端向服务器发送消息。但服务器不显示它

转载 作者:行者123 更新时间:2023-12-02 06:38:28 25 4
gpt4 key购买 nike

这里我创建了一个充当客户端的 GUI。它包含一个文本字段和一个按钮。我的目的是打印用户在文本字段中键入的任何内容必须在服务器命令提示符中显示。

服务器.java

import java.io.*;
import java.net.*;

public class Server {

Server(){
try{
ServerSocket server = new ServerSocket(4040);


while(true){
Socket sock = server.accept();
InputStream is = sock.getInputStream();
InputStreamReader streamReader = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(streamReader);
String string = reader.readLine();
System.out.println(string);
reader.close();

}
}
catch(IOException ex)
{
ex.printStackTrace();
}

}



public static void main(String args[]){
new Server();
}

}

SimpleChatClientA.java

import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;

public class SimpleChatClientA {
JTextField outgoing;
PrintWriter writer;
Socket sock;
public void go(){
JFrame frame = new JFrame("chat client");
outgoing = new JTextField(20);
JButton sendButton = new JButton("send");
frame.add(outgoing);
frame.add(sendButton);
sendButton.addActionListener(new SendButtonListener());
frame.setLayout(new FlowLayout());
frame.setSize(500,100);
setUpNetworking();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setUpNetworking(){
try {
Socket sock = new Socket("localhost",4040);
PrintWriter writer = new PrintWriter(sock.getOutputStream());
System.out.println("connection established");
} catch (Exception e) {
e.printStackTrace();
}

}

public class SendButtonListener implements ActionListener{

public void actionPerformed(ActionEvent e) {
try {
String str = outgoing.getText();
writer.println(str);
writer.flush();
}
catch (Exception ex) {
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}

}
public static void main(String args[]){
SimpleChatClientA simple = new SimpleChatClientA();
simple.go();
}

}

当我运行这个程序时。连接建立。但我看不到在服务器命令提示符的 JTextField 中键入的消息。我看到发生了一些错误。我不明白那是什么。我是java新手。请帮助我

最佳答案

问题似乎是,您正在创建编写器并存储在局部变量中。

private void setUpNetworking(){
try {
Socket sock = new Socket("localhost",4040);
// PROBLEM IS HERE
PrintWriter writer = new PrintWriter(sock.getOutputStream());
System.out.println("connection established");
} catch (Exception e) {
e.printStackTrace();
}

}

试试这个:

private void setUpNetworking(){
try {
Socket sock = new Socket("localhost",4040);
// STORE WRITER IN INSTANCE VARIABLE
writer = new PrintWriter(sock.getOutputStream());
System.out.println("connection established");
} catch (Exception e) {
e.printStackTrace();
}

}

您一定遇到了 NullPointerException,因为您还没有创建 writer 对象。

public void actionPerformed(ActionEvent e) {
try {
String str = outgoing.getText();
// HERE YOU WILL GET EXCEPTION AS WRITER (instace variable) IS NOT CREATED
writer.println(str);
writer.flush();
}
catch (Exception ex) {
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}

关于java - 客户端向服务器发送消息。但服务器不显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19397921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com