1

ソケット通信に使用したコードは、コマンド プロンプトで実行されるプログラムで機能しますが、同じコードを Web ページに埋め込まれたアプレットで使用すると、セキュリティ上の問題が発生します。接続できません... 助けてください。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);
            }
        }

この問題を解決するにはどうすればよいですか??

4

2 に答える 2

-1

あなたが得ているエラーの種類を投稿できますか?それはセキュリティ関連の問題でもあります

小さな提案:

サーバーコードを次のように変更する必要があると思います(サーバーは常にクライアントソケットの受け入れをリッスンするため)

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){
    }
于 2013-04-06T12:46:36.590 に答える