2013-04-06 43 views
1

我用於套接字通信的代碼在命令提示符下運行的程序中工作,但是當嵌入在網頁中的小程序使用相同的代碼時存在安全問題。它不連接... 請幫助我,需要在3天內完成這項.... 服務器:java小程序套接字連接問題

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); 
      } 
     } 

可以做些什麼來解決這個問題??

+0

http://docs.oracle.com/javase/tutorial/deployment/applet/security.html – JimmyB 2013-04-06 12:11:45

回答

2
  1. 未簽名的小程序只能連接到它從中加載的主機。

  2. 你的小程序試圖連接到'本地主機'。所以服務器也必須在本地主機上運行。是嗎?你怎麼安排的?

  3. 您正在使用readUTF()來讀取,但是println()來寫。這是行不通的。 println()需要readLine(),readUTF()需要writeUTF()

0

你可以發佈你做了什麼樣的錯誤,甚至是與安全相關的問題也

一個小小的建議:

我認爲你需要改變你的服務器代碼如下(因爲服務器總是聽接受客戶套接字)

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){ 
    } 
+0

那麼就是實際的區別?這是如何解決安全異常的? – EJP 2013-04-06 12:51:47

+0

我aleready寫服務器必須等待每次接受客戶端,但在服務器代碼中的上述邏輯接受只有onc客戶端請求它,但它在客戶端代碼客戶端請求將服務器不斷有服務器代碼我的帖子以及客戶端代碼。 – 2013-04-06 12:55:32

+0

所以差異是服務器端accept()的while(true)?這就是全部?這又如何解決安全異常? – EJP 2013-04-07 00:47:13