ServerSocket仅适用于Eclipse - 而不是嵌入到HTML中

时间:2016-07-05 21:27:40

标签: java html eclipse server serversocket

我创建的程序只有一台服务器,当连接到同一个端口时,它连接端口(6789)和客户端。但是,我的问题始于服务器启动并连接到端口6789.

有趣的是,我的服务器在Eclipse中完美运行,但是当它嵌入HTML时,虽然GUI和其他功能可行,但它不起作用。以下代码用于启动我的服务器。我没有包含ClientThread或ServerGUI类,因为我的主要问题是让服务器连接到端口。

在使用HTML时,我应该使用另一个端口而不是6789吗?

谢谢!

public class Server {

    private ServerGUI sg;
    private int port;
    // the boolean that will be turned of to stop the server
    private boolean keepGoing;
    HashSet<String> retailers;
    HashSet<String> retailers_company;
    ArrayList<HashSet<String>> al;

    public Server(int port, ServerGUI sg) {
        this.sg = sg;
        this.port = port;       
    }

    public void start() {
        keepGoing = true;
        try 
        {
            ServerSocket serverSocket = new ServerSocket(port);
            display("Server started on Port: " + port);
            while(keepGoing) 
            {
                Socket socket = serverSocket.accept();
                if(!keepGoing)
                    break;
                ClientThread t = new ClientThread(socket);
                t.start();
            }
            try {
                serverSocket.close();
            }
            catch(Exception e) {
                display("Exception closing the server and clients: " + e);
            }
        }
        // something went bad
        catch (IOException e) {
            display("Exception on new ServerSocket: " + e + "\n");
        }
    }
}

0 个答案:

没有答案