Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
服务器问题-Java_Java_Eclipse_Multithreading_Tcp_Server - Fatal编程技术网

服务器问题-Java

服务器问题-Java,java,eclipse,multithreading,tcp,server,Java,Eclipse,Multithreading,Tcp,Server,我正在尝试用Java制作一个聊天功能。问题是我有两门课。一个用于客户端,一个用于客户端GUI。其中,客户端具有逻辑部分,而客户端GUI具有设计。问题出在第46行,其中newlistenfromserver().start()出现错误 无法访问控制器类型的封闭实例。必须 使用类型为的封闭实例限定分配 控制器(例如x.newa(),其中x是控制器的实例) 所以,我所做的是,我将公共类listenfromserverextensedthread更改为static。这意味着公共静态类ListenFrom

我正在尝试用Java制作一个聊天功能。问题是我有两门课。一个用于
客户端
,一个用于
客户端GUI
。其中,
客户端
具有逻辑部分,而
客户端GUI
具有设计。问题出在第46行,其中
newlistenfromserver().start()出现错误

无法访问控制器类型的封闭实例。必须 使用类型为的封闭实例限定分配 控制器(例如x.newa(),其中x是控制器的实例)

所以,我所做的是,我将
公共类listenfromserverextensedthread
更改为
static
。这意味着
公共静态类ListenFromServer扩展了线程
,现在是我遇到的问题

连接到服务器时出错:java.net.ConnectException:连接:地址在本地计算机上无效,或者端口在远程计算机上无效

控制器(客户端逻辑)

package Server;

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




public class Controller {

    private static ObjectInputStream input;
    private static ObjectOutputStream output;
    private static Socket socket;
    private static ClientGUI clientgui;

    private static String username;
    private static String server;
    private static int port;



    public static boolean startClient(){

        try{
            socket = new Socket(server, port);
        }catch (Exception ex){
            System.out.print("Error connecting to the server: " + ex);
            return false;
        }

        String message = "Connection is accepted; " + socket.getInetAddress() +" - "+  socket.getPort();
        System.out.println(message);


    try {
        input=new ObjectInputStream(socket.getInputStream());
        output =new ObjectOutputStream(socket.getOutputStream());
    }
    catch (IOException io) {
        System.out.print("Exception creating new Input/Output Stream: "+ io);
        return false;

    }

    **********new ListenFromServer().start();********* //The problem is here

    try {
        output.writeObject(username);
    }
    catch(IOException io) {
        System.out.print("Exception doing login: " + io);
        disconnect();
        return false;
    }
    return true;
    }

    private void display(String message) {
        if(clientgui == null)
            System.out.println(message);
        else
            clientgui.append(message +"\n");
    }

    public static void sendMessage(Message message) {
        try {
            output.writeObject(message);
        }
        catch(IOException exd) {
            System.out.print("Eceptionwritingtoserver: " + exd);
        }
    }

    private static void disconnect() {
        try {
            if(input != null)
                input.close();
        }catch (Exception ex){}
        try{
            if(output != null)
                output.close();
        }catch(Exception ex){}
        try{
            if(socket != null)
                socket.close();
        }catch(Exception ex){};

        if (clientgui != null)
            clientgui.connectionFailed();
        }

    public class ListenFromServer extends Thread{


        public void run() {
            while(true){
                try{
                    String message = (String) input.readObject();
                    if(clientgui == null){
                        System.out.println(message);
                        System.out.print(":");
                    }
                    else {
                        clientgui.append(message);
                    }
                }
                catch(IOException io){
                    System.out.print("Server has closed the connection");
                    if(clientgui != null)
                        clientgui.connectionFailed();
                    break;
                }
                catch(ClassNotFoundException classex){

                }

                }


            }

        }

    }
ClientGUI

    package Server;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;


    /*
     * The Client with its GUI
     */
    public class ClientGUI extends JFrame implements ActionListener {

        private static final long serialVersionUID = 1L;

        private JLabel lblusername;

        private JTextField textfieldusername, textfieldserver, textfieldportnumber;

        private JButton btnlogin, btnlogout, btnonline;

        private JTextArea textareamessage;

        private boolean connected;

        private Client client;

        private int defaultPort;
        private String defaultHost;


        ClientGUI(String host, int port) {

            super("Chat Client");
            defaultPort = port;
            defaultHost = host;


            JPanel northPanel = new JPanel(new GridLayout(2,2));
            JPanel serverAndPort = new JPanel(new GridLayout(1,2, 2, 2));
            JLabel lblserveraddress = new JLabel("Server Address:  ");
            JLabel lblchat = new JLabel("                #BallIsLife");
            JLabel lblportnumber = new JLabel("Port Number:  ");

            textfieldserver = new JTextField(host);
            textfieldserver.setHorizontalAlignment(SwingConstants.LEFT);
            textfieldserver.setFont(new Font("Tahoma", Font.PLAIN, 20));
            textfieldportnumber = new JTextField("" + port);
            textfieldportnumber.setFont(new Font("Tahoma", Font.PLAIN, 20));
            textfieldportnumber.setHorizontalAlignment(SwingConstants.LEFT);

            lblserveraddress.setFont(new Font("Tahoma", Font.PLAIN, 19));
            serverAndPort.add(lblserveraddress);
            serverAndPort.add(textfieldserver);
            serverAndPort.add(lblchat);
            serverAndPort.add(lblportnumber);
            serverAndPort.add(textfieldportnumber);
            lblchat.setForeground(Color.RED);
            lblportnumber.setFont(new Font("Tahoma", Font.PLAIN, 19));
            northPanel.add(serverAndPort);
            getContentPane().add(northPanel, BorderLayout.NORTH);

            JPanel panelbtn = new JPanel();
            northPanel.add(panelbtn);


            btnlogin = new JButton("Login");
            panelbtn.add(btnlogin);
            btnlogin.setFont(new Font("Tahoma", Font.PLAIN, 17));
            btnlogin.addActionListener(this);

            btnonline = new JButton("Online");
            panelbtn.add(btnonline);
            btnonline.setFont(new Font("Tahoma", Font.PLAIN, 17));

            btnonline.addActionListener(this);
            btnonline.setEnabled(false);        

            btnlogout = new JButton("Logout");
            panelbtn.add(btnlogout);
            btnlogout.setFont(new Font("Tahoma", Font.PLAIN, 17));
            btnlogout.addActionListener(this);
            btnlogout.setEnabled(false);        

            JButton btnPicture = new JButton("Picture");
            btnPicture.setFont(new Font("Tahoma", Font.PLAIN, 17));
            btnPicture.setEnabled(false);
            panelbtn.add(btnPicture);


            textareamessage = new JTextArea("Welcome to the #BallIsLife Chat room.\n");
            textareamessage.setFont(new Font("Monospaced", Font.PLAIN, 15));

            textareamessage.setLineWrap(true);
            textareamessage.setEditable(false);

            JPanel centerPanel = new JPanel(new GridLayout(1,1));
            JScrollPane scrollPane = new JScrollPane(textareamessage);
            centerPanel.add(scrollPane);
            getContentPane().add(centerPanel, BorderLayout.CENTER);


            JPanel southPanel = new JPanel();
            getContentPane().add(southPanel, BorderLayout.SOUTH);

            lblusername = new JLabel("Enter your username", SwingConstants.CENTER);
            lblusername.setFont(new Font("Tahoma", Font.PLAIN, 15));
            southPanel.add(lblusername);

            textfieldusername = new JTextField("Write your username here.");
            textfieldusername.setFont(new Font("Tahoma", Font.PLAIN, 14));
            textfieldusername.setColumns(50);

            southPanel.add(textfieldusername);


            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(823, 665);
            setVisible(true);

        }

    //Logiken

        void append(String str) {
            textareamessage.append(str);
            textareamessage.setCaretPosition(textareamessage.getText().length() - 1);
        }


        void connectionFailed() {
            btnlogin.setEnabled(true);
            btnlogout.setEnabled(false);
            btnonline.setEnabled(false);
            lblusername.setText("Enter your username");
            textfieldusername.setText("Write your username here");

            textfieldportnumber.setText("" + defaultPort);
            textfieldserver.setText(defaultHost);

            textfieldserver.setEditable(false);
            textfieldportnumber.setEditable(false);

            textfieldusername.removeActionListener(this);
            connected = false;
        }

        //

        public void actionPerformed(ActionEvent e) {
            Object button = e.getSource();

            if(button == btnlogout) {
                Controller.sendMessage(new Message("", Message.LOGOUT)); //Ändra till Chatmessage klass
                btnlogin.setText("Login");
                return;
            }

            if(button == btnonline) {
                Controller.sendMessage(new Message("", Message.ONLINE));    //Ändra till Chatmessage klass          
                return;
            }


            if(connected) {

                Controller.sendMessage(new Message(textfieldusername.getText(), Message.MESSAGE)); //Ändra till Chatmessage klass       
                textfieldusername.setText("");
                return;
            }


            if(button == btnlogin) {

                String username = textfieldusername.getText();

                if(username.length() == 0)
                    return;

                String server = textfieldserver.getText();
                if(server.length() == 0)
                    return;

                String portNumber = textfieldportnumber.getText();
                if(portNumber.length() == 0)
                    return;


                int port = 0;
                try {
                    port = Integer.parseInt(portNumber);
                }
                catch(Exception en) {
                    return;  
                }


                client = new Client(server, username, port, this);

                if(!Controller.startClient()) 
                    return;

                }

                connected = true;

                textfieldusername.setText("");
                btnlogin.setText("Send message");


                btnlogin.setEnabled(true);

                btnlogout.setEnabled(true);
                btnonline.setEnabled(true);

                textfieldserver.setEditable(false);
                textfieldportnumber.setEditable(false);

                textfieldusername.addActionListener(this);
            }



        // to start the whole thing the server
        public static void main(String[] args) {
            new ClientGUI("localhost", 1500);
        }

    }
package Server;

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

/*
 * The server as a GUI
 */
public class ServerGUI extends JFrame implements ActionListener, WindowListener {

    private static final long serialVersionUID = 1L;
    // the stop and start buttons
    private JButton stopStart, saveLog;
    // JTextArea for the chat room and the events
    private JTextArea chat;
    // private JTextArea event;
    // The port number
    private JTextField tfPortNumber;
    // my server
    private Server server;

    // server constructor that receive the port to listen to for connection as
    // parameter
    ServerGUI(int port) {
        super("Chat Server");
        server = null;
        // in the NorthPanel the PortNumber the Start and Stop buttons
        JPanel north = new JPanel();
        north.add(new JLabel("Port number: "));
        tfPortNumber = new JTextField("" + port);
        north.add(tfPortNumber);
        // to stop or start the server, we start with "Start"
        stopStart = new JButton("Start");
        stopStart.addActionListener(this);
        saveLog = new JButton("Save log");
        saveLog.addActionListener(this);
        north.add(stopStart);
        north.add(saveLog);
        add(north, BorderLayout.NORTH);
        // the event and chat room
        JPanel center = new JPanel(new GridLayout());
        chat = new JTextArea(120, 20);
        chat.setEditable(false);
        chat.setWrapStyleWord(true);
        chat.setLineWrap(true);
        appendRoom(null, "Chat room and Events log for server.\n");
        center.add(new JScrollPane(chat));
        // event = new JTextArea(80,80);
        // event.setEditable(false);
        // appendEvent("Events log.\n");
        // center.add(new JScrollPane(event));
        add(center);
        // need to be informed when the user click the close button on the frame
        addWindowListener(this);
        setSize(450, 600);
        setVisible(true);
    }

    public void writeLog() {
        try {
            JFileChooser chooser = new JFileChooser();
            String content = chat.getText();
            int actionDialog = chooser.showSaveDialog(this);
            content = content.replaceAll("(?!\\r)\\n", "\r\n");
            if (actionDialog == JFileChooser.APPROVE_OPTION) {
                File file = new File(chooser.getSelectedFile() + ".txt");
                // if file doesnt exists, then create it
//              if (!file.exists()) {
//                  file.createNewFile();
//              }
                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(content);
                bw.close();

            }
        } catch (IOException ee) {
            ee.printStackTrace();
        }
        // JFileChooser chooser = new JFileChooser();
        // // chooser.setCurrentDirectory(new File("./"));
        // int actionDialog = chooser.showSaveDialog(this);
        // if (actionDialog == JFileChooser.APPROVE_OPTION) {
        // File fileName = new File(chooser.getSelectedFile() + "");
        // if (fileName == null)
//      // return;
//       if (fileName.exists()) {
//       actionDialog = JOptionPane.showConfirmDialog(this,
//       "Replace existing file?");
//       if (actionDialog == JOptionPane.NO_OPTION)
//       return;
//       }
        // try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
        // new FileOutputStream(fileName), "ISO-8859-1"))) {
        // bw.write(saveText);
        // // bw.newLine();
        // bw.flush();
        // }
        // }
    }

    // append message to the two JTextArea
    // position at the end
    void appendRoom(String chatStr, String eventStr) {
        chat.append(chatStr);
        chat.append(eventStr);
        // chat.setCaretPosition(chat.getText().length() - 1);
    }

    // void appendEvent(String str) {
    // event.append(str);
    // event.setCaretPosition(chat.getText().length() - 1);
    //
    // }

    // start or stop where clicked
    public void actionPerformed(ActionEvent e) {
        // if running we have to stop
        if (e.getSource() == saveLog) {
            writeLog();
        } else if (e.getSource() == stopStart) {
            if (server != null) {
                server.stop();
                server = null;
                tfPortNumber.setEditable(true);
                stopStart.setText("Start");
                return;
            }
            // OK start the server
            int port;
            try {
                port = Integer.parseInt(tfPortNumber.getText().trim());
            } catch (Exception er) {
                appendRoom(null, "Invalid port number");
                return;
            }
            // ceate a new Server
            server = new Server(port, this);
            // and start it as a thread
            new ServerRunning().start();
            stopStart.setText("Stop");
            tfPortNumber.setEditable(false);
        }
    }

    /*
     * A thread to run the Server
     */
    class ServerRunning extends Thread {
        public void run() {
            server.start(); // should execute until if fails
            // the server failed
            stopStart.setText("Start");
            tfPortNumber.setEditable(true);
            appendRoom(null, "Server closed\n");
            server = null;
        }
    }

    // entry point to start the Server
    public static void main(String[] arg) {
        // start server default port 1500
        new ServerGUI(1500);
    }

    /*
     * If the user click the X button to close the application I need to close
     * the connection with the server to free the port
     */
    public void windowClosing(WindowEvent e) {
        // if my Server exist
        if (server != null) {
            try {
                server.stop(); // ask the server to close the conection
            } catch (Exception eClose) {
            }
            server = null;
        }
        // dispose the frame
        dispose();
        System.exit(0);
    }

    // I can ignore the other WindowListener method
    public void windowClosed(WindowEvent e) {
    }

    public void windowOpened(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }

    // /*
    // * A thread to run the Server
    // */
    // class ServerRunning extends Thread {
    // public void run() {
    // server.start(); // should execute until if fails
    // // the server failed
    // stopStart.setText("Start");
    // tfPortNumber.setEditable(true);
    // appendRoom(null, "Server closed\n");
    // server = null;
    // }
    // }
}
服务器

   package Server;

import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;

/*
 * The server that can be run both as a console application or a GUI
 */
public class Server {
    // a unique ID for each connection
    private static int uniqueId;
    // an ArrayList to keep the list of the Client
    private ArrayList<ClientThread> al;
    // if I am in a GUI
    private ServerGUI sg;
    // to display time
    private SimpleDateFormat sdf;
    // the port number to listen for connection
    private int port;
    // the boolean that will be turned of to stop the server
    private boolean keepGoing;


    /*
     *  server constructor that receive the port to listen to for connection as parameter
     *  in console
     */
    public Server(int port) {
        this(port, null);
    }

    public Server(int port, ServerGUI sg) {
        // GUI or not
        this.sg = sg;
        // the port
        this.port = port;
        // to display hh:mm:ss
        sdf = new SimpleDateFormat("HH:mm:ss");
        // ArrayList for the Client list
        al = new ArrayList<ClientThread>();
    }

    public void start() {
        keepGoing = true;
        /* create socket server and wait for connection requests */
        try 
        {
            // the socket used by the server
            ServerSocket serverSocket = new ServerSocket(port);

            // infinite loop to wait for connections
            while(keepGoing) 
            {
                // format message saying we are waiting
                display("Server waiting for Clients on port " + port + ".");

                Socket socket = serverSocket.accept();      // accept connection
                // if I was asked to stop
                if(!keepGoing)
                    break;
                ClientThread t = new ClientThread(socket);  // make a thread of it
                al.add(t);                                  // save it in the ArrayList
                t.start();
            }
            // I was asked to stop
            try {
                serverSocket.close();
                for(int i = 0; i < al.size(); ++i) {
                    ClientThread tc = al.get(i);
                    try {
                    tc.sInput.close();
                    tc.sOutput.close();
                    tc.socket.close();
                    }
                    catch(IOException ioE) {
                        // not much I can do
                    }
                }
            }
            catch(Exception e) {
                display("Exception closing the server and clients: " + e);
            }
        }
        // something went bad
        catch (IOException e) {
            String msg = sdf.format(new Date()) + " Exception on new ServerSocket: " + e + "\n";
            display(msg);
        }
    }       
    /*
     * For the GUI to stop the server
     */
    protected void stop() {
        keepGoing = false;
        // connect to myself as Client to exit statement 
        // Socket socket = serverSocket.accept();
        try {
            new Socket("localhost", port);
        }
        catch(Exception e) {
            // nothing I can really do
        }
    }
    /*
     * Display an event (not a message) to the console or the GUI
     */
    private void display(String msg) {
        String time = sdf.format(new Date()) + " " + msg;
        if(sg == null)
            System.out.println(time);
        else
            sg.appendRoom(null,time + "\n");
    }
    /*
     *  to broadcast a message to all Clients
     */
    private synchronized void broadcast(String message) {
        // add HH:mm:ss and \n to the message
        String time = sdf.format(new Date());
        String messageLf = time + " " + message + "\n";
        // display message on console or GUI
        if(sg == null)
            System.out.print(messageLf);
        else
            sg.appendRoom(messageLf,null);     // append in the room window

        // we loop in reverse order in case we would have to remove a Client
        // because it has disconnected
        for(int i = al.size(); --i >= 0;) {
            ClientThread ct = al.get(i);
            // try to write to the Client if it fails remove it from the list
            if(!ct.writeMsg(messageLf)) {
                al.remove(i);
                display("Disconnected Client " + ct.username + " removed from list.");
            }
        }
    }

    // for a client who logoff using the LOGOUT message
    synchronized void remove(int id) {
        // scan the array list until we found the Id
        for(int i = 0; i < al.size(); ++i) {
            ClientThread ct = al.get(i);
            // found it
            if(ct.id == id) {
                al.remove(i);
                return;
            }
        }
    }

    /*
     *  To run as a console application just open a console window and: 
     * > java Server
     * > java Server portNumber
     * If the port number is not specified 1500 is used
     */ 
    public static void main(String[] args) {
        // start server on port 1500 unless a PortNumber is specified 
        int portNumber = 1500;
        switch(args.length) {
            case 1:
                try {
                    portNumber = Integer.parseInt(args[0]);
                }
                catch(Exception e) {
                    System.out.println("Invalid port number.");
                    System.out.println("Usage is: > java Server [portNumber]");
                    return;
                }
            case 0:
                break;
            default:
                System.out.println("Usage is: > java Server [portNumber]");
                return;

        }
        // create a server object and start it
        Server server = new Server(portNumber);
        server.start();
    }

    /** One instance of this thread will run for each client */
    class ClientThread extends Thread {
        // the socket where to listen/talk
        Socket socket;
        ObjectInputStream sInput;
        ObjectOutputStream sOutput;
        // my unique id (easier for deconnection)
        int id;
        // the Username of the Client
        String username;
        // the only type of message a will receive
        Message cm;
        // the date I connect
        String date;

        // Constructore
        ClientThread(Socket socket) {
            // a unique id
            id = ++uniqueId;
            this.socket = socket;
            /* Creating both Data Stream */
            System.out.println("Thread trying to create Object Input/Output Streams");
            try
            {
                // create output first
                sOutput = new ObjectOutputStream(socket.getOutputStream());
                sInput  = new ObjectInputStream(socket.getInputStream());
                // read the username
                username = (String) sInput.readObject();
                display(username + " just connected.");
            }
            catch (IOException e) {
                display("Exception creating new Input/output Streams: " + e);
                return;
            }
            // have to catch ClassNotFoundException
            // but I read a String, I am sure it will work
            catch (ClassNotFoundException e) {
            }
            date = new Date().toString() + "\n";
        }

        // what will run forever
        public void run() {
            // to loop until LOGOUT
            boolean keepGoing = true;
            while(keepGoing) {
                // read a String (which is an object)
                try {
                    cm = (Message) sInput.readObject();
                }
                catch (IOException e) {
                    display(username + " Exception reading Streams: " + e);
                    break;              
                }
                catch(ClassNotFoundException e2) {
                    break;
                }
                // the messaage part of the ChatMessage
                String message = cm.getMessage();

                // Switch on the type of message receive
                switch(cm.getType()) {

                case Message.MESSAGE:
                    broadcast(username + ": " + message);
                    break;
                case Message.LOGOUT:
                    display(username + " disconnected with a LOGOUT message.");
                    keepGoing = false;
                    break;
                case Message.ONLINE:
                    writeMsg("List of the users connected at " + sdf.format(new Date()) + "\n");
                    // scan al the users connected
                    for(int i = 0; i < al.size(); ++i) {
                        ClientThread ct = al.get(i);
                        writeMsg((i+1) + ") " + ct.username + " since " + ct.date);
                    }
                    break;
                }
            }
            // remove myself from the arrayList containing the list of the
            // connected Clients
            remove(id);
            close();
        }

        // try to close everything
        private void close() {
            // try to close the connection
            try {
                if(sOutput != null) sOutput.close();
            }
            catch(Exception e) {}
            try {
                if(sInput != null) sInput.close();
            }
            catch(Exception e) {};
            try {
                if(socket != null) socket.close();
            }
            catch (Exception e) {}
        }

        /*
         * Write a String to the Client output stream
         */
        private boolean writeMsg(String msg) {
            // if Client is still connected send the message to it
            if(!socket.isConnected()) {
                close();
                return false;
            }
            // write the message to the stream
            try {
                sOutput.writeObject(msg);
            }
            // if an error occurs, do not abort just inform the user
            catch(IOException e) {
                display("Error sending message to " + username);
                display(e.toString());
            }
            return true;
        }
    }
}
消息

    package Server;

import java.io.Serializable;

//Klassen som kollar vad för typ av message 

public class Message implements Serializable {

    protected static final long serialVersionUID = 42L;

    static final int ONLINE = 0;

    static final int MESSAGE = 1;

    static final int LOGOUT = 2;

    private int SAVELOG = 3;
    private String message; 
    private int type;

    public Message(String message, int type){
        this.type = type;
        this.message = message;
    }

    public int getType(){
        return type;
    }

    public String getMessage(){
        return message;
    }
}

使您的内部类
ListenFromServer
static,因为您是从静态方法引用它的

public class Controller {
    ...
    public static class ListenFromServer {
        ...
    }
}

在你缩小问题范围之前,人们不太可能帮你解决这个问题。发布那么多代码会妨碍人们提供帮助-最好只发布与问题本身相关的代码。这不是对您的问题的回答,但早在我构建的框架(ServerSocketEx)非常适用于解决您的问题。你可以在这里找到:@Oli是的,我确实理解,问题是我并不真正知道问题所在,因为我以前从未犯过这个错误。这让我很难知道问题是什么。因为一切看起来都很好,但是java连接出现了错误。我认为这与服务器有关。但我对此表示怀疑。这就是我添加所有内容的原因:(@ControlAltDel哦,谢谢,我会查看:)您的类
客户端的代码缺失,但无论如何,您的问题不是
静态
修饰符太少,而是太多。不要将嵌套的类
Controller.ListenFromServer
更改为
static
您应该删除所有其他
static
修饰符。然后,在尝试连接之前,确保所有这些字段都已正确初始化,例如,向
控制器
添加适当的构造函数。我敢打赌,一旦您在连接之前确保
服务器
端口
具有有效值,问题就会消失。欢迎访问该网站。阅读整篇文章,你应该得到很多选票:-)