我在java中创建了一个带GUI的简单聊天服务器,并在创建我尝试的客户端之前对其进行测试 telnet localhost 8888。 它已连接,但只要我在cmd中输入任何内容,连接就会结束? 这是服务器代码: package chat.application;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChatServer extends JFrame{
private JTextField userText;
private final JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
public ChatServer(){
super("Chat messenger");
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));
setSize(300,150);
setVisible(true);
}
// setting up server
public void startRunning(){
try{
server = new ServerSocket(8888, 32);
// if 8888 doesn't work try 6789
// 32 is backlog- how people can speak
while(true){
// run forever
try{
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Server ended the connection");
}finally{
closeSocket();
}
}
}catch(IOException ioException){
System.out.println("Error, Oh no!");
}
}
private void waitForConnection() throws IOException{
showMessage("Waiting for a connection...\n");
connection = server.accept();
showMessage("Connected to "
+ connection.getInetAddress().getHostName()+ " so you can start talking now!");
}
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are set up. \n");
}
private void whileChatting() throws IOException{
String message = "Connected ";
sendMessage(message);
ableToType(true);
do{
try{
message = (String) input.readObject();
showMessage("\n" + message);
}catch(ClassNotFoundException classNotFoundException){
showMessage("\n error");
}
}while(!message.equals("CLIENT - END"));
// if user types end program terminates
}
private void closeSocket(){
// close streams, sockets when done.
showMessage("\n Closing connections... \n");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
System.out.println("Error");
}
}
private void sendMessage(String message){
// sends message to client
try{
output.writeObject("SERVER - " + message);
output.flush();
showMessage("\n SERVER - " + message);
}catch(IOException ioException){
chatWindow.append("\n ERROR: dont do that okay! \n");
}
}
private void showMessage(final String text){
// updates chat window
SwingUtilities.invokeLater(
new Runnable(){
@Override
public void run(){
chatWindow.append(text);
}
}
);
}
private void ableToType(final boolean tr_or_flse){
SwingUtilities.invokeLater(
new Runnable(){
@Override
public void run(){
}
}
);
}
}
这里是主方法的类:
package chat.application;
import javax.swing.JFrame;
public class ServerTest {
public static void main(String[] args){
ChatServer the_server = new ChatServer();
the_server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
the_server.startRunning();
}
}
顺便说一句,我正在同一台计算机上运行telnet和服务器,如果这会影响到它,则为idk。
答案 0 :(得分:2)
您正尝试使用ObjectInputStream
和ObjectOutputStream
读取/写入字符串。他们使用特殊的协议来解码/编码对象,简单地使用telnet客户端发送字符串是行不通的。我假设您的input.readObject();
电话会抛出异常。
如果您希望能够使用telnet客户端,请考虑使用BufferedReader#readLine
和BufferedWriter
来读取/写入字符串。