gpt4 book ai didi

Java服务器客户端网络

转载 作者:行者123 更新时间:2023-12-01 13:04:15 24 4
gpt4 key购买 nike

我应该在每次客户端 GUI 发送消息时打开-关闭连接,还是打开一次连接并保持打开状态?

我使用 BufferedWriter 发送数据,但这种方法似乎只工作一次,在完成写入和刷新方法之后,我只能向我的服务器发送 1 条消息,然后它就无法获取我发送的数据。 .

客户端连接 - 将数据发送到服务器(第一次) - 有效客户端再次发送数据 - 服务器控制台上没有回显数据。

我应该在每次要发送数据时打开一个 bufferedwriter 还是一直保持打开状态并且从不关闭它?

我的实际代码 - 客户端

主要

public class Main {

public static void main(String args[]) {

Client chat = new Client("localhost", 6111);
}
}

客户端

公共(public)类客户端{

    public Client(String host, int port) {

SwingUtilities.invokeLater(new Runnable() {

private Socket chat;

public void run() {

try {

this.chat = new Socket("localhost", 6113);

} catch ( IOException e) {

e.printStackTrace();
}

Gui gui = new Gui("Chat", this.chat);
}
});
}
}

图形用户界面

public class Gui {

private JFrame frame;
private JTextArea area;
private JTextField field;
private JMenuBar menu;
private JButton button;
private Socket socket;
private BufferedWriter write;

public Gui(String title, Socket socket) {

this.socket = socket;

try {

this.write = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));

} catch (IOException e) {

e.printStackTrace();
}

this.frame = new JFrame(title);
this.frame.setSize(new Dimension(400, 400));
this.frame.setLayout(new BorderLayout());
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setResizable(false);

Elements interfce = new Elements(this.frame);

this.field = interfce.addField("Texto to send...");
this.menu = interfce.addBar();
this.area = interfce.addArea();
this.button = interfce.addButton("Send");
this.button.addActionListener(new Listener(this.field, this.socket, this.write));
this.menu.add(this.field);
this.menu.add(this.button);
this.frame.setVisible(true);
}
}

监听器(发送数据)

public class Listener implements ActionListener {

private JTextField text;
private Socket chat;
private BufferedWriter writer;

public Listener(JTextField field, Socket chat, BufferedWriter write) {

this.text = field;
this.chat = chat;
this.writer = write;
}

public void actionPerformed(ActionEvent e) {

System.out.println(this.text.getText());

try {

writer.write("Hello\n");
writer.flush();

} catch (IOException e1) {

e1.printStackTrace();
}
}
}

还有服务器

public class Main {

public static void main(String args[]) {

Server chat = new Server(6113);
}
}

public class Server {

private int port;
private ServerSocket server;

public Server(int port) {

this.port = port;
startServer();
}

public void startServer() {

try {

this.server = new ServerSocket(this.port);
System.out.println("Starting chat server at port " + this.port + "...");

while (true) {

Socket s = this.server.accept();
System.out.println("Client connected - " + s.getLocalAddress().getHostName());
Thread client = new Thread(new Client(s));
client.start();
}

} catch (IOException e) {

System.out.println("Error found!");
e.printStackTrace();
}
}
}


public class Client implements Runnable {

private Socket client;

public Client(Socket client) {

this.client = client;
}

public void run() {

try {

BufferedReader read = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
System.out.println("Client says - " + read.readLine());

} catch (IOException e) {

e.printStackTrace();
}
}
}

最佳答案

Should I open - close the connection each time my client GUI sends a message or open a connection one time and keep it open ?

客户端和服务器之间的连接通常在整个消息传递 session 中打开,并且不会在发送每条消息时重新创建。因此,请保持其打开状态,直到客户端或服务器必须离开为止。

查看您在接受连接后在服务器中启动客户端线程。

public void run() {

try {

BufferedReader read = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
System.out.println("Client says - " + read.readLine());

} catch (IOException e) {

e.printStackTrace();
}
}

这会给你提示吗?好吧 - 您必须在 run() 中安排某种循环 - 监听来自该客户端的更多输入。

关于Java服务器客户端网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23352254/

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