JAVA -  #11


1. java.net
URL URLConnection
Socket, ServerSocket, DatagramSocket, InetAddress
ContentHandler, URLStreamHandler

2. MalformedURLException
URL queryWeb = new URL("http://info.cern.ch/oldFin/lemor.txt");
   try {
       InputStream is = queryWeb.openStream();
       DataInputStream ds = new DataInputStream(is);
          ...
    } catch (IOException e) {
          System.out.println("Problem with network connection"); }
3. Socket/ServerSocket client/server TCP/IP
native methods host, port

4. Server side

import java.net.*;
import java.io.*;

public class SimpleServer {

    public static void main(String[] args) {
        int port = Integer.parseInt(args[0]);

        try {  
            ServerSocket server =
                new ServerSocket(port);
            System.out.println("Started on port " + port);
            Socket client = server.accept();
            InputStream input = client.getInputStream();
            OutputStream output = client.getOutputStream();

            BufferedReader reader =
                new BufferedReader(
                    new InputStreamReader(input));
            PrintWriter writer =
                new PrintWriter(
                    new OutputStreamWriter(output),true);

            writer.println("Hello client");
            writer.println("What is your name?");
            String name = reader.readLine();
            // ...
        } catch (IOException ioe) {
            System.out.println("Error: " + ioe);
            System.exit(1);
        } 
    }
}

5. Client side

import java.net.*;
import java.io.*;

public class SimpleClient {

    public static void main(String[] args) {
        int port = Integer.parseInt(args[0]);

        try {
            InetAddress addr = InetAddress.getByName(null);
// Alternatively:
//          InetAddress addr = InetAddress.getByName("localhost");
            Socket socket = new Socket(addr,port);
            InputStream input = socket.getInputStream();
            OutputStream output = socket.getOutputStream();

            BufferedReader reader =
                new BufferedReader(
                    new InputStreamReader(input));
            PrintWriter writer =
                new PrintWriter(
                    new OutputStreamWriter(output),true);

            while(true) {
                String message = reader.readLine();
                if (message.equals("What is your name?")) {
                    writer.println("What do you care...");
                }
                // ...
            }
        } catch (IOException ioe) {
            // ...
        } finally {
            System.out.println("closing");
//          socket.close();
        }
    }
}

6. Multi-client server

import java.net.*;
import java.io.*;

public class MultipleClientServer {

    public static void main(String[] args) {
        int port = Integer.parseInt(args[0]);

        ServerSocket server = null;
        try {
            server = new ServerSocket(port);
        } catch (IOException ioe) {
            System.err.println("Cannot initiate the server "+ioe);
            return;
        }

        while(true) {
            Socket client = server.accept();
            new ConversationThread(client).start();
        }
    }

}

class ConversationThread extends Thread {

    private Socket client;

    public ConversationThread(Socket client) {
        this.client=client;
    }

    public void run() {
        InputStream input = client.getInputStream();
        OutputStream output = client.getOutputStream();

        BufferedReader reader =
            new BufferedReader(
                new InputStreamReader(input));
        PrintWriter writer =
            new PrintWriter(
                new OutputStreamWriter(output),true);

        while(true) {
            try {
                String request = reader.readLine();
                // ...
            } catch (IOException ioe) {
                // ...
            }
        }
    }
}

7. ServerSocket ss = new ServerSocket(3000);
   Socket sock = ss.accept();
   InetAddress addr = sock.getInetAddress();
   System.out.println("Address: " + addr.toString());
   System.out.println("Address:"+addr.getHostName());

getByName()  getAllByName() getLocalHost()

8. import java.applet.*;
   import java.net.*;
   import java.io.*;
   import java.awt.*;

   public class safeConn extends Applet {
      Socket conn;
      int port = 13;
      String host;
      BufferedInputStream instream;
      String time;
      public void init() {
        try {
         URL tt = new URL(this.getDocumentBase(),
                               "somefile");
         host = tt.getHost();
        } catch (Exception e) {
            System.out.println("Error: " + e); }
        try {
         conn = new Socket(host, port);
         instream = new          
          BufferedInputStream(conn.getInputStream());
        } catch (Exception e) {
            System.out.println("Error: " + e);
            System.exit(1); }
      }
      public void start() {
      // Read a line from the socket.
        time = this.getline(); }
      public void paint(Graphics g) {
        g.drawString("Host: "+host+" Time: "+time,10,
                         10); }
      public String getline() {
         ...    }

9. close()