My problem is that I am making client/server who can also does cryptography by using BeaufortAlgo. But the server after receiving request stuck in processing and doesn’t give response in form of data. Please help.
This is my server side app
// 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
The other part is Client app which is down here.
// 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
now see the output
Output of My Code
Server started…
connection request recieved
Answer
From what you have posted You have synchronization problems. Both client and server are calling br.readline() then pw.println(). You have to write on one side. For example:
// 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);
The thing is if one side is writing then the other side is reading and vice versa.