how can u pass data from 1-another system by socket class?
ReceiverMain.java
import java.net.*; import java.io.*; class RequesterMain {
ObjectOutputStream out; ObjectInputStream in; Socket connection=null; String message,message1; RequesterMain(){} void run() { try { connection=new Socket("localhost",19999); InputStreamReader isr=new InputStreamReader(System.in); BufferedReader stdin=new BufferedReader(isr); out=new ObjectOutputStream(connection.getOutputStream()); in=new ObjectInputStream(connection.getInputStream()); System.out.println("connected to port"+connection.getLocalPort()); try { message=(String)in.readObject(); System.out.println(message); } catch(ClassNotFoundException e) { System.out.println("Exception is"+e); } message=""; System.out.println("Enter the number :"); message1=stdin.readLine(); out.writeObject(message1); out.flush(); try { message=(String)in.readObject(); System.out.println(message); } catch(ClassNotFoundException e) { System.out.println("Exception is"+e); } System.out.println(" The binary form of number is:"+message); } catch(UnknownHostException ae) { System.out.println("Exception is"+ae); } catch(IOException e) { System.out.println("Exception is"+e); } try { out.close(); in.close(); connection.close(); } catch(IOException e) { System.out.println("Exception is"+e); } } public static void main(String args[])throws IOException { RequesterMain s=new RequesterMain(); s.run(); }
}
ServerMain.java
import java.net.*; import java.io.*; class ServerMain {
ServerSocket server; ObjectOutputStream out; ObjectInputStream in; Socket connection1=null; String message,message1,message2=""; int i=7; ServerMain(){} void run() { try { System.out.println("waiting for connection"); message="hi i am server"; server=new ServerSocket(19999,10); connection1=server.accept(); System.out.println("connected to port"+connection1.getLocalPort()); out=new ObjectOutputStream(connection1.getOutputStream()); in=new ObjectInputStream(connection1.getInputStream()); out.writeObject(message); out.flush(); try { message1=(String)in.readObject(); System.out.println(message1); } catch(ClassNotFoundException e) { System.out.println("Exception is"+e); } int m; int n=Integer.parseInt(message1); message1=""; while(n>0) { m=n%2; message1=message1+m; n=n/2; i--; } System.out.println(message1); n=message1.length(); System.out.println("string in reversed order is "); for(int i=n-1;i>=0;i--) { char c=message1.charAt(i); message2+=c; } out.writeObject(message2); out.flush(); } catch(UnknownHostException ae){ System.out.println("Exception is"+ae); } catch(IOException e) { System.out.println("Exception is"+e); } try { out.close(); in.close(); connection1.close(); } catch(IOException e) { System.out.println("Exception is"+e); } } public static void main(String args[])throws IOException { ServerMain s=new ServerMain(); s.run(); }
}
run first ServerMain in one cmd and ReceiverMain.java in another cmd.
ReceiverMain.java
import java.net.*; import java.io.*; class RequesterMain {
ObjectOutputStream out; ObjectInputStream in; Socket connection=null; String message,message1; RequesterMain(){} void run() { try { connection=new Socket("localhost",19999); InputStreamReader isr=new InputStreamReader(System.in); BufferedReader stdin=new BufferedReader(isr); out=new ObjectOutputStream(connection.getOutputStream()); in=new ObjectInputStream(connection.getInputStream()); System.out.println("connected to port"+connection.getLocalPort()); try { message=(String)in.readObject(); System.out.println(message); } catch(ClassNotFoundException e) { System.out.println("Exception is"+e); } message=""; System.out.println("Enter the number :"); message1=stdin.readLine(); out.writeObject(message1); out.flush(); try { message=(String)in.readObject(); System.out.println(message); } catch(ClassNotFoundException e) { System.out.println("Exception is"+e); } System.out.println(" The binary form of number is:"+message); } catch(UnknownHostException ae) { System.out.println("Exception is"+ae); } catch(IOException e) { System.out.println("Exception is"+e); } try { out.close(); in.close(); connection.close(); } catch(IOException e) { System.out.println("Exception is"+e); } } public static void main(String args[])throws IOException { RequesterMain s=new RequesterMain(); s.run(); }
}
ServerMain.java
import java.net.*; import java.io.*; class ServerMain {
ServerSocket server; ObjectOutputStream out; ObjectInputStream in; Socket connection1=null; String message,message1,message2=""; int i=7; ServerMain(){} void run() { try { System.out.println("waiting for connection"); message="hi i am server"; server=new ServerSocket(19999,10); connection1=server.accept(); System.out.println("connected to port"+connection1.getLocalPort()); out=new ObjectOutputStream(connection1.getOutputStream()); in=new ObjectInputStream(connection1.getInputStream()); out.writeObject(message); out.flush(); try { message1=(String)in.readObject(); System.out.println(message1); } catch(ClassNotFoundException e) { System.out.println("Exception is"+e); } int m; int n=Integer.parseInt(message1); message1=""; while(n>0) { m=n%2; message1=message1+m; n=n/2; i--; } System.out.println(message1); n=message1.length(); System.out.println("string in reversed order is "); for(int i=n-1;i>=0;i--) { char c=message1.charAt(i); message2+=c; } out.writeObject(message2); out.flush(); } catch(UnknownHostException ae){ System.out.println("Exception is"+ae); } catch(IOException e) { System.out.println("Exception is"+e); } try { out.close(); in.close(); connection1.close(); } catch(IOException e) { System.out.println("Exception is"+e); } } public static void main(String args[])throws IOException { ServerMain s=new ServerMain(); s.run(); }
}
run first ServerMain in one cmd and ReceiverMain.java in another cmd.
Ads