2015-03-13 33 views
-1

我正在尝试与Java进行聊天功能。问题是我有两个班。一个用于Client,另一个用于ClientGUI。其中Client一个有逻辑部分和ClientGUI的设计。问题是在第46行,其中new ListenFromServer().start();出现错误服务器问题 - Java

没有可以访问类型Controller的封闭实例。必须 使用包含类型为 控制器的封闭实例(例如,x.new A()其中x是控制器的实例)来限定分配。

所以我所做的就是将public class ListenFromServer extends Thread更改为static。这意味着public static class ListenFromServer extends Thread而现在我越来越

错误连接到服务器的问题:java.net.ConnectException:连接:Address是本地计算机上无效,或者端口不是远程机器

上有效

控制器(客户端逻辑)

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 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; 
     } 
    } 
} 

ServerGUI

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.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; 
    } 
} 
+0

人是不可能帮助你,直到你缩小了的问题是什么提到它。发布这么多代码会阻止人们提供帮助 - 最好只发布与问题本身相关的代码。 – Oli 2015-03-13 17:11:39

+0

不是你的问题的答案,但回到当天我建立了一个非常适用于解决你的问题的框架(ServerSocketEx)。你可以在这里找到:http://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/io/ – ControlAltDel 2015-03-13 17:11:56

+0

@Oli是的,我明白,问题是,我真的不知道这个问题,因为我有从来没有得到过这个错误。这使我很难知道问题是什么。由于一切看起来不错,但得到关于Java连接的错误。我假设它是关于服务器的。但我怀疑它。这就是为什么我添加了一切:( – ThrillOfit 2015-03-13 17:20:54

回答

1

ListenFromServer您的内部类静态的,因为你是从一个静态方法

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

欢迎来到本网站。你应该阅读整篇文章,应得到许多赞扬:-) – mins 2015-03-13 17:48:09