OO Programming in Java
Class 10 Material
Network
Java is supposed to be about networking .....
Server side ..
import java.awt.*; import java.lang.*; import java.util.*; import java.net.*; import java.io.*; public class View extends Frame{ public static void main(String args[]){ ServerSocket theServer = null; Socket theSocket = null; try { theServer = new ServerSocket(200); System.out.println ("Server ready"); } catch (IOException e) { System.out.println("Server Socket Error - " + e); } while(true) { try { theSocket = theServer.accept(); PrintStream p = new PrintStream(theSocket.getOutputStream()); p.println("You are talking to the Java Server"); theSocket.close(); } catch (IOException e) { System.out.println("Serving Error"); } } } }
Client side ...
import java.awt.*; import java.lang.*; import java.util.*; import java.net.*; import java.io.*; public class View extends Frame{ /* This is the companion to servertest.prj This is the client, servertest is the server */ public static void main(String args[]){ InetAddress address = null; Socket theSocket = null; DataInputStream theInput = null; System.out.println ("Begin Socket Test, Godzilla/Trex"); try { address = InetAddress.getByName("Trex"); System.out.println("Found address = " + address); } catch (UnknownHostException e) { System.out.println("Could not find address = " + address); } try { theSocket = new Socket(address, 200); theInput = new DataInputStream (theSocket.getInputStream()); String theLine = theInput.readLine(); System.out.println("Received - " + theLine); } catch (IOException e) { System.out.println("IO Error during communication with server"); } try {Thread.sleep(5000); } catch (InterruptedException e) {} } }