java - 我的服务器响应收到的请求,但没有显示加密文本并在java

java - 我的服务器响应收到的请求,但没有显示加密文本并在java,第1张

我的问题是我正在制作客户端/服务器,他们也可以使用BeaufortAlgo进行加密。但是接收到请求后服务器停留在处理中并且没有以数据的形式给出响应。请帮忙。

这是我的服务器端应用

// step 1: importing required package
import java.net.*; 
import java.util.Scanner;
import java.io.*;  
import javax.swing.*;  
public class EchoServer{ 
public static void main(String args[]){ 
 try {  
     //step 2: create a server socket  
     ServerSocket ss = new ServerSocket(1555); 
     System.out.println("Server started...");  
     /* Loop back to the accept method of the server 
     socket and wait for a new connection request. So 
      server will continuously listen for requests 
      */ 
     while(true) { 

     // step 3: wait for incoming connection 
     Socket s = ss.accept(); 
     System.out.println("connection request recieved"); 

     // step 4: Get I/O streams  
     InputStream is = s.getInputStream(); 
     InputStreamReader isr= new InputStreamReader(is); 
     BufferedReader br = new BufferedReader(isr); 

     OutputStream os = s.getOutputStream(); 
     PrintWriter pw = new PrintWriter(os,true); 
     // step 5: Send / Receive message 
     // reading name sent by client 
     String name = br.readLine(); 
     // appending “hello” with the received name 
     String msg = "Hello Server Recieved your message"; 
     // sending back to client 

     // code for encrption decryption
     String key,plain,cipher="",compString;
     compString="abcdefghijklmnopqrstuvwxyz";
     int charInc=0,diff,cipherNum=0;
     char tableChar='a',plainChar;
     key="DoeeePhoeee";
     key=key.toLowerCase();
     key=key.replaceAll(" ","");
     plain=msg;
     plain=plain.toLowerCase();
     plain=plain.replaceAll(" ","");
     if(key.length()<plain.length())
     {
       diff=plain.length()-key.length();
       for(int i=0;i<diff;i  )
       {
       key=key key.charAt(i);
       if(i==key.length())
       {
         i=0;
       }
       }
     }
     for(int eD=0;eD<2;eD  )
     {
     for(int k=0;k<key.length();k  )
     {
     for(int j=0;j<plain.length();j  )
     {
     plainChar=plain.charAt(charInc);
    if(plainChar==tableChar)
    {
      tableChar=key.charAt(charInc);
      for(int i=0;i<26;i  )
      {
        if(plainChar==compString.charAt(i))
        {
          cipherNum=i;
          i=26;
        }
      }
      for(int i=0;i<cipherNum;i  )
      {
        if(tableChar=='a')
        {
          tableChar='z';
          i  ;
        }
        tableChar--;
      }
    }
    else
    {
      tableChar  ;
      j--;
    }
     }
     charInc  ;
     cipher=cipher tableChar;
     }
     if(eD==0)
     {
     System.out.println("Encrypted Text: ");
     System.out.println(cipher);
     pw.println(cipher);
     }
     else
     {
     System.out.println("Decrypted Text: ");
     System.out.println(cipher);
     }
     plain=name;
     cipher="";
     tableChar='a';
     cipherNum=0;
     charInc=0;
     }

     // closing communication sockey 
     s.close(); 
     } // end while 
     }catch(Exception ex){ 
     System.out.println(ex);  
     }  
     } 
     } // end class 

另一部分是客户端应用程序,位于此处。

// step 1: importing required package  
import java.net.*;  
import java.io.*; 

import javax.swing.*; 
public class EchoClient{public static void main(String args[]){ 
try { 

//step 2: create a communication socket 

/* if your server will run on the same machine then you can pass
“localhost” as server address.Notice that port no is similar to
one passed while creating server socket */ 
Socket s = new Socket("localhost", 1555); 

// step 3: Get I/O streams  
InputStream is = s.getInputStream(); 
InputStreamReader isr= new InputStreamReader(is); 
BufferedReader br = new BufferedReader(isr); 

OutputStream os = s.getOutputStream();  
PrintWriter pw = new PrintWriter(os,true); 


// step 4: Send / Receive message 
// asking user to enter his/her name 
String msg = JOptionPane.showInputDialog( "Enter your message"); 
// reading message (name appended with hello) from 
// server 
String name = br.readLine();
// displaying received message 

//code for encrption decryption

  String key,plain,cipher="",compString;
  compString="abcdefghijklmnopqrstuvwxyz";
  int charInc=0,diff,cipherNum=0;
  char tableChar='a',plainChar;
  key="DoeeePhoeee";
  key=key.toLowerCase();
  key=key.replaceAll(" ","");
  plain=msg;
  plain=plain.toLowerCase();
  plain=plain.replaceAll(" ","");
  if(key.length()<plain.length())
  {
    diff=plain.length()-key.length();
    for(int i=0;i<diff;i  )
    {
    key=key key.charAt(i);
    if(i==key.length())
    {
      i=0;
    }
    }
  }
  for(int eD=0;eD<2;eD  )
  {
  for(int k=0;k<key.length();k  )
  {
  for(int j=0;j<plain.length();j  )
  {
  plainChar=plain.charAt(charInc);
 if(plainChar==tableChar)
 {
   tableChar=key.charAt(charInc);
   for(int i=0;i<26;i  )
   {
     if(plainChar==compString.charAt(i))
     {
       cipherNum=i;
       i=26;
     }
   }
   for(int i=0;i<cipherNum;i  )
   {
     if(tableChar=='a')
     {
       tableChar='z';
       i  ;
     }
     tableChar--;
   }
 }
 else
 {
   tableChar  ;
   j--;
 }
  }
  charInc  ;
  cipher=cipher tableChar;
  }
  if(eD==0)
  {
  System.out.println("Encrypted Text: ");
  System.out.println(cipher);
  pw.println(cipher);
  }
  else
  {
  System.out.println("Decrypted Text: ");
  System.out.println(cipher);
JOptionPane.showMessageDialog(null , cipher); 
  }
  plain=name;
  cipher="";
  tableChar='a';
  cipherNum=0;
  charInc=0;
  }


// closing communication socket 
s.close(); 
}catch(Exception ex){ 
System.out.println(ex); 
}  
}  
} // end class 

现在看输出
了     我的代码输出
    服务器已启动...     已收到连接请求

最佳答案:

1 个答案:

答案 0 :(得分:0)

根据您发布的内容您有同步问题。客户端和服务器都调用br.readline()然后调用pw.println()。你必须在一边写。 例如:

     // client
     pw.println ("client is connected");
     br.readline ();
     String cipher = br.readline ();

     //server
     String msg = br.readline ();
     pw.println ("server is running");
     pw.println (cipher);

问题是,如果一方正在写作,那么另一方正在阅读,反之亦然。

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复