首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >serverSocket未侦听

serverSocket未侦听
EN

Stack Overflow用户
提问于 2016-03-26 22:49:44
回答 1查看 191关注 0票数 0

serverSocket没有监听本地主机的端口,我尝试了几个端口。即使没有超时线,它也不能工作。请在此代码中提出任何修改建议。

代码语言:javascript
复制
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);
}
}

我甚至检查了防火墙,如果端口没有被其他进程使用。任何帮助我们都将不胜感激

EN

回答 1

Stack Overflow用户

发布于 2016-03-26 23:10:20

这并不令人惊讶。您开发的应用程序是一个Java小程序。这意味着它将在web浏览器中运行的通常执行容器。当然,Java浏览器插件必须对用户浏览器中运行的applet设置安全限制。否则,applet将是将恶意代码发送给用户的完美方式。

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

但是,您可以像这里解释的那样对小程序进行签名,以摆脱这些限制:对于已签名的小程序,解析现有的清单文件的http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/security/rsa_signing.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/jweb/security/manifest.html

长短答案:正确地签署你的applet,你就可以开始工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36236695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档