【问题标题】:java applet socket connection issuejava小程序套接字连接问题
【发布时间】:2013-04-06 11:35:44
【问题描述】:

我用于套接字通信的代码在命令提示符下运行的程序中工作,但是当在嵌入网页的小程序中使用相同的代码时,会出现安全问题。它不连接... 请帮帮我,需要在3天内完成这个...... 服务器:

    public void run()
{
    try
    {
        ServerSocket s1 = new ServerSocket(5555); // create a server socket and bind it to port number.

        Socket s2 = s1.accept(); // make the server listen for a connection.

        DataInputStream in = new DataInputStream(s2.getInputStream());
        PrintStream out = new PrintStream(s2.getOutputStream());
        while(true)
        {
            char[] buf = new char[150];
            String line = in.readUTF(); // wait for the client to send a line of text.
            if(line.equals("send"))
            {
                for(int i=0;i<150;i++)
                    buf[i]=0;
                if(Traffic1.s1wiut) buf[0]='1';
                if(Traffic1.s1wist) buf[1]='1';
                if(Traffic1.s1wirt) buf[2]='1';
                if(Traffic1.s1silt) buf[3]='1';
                if(Traffic1.s1siut) buf[4]='1';
                if(Traffic1.s1sirt) buf[5]='1';
            }
            String line1 = new String(buf);
            out.println(line1); // send the data to the client.
            out.flush(); // flush the stream to ensure that the data reaches the other end.
        }
    }

客户端:

    public void run()
{

    while(true)
    {
        int serverPort = 5555; // port number on which the server is listening.

        try
        {
            InetAddress ipAddress = InetAddress.getLocalHost(); // create an object that represents the above IP address.

            Socket socket = new Socket(ipAddress,serverPort); // create a socket with the server's IP address and server's port.

            DataInputStream in = new DataInputStream(socket.getInputStream());
            PrintStream out = new PrintStream(socket.getOutputStream());

            while(true)
            {
                char[] buf = new char[150];
                String line = "send"; // request string to send to server.
                out.println(line); // send the above line to the server.
                out.flush(); // flush the stream to ensure that the data reaches the other end.
                line = in.readUTF(); // wait for the server to send a line of text.
                buf = line.toCharArray();
                if(buf[0]=='1')     s1wiut=true;    else    s1wiut=false;
                if(buf[1]=='1')     s1wist=true;    else    s1wist=false;
                if(buf[2]=='1')     s1wirt=true;    else    s1wirt=false;
                if(buf[3]=='1')     s1silt=true;    else    s1silt=false;
                if(buf[4]=='1')     s1siut=true;    else    s1siut=false;
                if(buf[5]=='1')     s1sirt=true;    else    s1sirt=false;

                repaint();
                Thread.sleep(1000);
            }
        }

有什么办法可以解决这个问题??

【问题讨论】:

标签: java security sockets applet connection


【解决方案1】:
  1. 未签名的小程序只能连接到加载它的主机。

  2. 您的小程序尝试连接到“localhost”。所以服务器也必须在 localhost 上运行。是吗?你是怎么安排的?

  3. 您使用readUTF() 读取,但println() 写入。那是行不通的。 println() 需要readLine()readUTF() 需要writeUTF()

【讨论】:

    【解决方案2】:

    您能发布您遇到的错误类型吗?即使它也是与安全相关的问题

    一个小建议:

    我认为您需要按如下方式更改您的服务器代码(因为服务器总是监听客户端套接字的接受)

    public void run()
    {
        try
        {
            ServerSocket s1 = new ServerSocket(5555); // create a server socket and bind it to port number.
    
            while(true){
    
            Socket s2 = s1.accept(); // make the server listen for a connection.
    
            DataInputStream in = new DataInputStream(s2.getInputStream());
            PrintStream out = new PrintStream(s2.getOutputStream());
            while(true)
            {
                char[] buf = new char[150];
                String line = in.readUTF(); // wait for the client to send a line of text.
                if(line.equals("send"))
                {
                    for(int i=0;i<150;i++)
                        buf[i]=0;
                    if(Traffic1.s1wiut) buf[0]='1';
                    if(Traffic1.s1wist) buf[1]='1';
                    if(Traffic1.s1wirt) buf[2]='1';
                    if(Traffic1.s1silt) buf[3]='1';
                    if(Traffic1.s1siut) buf[4]='1';
                    if(Traffic1.s1sirt) buf[5]='1';
                }
                String line1 = new String(buf);
                out.println(line1); // send the data to the client.
                out.flush(); // flush the stream to ensure that the data reaches the other end.
              }
           } // end of while
        }
    

    你的客户如下所示

    public void run()
    {
    
        int serverPort = 5555; // port number on which the server is listening.
    
        try
        {
            InetAddress ipAddress = InetAddress.getLocalHost(); // create an object that represents the above IP address.
    
            Socket socket = new Socket(ipAddress,serverPort); // create a socket with the server's IP address and server's port.
    
            DataInputStream in = new DataInputStream(socket.getInputStream());
            PrintStream out = new PrintStream(socket.getOutputStream());
    
            while(true)
            {
                char[] buf = new char[150];
                String line = "send"; // request string to send to server.
                out.println(line); // send the above line to the server.
                out.flush(); // flush the stream to ensure that the data reaches the other end.
                line = in.readUTF(); // wait for the server to send a line of text.
                buf = line.toCharArray();
                if(buf[0]=='1')     s1wiut=true;    else    s1wiut=false;
                if(buf[1]=='1')     s1wist=true;    else    s1wist=false;
                if(buf[2]=='1')     s1wirt=true;    else    s1wirt=false;
                if(buf[3]=='1')     s1silt=true;    else    s1silt=false;
                if(buf[4]=='1')     s1siut=true;    else    s1siut=false;
                if(buf[5]=='1')     s1sirt=true;    else    s1sirt=false;
    
                repaint();
                Thread.sleep(1000);
            }
        }catch(Exception e){
        }
    

    【讨论】:

    • 那么实际的区别是什么?这如何解决安全异常?
    • 我已经写过服务器每次都必须等待接受客户端,但在服务器代码中的上述逻辑中,仅接受一次客户端请求,但在客户端代码中,客户端请求将持续发送到服务器在我的帖子的服务器代码和客户端代码中查看一次。
    • 那么区别在于服务器中accept()周围的while(true)?这就是全部?这又是如何解决安全异常的呢?
    猜你喜欢
    • 2019-10-21
    • 2017-04-15
    • 2014-10-21
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多