serverSocket不能是creater - Java

时间:2015-03-07 15:03:42

标签: java eclipse server

我正在写一个服务器:

Server.java

package server;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.Observer;
import java.util.Vector;
import java.util.Observable;
import java.io.IOException;
//import java.io.*;

public class Server implements Observer {
    private Socket socket;

/** This vector holds all connected clients.
 * May be used for broadcasting, etc. */
private Vector<ClientThread> clients;
private ServerSocket ssocket;  //Server Socket
private StartServerThread sst; //inner class

/**
 * Represents each currently connected client.
 * @label initiates
 * @clientCardinality 1
 * @supplierCardinality 0..*
 */
private ClientThread clientThread;

/** Port number of Server. */
private int port;
private boolean listening; //status for listening

public Server() {
    this.clients = new Vector<ClientThread>();
    this.port = 5555; //default port
    this.listening = false;
}

public void startServer() {
    if (!listening) {
        this.sst = new StartServerThread();
        this.sst.start();
        this.listening = true;
    }
}

public void stopServer() {
    if (this.listening) {
        this.sst.stopServerThread();
    //close all connected clients//

        java.util.Enumeration<ClientThread> e = this.clients.elements();
        while(e.hasMoreElements())
        {
        ClientThread ct = (ClientThread)e.nextElement();
            ct.stopClient();
        }
        this.listening = false;
    }
}

//observer interface//
public void update(Observable observable, Object object) {
    //notified by observables, do cleanup here//
    this.clients.removeElement(observable);
}

public int getPort() {
    return port;
}

public void setPort(int port) {
    this.port = port;
}



/** This inner class will keep listening to incoming connections,
 *  and initiating a ClientThread object for each connection. */

  private class StartServerThread extends Thread {
    private boolean listen;

    public StartServerThread() {
        this.listen = false;
    }

    public void run() {
        this.listen = true;
        try {

/**The following constructor provides a default number of
 * connections -- 50, according to Java's documentation.
 * An overloaded constructor is available for providing a 
 * specific number, more or less, about connections. */

Server.this.ssocket = 
                      new ServerSocket(Server.this.port);
                    System.out.println(TimeStamp.getTimeStamp() + " " + "Server socket created");

               while (this.listen) {
                   //wait for client to connect//

              Server.this.socket = Server.this.ssocket.accept();
                System.out.println(TimeStamp.getTimeStamp() + " " + "Client connected");
                try {
                    Server.this.clientThread = 
                         new ClientThread(Server.this.socket);
                    Thread t = 
                         new Thread(Server.this.clientThread);
                                      Server.this.clientThread.addObserver(Server.this);
                    Server.this.clients.addElement(
                       Server.this.clientThread
                          );
                    t.start();
                } catch (IOException ioe) {
                    //some error occurred in ClientThread //
                }
            }
        } catch (IOException ioe) {
            //I/O error in ServerSocket//
            this.stopServerThread();
        }
    }

    public void stopServerThread() {
        try {
            Server.this.ssocket.close();
        }
        catch (IOException ioe) {
            //unable to close ServerSocket
        }

        this.listen = false;
    }
}
}

我在Main.java中运行服务器:

package server;

public class Main {

    public static void main(String[] args) {
        System.out.println("Main()\n");
        Server server = new Server();
        server.startServer();
    }
}

我第一次运行它,一切都很好。 现在由于某种原因,我发现无法创建ServerSocket。 dubbuging流程直接进入尝试创建新ServerSocket的块的catch块。

我进入 eclipse 控制台:

  

main()的

     

线程中的异常&#34;线程0&#34; java.lang.NullPointerException at   server.Server $ StartServerThread.stopServerThread(Server.java:127)at   server.Server $ StartServerThread.run(Server.java:121)

0 个答案:

没有答案