gpt4 book ai didi

java - 创建客户端-服务器应用程序以回显用户发送的内容

转载 作者:行者123 更新时间:2023-11-30 11:36:59 25 4
gpt4 key购买 nike

我正在创建一个简单的客户端服务器应用程序,其中有一个 GUI 客户端,用户可以在其中输入一些文本,服务器会将文本连同时间戳发回。
enter image description here

问题是每当我点击Echo 按钮时,我都会收到Connection Reset 错误消息。我不知道为什么会这样。
这是代码:

服务器

package echo;
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;

public class Server extends Thread{

final int PORT = 444;
ServerSocket serverSocket;
Socket socket;
InputStreamReader ir;
BufferedReader b;
PrintStream p;
Date currentTime;
Format fmt;

//------------------------------------------------------------------------------
public static void main(String[] args) {
Server s = new Server();
s.start();
}
//------------------------------------------------------------------------------
public void setupConnection(){
try{
serverSocket = new ServerSocket(PORT);
socket = serverSocket.accept();

ir = new InputStreamReader(socket.getInputStream());
b = new BufferedReader(ir);

p = new PrintStream(socket.getOutputStream());
fmt = DateFormat.getDateTimeInstance();

}catch(Exception e){
e.printStackTrace();
}
}
//------------------------------------------------------------------------------

public Server(){


}

//------------------------------------------------------------------------------
@Override
public void run(){
setupConnection();
if(socket!=null){
try {
String message = b.readLine();
if(message!=null){
p.println(fmt.format(new Date()) + " " + message);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

客户端

package echo;
import java.net.*;
import java.io.*;

import javax.swing.*;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;

public class Client extends JFrame{

final int PORT = 444;
Socket s;

InputStreamReader ir;
BufferedReader b;
PrintStream p;

JTextArea textArea;
JTextField field;
JScrollPane pane;
JButton echo;


//------------------------------------------------------------------------------
public static void main(String[] args) {
new Client();
}
//------------------------------------------------------------------------------
public Client(){
setupConnection();
setupGUI();
addListeners();
}
//------------------------------------------------------------------------------

public void setupConnection(){
try {
s = new Socket("localhost",PORT);
ir = new InputStreamReader(s.getInputStream());
b = new BufferedReader(ir);
p = new PrintStream(s.getOutputStream());

p.println("User Logged In");

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//------------------------------------------------------------------------------
public void setupGUI(){
setLayout(new GridBagLayout());
textArea = new JTextArea(30,30);
field = new JTextField(10);
pane = new JScrollPane(textArea);
echo = new JButton("Echo");

GridBagConstraints gbc = new GridBagConstraints();
textArea.setBorder(BorderFactory.createTitledBorder("Replies from server: "));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 5;
gbc.gridheight = 5;
add(pane,gbc);

gbc.gridy = 5;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(field,gbc);

field.setBorder(BorderFactory.createTitledBorder("Enter text here:"));
gbc.gridy = 6;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(echo,gbc);

setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
//------------------------------------------------------------------------------
public void addListeners(){
echo.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
String message = field.getText();
field.setText("");
p.println(message);
try {
String reply = b.readLine();
if(reply!=null){
textArea.append(reply);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println();
}
});
}
//------------------------------------------------------------------------------
}

你能帮我解决这个问题吗?

最佳答案

在服务器运行 () 中,您需要有一个 while 循环,只有在您的客户端说“关闭此连接”后才会中断。现在发生的事情是您的服务器正在等待数据,客户端收到数据并退出(readline)。

这个异常(exception)是正确的,如果你想的话:)。

关于java - 创建客户端-服务器应用程序以回显用户发送的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14277368/

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