Lecture 13B
Networking
Internet Basics
IP - Datagrams
TCP - UDP
Application Layer
Ports
Java Networking
URL
InetAddress
Sockets
Client-Server

RMI
Exercises

Lectures

1A: Introduction
1B: Java Intro
2A: BuildingBlocks
2B: Objects
3A: More Objects
3B: Exceptions
       & Threads
4A: Waves
4B: Nuclear &
       Particle
5A: AWT
5B: More AWT
      & Graphics
6A:Detectors &
      Simulation
6B: LHC/Atlas &
     RandomSims    
7A: Swing
7B: Java2D
8A: Java Apps
8B: Dialogs &
    MoreClasses
9A: Java I/O
9B: Utilities,
        Unicode
10A: More
    Threading
10B: File
    Handling
11A: Array,Print,
    Images
11B: SimplePhysics
    Simulation
12A: Tips &
    Techniques
12B: More Tips &
    Techniques
13A: Satellite
    
Simulations
13B: Intro to Java
    Networking
14A: Java Servers
14B: HTTP Server
15A: ServerClient
15B: ServerClient
   Expt.Simulation
16A: Course
          Review
16B: ExerciseTest
        Discussion

    Contacts
    Description
    Exercises
    Index
    Outline
    Q&A
    Resources
    StudentInfoForm
    Student Pages
    What's New

 

Home : Lectures : Lecture 13B : Sockets
Sockets

Sockets provide connections between applications that allow streams of data to flow.

Sockets are fairly straightforward to setup in Java.

Java provides 2 kinds of sockets:

Socket:

  • connection-oriented protocol (i.e. behaves like a telephone line, the connection remains even when no one is talking)

DatagramSocket:

  • a connectionless protocol
  • datagram packets
  • no attempt to maintain a fixed connection
  • does not keep packets in order or guarantee their arrival.

We will talk more about sockets in a later lecture.

Here is a demo showing a socket used to recieve the data from a socket connected to port 37 on a remote computer.

On some machines this port provides a current time service.

DateAtHost.java

 

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

public class DateAtHost extends java.util.Date {
  static int timePort = 37;
  static final long offset= 2208988800L;//Seconds from century
                                        //to Jan 1,1970 00:00GMT

  public DateAtHost( String host ) throws IOException {
   this( host, timePort );
  }

  public DateAtHost( String host, int port ) throws IOException {

    // Open a socket to this host on port 37
    Socket sock = new Socket( host, port );

    // Open an input stream to this socket.
    DataInputStream din =
      new DataInputStream(sock.getInputStream() )
;

    // Now read the integer time value from the socket stream.
    int time = din.readInt();
    sock.close();
    setTime( (((1L << 32) + time) - offset) * 1000 ) ;
  }

  // Example usage: java DateAtHost helio.ora.com
  public static void main (String [] args) {
    try {
      System.out.println( new DateAtHost( args[0] ) );
    } catch (UnknownHostException e){
      System.out.println("Host " + args[0] +" is unknown.");
    } catch (IOException e){
      System.out.println("IO Exception");
    }
  }
}

 

 

Home Lectures Resources Index Contacts Students


Physics Simulations with JavaTM
KTH, Kurskod: 5A1418
Curator: Clark S. Lindsey