serverSocket没有收听

时间:2016-03-26 14:49:44

标签: java networking serversocket

serverSocket没有监听localhost的端口,我尝试了几个端口。即使没有超时线也无法正常工作。请在此代码中提出任何修改建议。

public class server1 extends JApplet implements Serializable{

static JApplet japplet = new JApplet();
private static ServerSocket serverSocket = null;
private static Socket clientSocket = null;
private static final int maxClientsCount = 5;
private static final clientThread[] threads = new clientThread[maxClientsCount];

public void init() {
    tool = Toolkit.getDefaultToolkit();
    setup_applet();
    setup_layout();
    run();
}

public void run() {
    try {
        serverSocket = new ServerSocket(6789);
        serverSocket.setSoTimeout(60000);

    while (true) {
        screen.init_Screen();
        clientSocket = serverSocket.accept();

            int i = 0;
            for (i = 0; i < maxClientsCount; i++) {
                if (threads[i] == null) {
                    (threads[i] = new clientThread(clientSocket, threads))
                            .start();
                    break;
                }
            }
            if (i == maxClientsCount) {
                clientSocket.close();
            }
        }
    } catch (IOException e) {
        System.out.println(e);
    }
}
}

class clientThread extends Thread implements Serializable {

private String clientName = null;
private PrintStream os = null;
private Socket clientSocket = null;
private final clientThread[] threads;
private int maxClientsCount;

public clientThread(Socket clientSocket, clientThread[] threads) {
    this.clientSocket = clientSocket;
    this.threads = threads;
    maxClientsCount = threads.length;
}

public void run() {
    int maxClientsCount = this.maxClientsCount;
    clientThread[] threads = this.threads;

    try {
        os = new PrintStream(clientSocket.getOutputStream());

        while (true) {
            String msg = "server:apl";
            synchronized (this) {
                for (int i = 0; i < maxClientsCount; i++) {
                    if (threads[i] != null && threads[i] == this) {
                        os.println(msg);
                        break;
                    }
                }
            }
            synchronized (this) {
                for (int i = 0; i < maxClientsCount; i++) {
                    if (threads[i] != null && threads[i] != this
                            && threads[i].clientName != null) {
                        BufferedImage image = ImageIO.read(clientSocket.getInputStream());
                        if(image != null) {
                            soms1.screen.paint(image.getGraphics());
                        } else {
                            System.out.println("failed to get");
                        }
                    }
                }
            }
            os.close();
        }
    } catch (IOException e) {
        System.out.println(e);
}
}

我甚至检查了防火墙,以及其他进程是否未使用这些端口。 任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

这并不奇怪。您开发的应用程序是Java小程序。这意味着它将运行的通常执行容器是Web浏览器。当然,Java浏览器插件必须对在用户浏览器中运行的applet施加安全限制。否则,applet将是向用户发送恶意代码的完美方式。

保护机制称为沙盒。这带来了许多限制,在您的上下文中唯一重要的限制是,只允许applet打开与它们来自的主机和端口的网络连接。这意味着他们可以对从中加载的服务器进行HTTP调用。有关详细信息,请参阅此处的文 http://docs.oracle.com/javase/tutorial/deployment/applet/security.html

但是,您可以按照此处的说明签署您的applet,以摆脱这些限制: http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/security/rsa_signing.html 对于签名的applet,将解析现有的清单文件。 http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/security/manifest.html

长答案:正确签署你的applet,你应该好好去。