Thursday, 29 January 2015

Write a Program for Client and Server communication in java

server program: import java.net.*; import java.io.*; class Server { public static void main(String[] args) { try{ ServerSocket ss = new ServerSocket(5200); System.out.println("Server waiting....."); Socket s = ss.accept(); InputStreamReader is = new InputStreamReader(s.getInputStream()); BufferedReader br = new BufferedReader(is); String mc = br.readLine(); String key = "i am the passkey"; System.out.println(mc); StringBuilder sb = new StringBuilder(); for (int i=0;i<16;i++ ) { sb.append(mc.charAt(i)^key.charAt(i)); } System.out.println("The Message from client is"+sb.toString()); } catch(Exception e){System.out.println("Got an error "+e.getMessage());} } } client program import java.net.*; import java.io.*; class Client { public static void main(String[] args) { try{ Socket cs = new Socket("localhost",5200); OutputStreamWriter osw = new OutputStreamWriter(cs.getOutputStream()); BufferedWriter bw = new BufferedWriter(osw); String msg = "i am the message"; String key = "i am the passkey"; StringBuilder sb = new StringBuilder(); for (int i=0;i<msg.length() ;i++ ) { sb.append(msg.charAt(i)^key.charAt(i)); } bw.write(new String(sb)); bw.newLine(); bw.flush(); }catch(Exception e){ System.out.println("got an exception at client"+e.getMessage()); } } }

No comments:

Post a Comment