PJF16

Echoclient + MulServer

Jan 22nd, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.Scanner;
  4.  
  5. class MulClient
  6. {
  7.   public static void main( String[] args )
  8.   {
  9.     Socket server = null;
  10.     try
  11.     {
  12.       server = new Socket( "localhost", 3116 );
  13.       Scanner     in  = new Scanner( server.getInputStream() );
  14.       PrintWriter out = new PrintWriter( server.getOutputStream(), true );
  15.  
  16.       out.println( "2" );
  17.       out.println( "4" );
  18.       System.out.println( in.nextLine() );
  19.  
  20.       server = new Socket( "localhost", 3116 );
  21.       in  = new Scanner( server.getInputStream() );
  22.       out = new PrintWriter( server.getOutputStream(), true );
  23.  
  24.       out.println( "23895737895" );
  25.       out.println( "434589358935857" );
  26.       System.out.println( in.nextLine() );
  27.     }
  28.     catch ( UnknownHostException e ) {
  29.       e.printStackTrace();
  30.     }
  31.     catch ( IOException e ) {
  32.       e.printStackTrace();
  33.     }
  34.     finally {
  35.       if ( server != null )
  36.         try { server.close(); } catch ( IOException e ) { }
  37.     }
  38.   }
  39. }
  40.  
  41. import java.io.*;
  42. import java.math.BigInteger;
  43. import java.net.*;
  44. import java.util.Scanner;
  45.  
  46. public class MulServer
  47. {
  48.   private static void handleConnection( Socket client ) throws IOException
  49.   {
  50.     Scanner     in  = new Scanner( client.getInputStream() );
  51.     PrintWriter out = new PrintWriter( client.getOutputStream(), true );
  52.  
  53.     String factor1 = in.nextLine();
  54.     String factor2 = in.nextLine();
  55.  
  56.     out.println( new BigInteger(factor1).multiply( new BigInteger(factor2) ) );
  57.   }
  58.  
  59.   public static void main( String[] args ) throws IOException
  60.   {
  61.     ServerSocket server = new ServerSocket( 3116 );
  62.  
  63.     while ( true )
  64.     {
  65.       Socket client = null;
  66.  
  67.       try
  68.       {
  69.         client = server.accept();
  70.         handleConnection ( client );
  71.       }
  72.       catch ( IOException e ) {
  73.         e.printStackTrace();
  74.       }
  75.       finally {
  76.         if ( client != null )
  77.           try { client.close(); } catch ( IOException e ) { }
  78.       }
  79.     }
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment