1 / 5

Client/Server example

Client/Server example. Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static final int PORT = 1301; // port out of the range of 1-1024 public static void main(String args[]) throws IOException {

Download Presentation

Client/Server example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Client/Server example

  2. Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static final int PORT = 1301; // port out of the range of 1-1024 public static void main(String args[]) throws IOException { PrimeServer server = new PrimeServer(); server.go(); } public void go() throws IOException { Socket soc = null; sSoc = new ServerSocket(PORT);

  3. while(true) { soc = sSoc.accept(); // blocks until a connection occurs PrintWriter pw = new PrintWriter( //creating an OutputStream object new OutputStreamWriter( soc.getOutputStream()),true); BufferedReader br = new BufferedReader( new InputStreamReader( soc.getInputStream())); int num = Integer.parseInt( br.readLine() ); pw.println( prime(num) ); pw.close(); br.close(); soc.close(); } } String prime( int num ) { for(int i=2; i*i<= num; i++) if( num%i==0 ) return(num +" is not a primary number."); return(num +" is a primary number."); } } //close class

  4. Client import java.net.*; import java.io.*; public class PrimeClient { public static final int PORT = 1301;// port out of the range of 1-1024 String hostName; Socket soc; public static void main(String args[]) throws IOException { //replace localhost =>args[0] or with url PrimeClient client = new PrimeClient("localhost"); client.go(); } public PrimeClient(String hostString) { this.hostName = hostString; } String readInput() throws IOException { BufferedReader in =new BufferedReader( new InputStreamReader(System.in)); return( in.readLine() ); }

  5. public void go() throws IOException { soc = new Socket(hostName, PORT); BufferedReader ibr = new BufferedReader( new InputStreamReader( soc.getInputStream())); PrintWriter pw = new PrintWriter( new OutputStreamWriter( soc.getOutputStream()),true); System.out.println("************** Check Prime *************"); System.out.println("Enter a number."); pw.println( readInput() ); System.out.println(ibr.readLine()); ibr.close(); pw.close(); soc.close(); } } //close class

More Related