Class 23: Java network programming


A simple tcp client
Here's a simple tcp client program that fetches google's front page.
import java.io.*;
import java.net.*;
import java.util.*;

public class ClientA
{
  public static void main(String[] args)
  {
    try
    {
      // Create socket & address, and connect
      Socket s = new Socket();
      InetSocketAddress address = new InetSocketAddress("www.google.com",80);
      s.connect(address);
      System.out.println("Connected: " + s); // just for our own info

      try
      {
	// Send request to server & close socket for output
	PrintWriter out = new PrintWriter(s.getOutputStream(),true);
	out.println("GET / HTTP/1.0\n");
	s.shutdownOutput();

	// Read response and echo to terminal
	Scanner in = new Scanner(s.getInputStream());
	while(in.hasNextLine())
	  System.out.println(in.nextLine());
      }
      finally { s.close(); }      
    }
    catch (IOException e) { e.printStackTrace(); }   
  }
}

Simple client-server: part 1
We wrote a simple client/server system in which the server echo'd back whatever the client sent it ... in all caps! The code assumes the server is running on the host oregano, using port 10001.

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

public class ServerB
{
  public static void main(String[] args)
  {
    try 
    {
      ServerSocket s = new ServerSocket(10001);
      Socket next = null;
      try
      {	
	// Accept connection request & set up input/output streams
	next = s.accept();	
	Scanner in = new Scanner(next.getInputStream());
	PrintWriter out = new PrintWriter(next.getOutputStream(),true);

	// Read line and return capitalized (terminate with BYE)
	while(in.hasNextLine())
	{
	  String line = in.nextLine();
	  if (line.equals("BYE")) { 
	    System.out.println("Terminating: " + next); 
	    break; 
	  }
	  out.println(line.toUpperCase());
	}
      }
      finally { s.close(); if (next != null) next.close(); }      
    }
    catch (IOException e) { e.printStackTrace(); }
  }
}
import java.io.*;
import java.net.*;
import java.util.*;

public class ClientB
{
  public static void main(String[] args)
  {
    try
    {
      // Create socket & address
      Socket s = new Socket();
      InetSocketAddress address = 
	new InetSocketAddress("chipotle.cs.usna.edu",10001);

      // Connect
      s.connect(address);      
      System.out.println("Connected: " + s);

      try
      {
	PrintWriter out = new PrintWriter(s.getOutputStream(),true);
	out.println("the rain in spain falls mainly on the plain\nBYE");
	s.shutdownOutput();

	Scanner in = new Scanner(s.getInputStream());
	while(in.hasNextLine())
	  System.out.println(in.nextLine());
      }
      finally { s.close(); }
      
    }
    catch (IOException e) { e.printStackTrace(); }   
  }
}

Simple client-server: part 2
Next, we extended our server so that it could serve an arbitrary number of clients - even simultaneously. This is done by spawning a new thread to serve each client that connects.

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

public class ServerB
{
  public static void main(String[] args)
  {
    try 
    {
      // Create server socket & deal with incoming connection requests
      ServerSocket s = new ServerSocket(10001);
      try {
	while(true)
	{
	  Socket next = s.accept();
	  Thread t = new Thread(new ServeOne(next));
	  t.start();
	}
      }
      finally { s.close(); }
    }
    catch (IOException e) { e.printStackTrace(); }
  }

  static class ServeOne implements Runnable
  {
    Socket s;
    public ServeOne(Socket sk) { s = sk; }
    public void run()
    {
      try
      {
	try
	{
	  // Set up I/O for the socket
	  Scanner in = new Scanner(s.getInputStream());
	  PrintWriter out = new PrintWriter(s.getOutputStream(),true);

	  // Read line & print capitalized (or exit with BYE)
	  while(in.hasNextLine())
	  {
	    String line = in.nextLine();
	    if (line.equals("BYE")) { 
	      System.out.println("Terminating: " + s); 
	      break; 
	    }
	    out.println(line.toUpperCase());
	  }
	}
	finally { s.close(); }      
      }
      catch(IOException e) { e.printStackTrace(); }      
    }
  }
}
import java.io.*;
import java.net.*;
import java.util.*;

public class ClientB
{
  public static void main(String[] args)
  {
    try
    {
      // Create socket & address
      Socket s = new Socket();
      InetSocketAddress address = 
	new InetSocketAddress("chipotle.cs.usna.edu",10001);

      // Connect
      s.connect(address);      
      System.out.println("Connected: " + s);

      try
      {
	PrintWriter out = new PrintWriter(s.getOutputStream(),true);
	out.println("the rain in spain falls mainly on the plain\nBYE");
	s.shutdownOutput();

	Scanner in = new Scanner(s.getInputStream());
	while(in.hasNextLine())
	  System.out.println(in.nextLine());
      }
      finally { s.close(); }
      
    }
    catch (IOException e) { e.printStackTrace(); }   
  }
}


Christopher W Brown
Last modified: Wed Aug 19 11:00:04 EDT 2009