import java.io.* import java.net.* class ConnectDemo { public static void main(String args[]) { try { Socket s = new Socket("10.5.18.213",225); DataInputStream inp = new DataInputStream(s.getInputStream()); boolean more_data = true; System.out.println("Established connection"); while(more_data) { String line = inp.readLine(); if(line == null) more_data = false; else System.out.println(line); } } catch(IOException e) { System.out.println("IO error " + e) } } import java.io.*; import java.net.*; class SimpleServer { public static void main(String arg[]) { try { ServerSocket sock = new ServerSocket(7500); Socket newSock = sock.accept(); DataInputStream inp = new DataInputStream(newsock.getInputStream()); PrintStream outp = new PrintStream(newsock.getOutputStream()); outp.println("Hello: enter QUIT to exit); boolean more_data = true; while (more_data) { String line = inp.readLine(); if(line == null) more_data = false; else { outp.println("From the server: " + line + "\n"); if(line.trim().equals("QUIT")) more_data = false; } } newsock.close(); } catch(Exception e) { System.out.println("IO error " + e); } } import java.io.*; import java.net.*; class ThreadHandler extends Thread { Socket newsock; int n; ThreadHandler(Socket s, int v) { newsock = s; n = v; } public void run() { try { DataInputStream inp = new DataInputStream(newsock.getInputStream()); PrintStream outp = new PrintStream(newsock.getOutputStream()); outp.println("Hello::enter QUIT to exit"); boolean more_data = true; while (more_data) { String line = inp.readLine(); if(line == null) more_data = false; else { outp.println("From the server: " + line + "\n"); if(line.trim().equals("QUIT")) more_data = false; } } newsock.close(); } catch (Exception e) { System.out.println("IO error " + e); } } } class ConcurrentServer { public static void main(String args[]) { int nreq = 1; try { ServerSocket sock = new ServerSocket(7500); for (;;) { Socket newsock = sock.accept(); System.out.println("Creating thread ..."); Thread t = new ThreadHandler(newsock, nreq); t.start(); nreq++; } } catch(Exception e) { System.out.println("IO error " + e); } } } import java.io.*; import java.net.*; class DatagramDemo { public static int server_port = 7500; public static int client_port = 7501; public static DatagramSocket dgsock; public static byte buffer[] = new byte[512]; public static void DgServer() throws Exception { System.out.println("Server starts ... "); int ptr = 0; for(;;) { int nextchar = System.in.read(); switch(nextchar) { case -1: System.out.println("Exiting ..."); return; case '\n': dgsock.send (new DatagramPacket(buffer, ptr, inet.Address.getLocalHost(), client_port)); ptr = 0; break; default: buffer[ptr++] = (byte)nextchar; } } } public static void DgClient() throws Exception { System.out.println("Client starts ..."); for (;;) { DatagramPacket pkt = new DatagramPacket(buffer, buffer.length); dgsock.receive(pkt); System.out.println(new String (pkt.getData(), 0, 0, pkt.getLength())); } } public static static void main(String args[]) throws Exception { if(args.length != 1) System.out.println("Wrong number of arguments"); else if (args[0].equals("client")) { dgsock = new DatagramSocket(client_port); DgClient(); } else if (args[0].equals("server")) { dgsock = new DatagramSocket(server_port); DgServer(); } } } }