TCP and UDP Socket Programming

Tom Kelliher, CS 325

Feb. 25, 2011

Administrivia

Announcements

Assignment II due Monday.

Assignment

Read 3.1-3.3.

From Last Time

Wireshark labs.

Outline

  1. Introduction.

  2. TCP socket example.

  3. UDP socket example.

Coming Up

Transport layer introduction and UDP.

Introduction

Basics:

  1. Hostname and port are used to specify transport endpoints.

  2. Socket -- the communication object.

  3. TCP properties: reliable, connection-oriented, byte-stream, connection established before application-level protocols exchange information, two-way communication.

  4. UDP properties: unreliable, packet-switched, packet data, no connection overhead, application-level protocols exchange information immediately, two-way communication.

A socket connection is a 4-tuple -- (HostA, PortA, HostB, PortB) -- uniquely defining the connection.

\begin{figure}\centering\includegraphics[width=4in]{Figures/fig02_31.eps}\end{figure}

TCP Socket Example

TCP client/server communication flow:

\begin{figure}\centering\includegraphics[width=5in]{Figures/fig02_32.eps}\end{figure}

TCPClient.java communication model:

\begin{figure}\centering\includegraphics[width=4in]{Figures/fig02_33.eps}\end{figure}

TCPServer.java communication model (UDP figure):

\begin{figure}\centering\includegraphics[width=4in]{Figures/fig02_36.eps}\end{figure}

TCP server code:

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

class TCPServer 
{
   public static void main(String argv[]) throws Exception
   {
      String clientSentence;
      String capitalizedSentence;
      ServerSocket welcomeSocket = new ServerSocket(6789);

      while(true) 
      {
         Socket connectionSocket = welcomeSocket.accept();

         BufferedReader inFromClient =
            new BufferedReader(
            new InputStreamReader(
            connectionSocket.getInputStream()));

         DataOutputStream outToClient =
            new DataOutputStream(
            connectionSocket.getOutputStream());

         clientSentence = inFromClient.readLine();

         capitalizedSentence = clientSentence.toUpperCase() + '\n';

         outToClient.writeBytes(capitalizedSentence);
      }
   }
}

TCP client code:

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

class TCPClient 
{
   public static void main(String argv[]) throws Exception
   {
      String sentence;
      String modifiedSentence;

      BufferedReader inFromUser = 
         new BufferedReader(
         new InputStreamReader(System.in));

      Socket clientSocket = new Socket("hostname", 6789);

      DataOutputStream outToServer =
         new DataOutputStream(
         clientSocket.getOutputStream());

      BufferedReader inFromServer =
         new BufferedReader(
         new InputStreamReader(
         clientSocket.getInputStream()));

      sentence = inFromUser.readLine();

      outToServer.writeBytes(sentence + '\n');

      modifiedSentence = inFromServer.readLine();

      System.out.println("FROM SERVER: " + modifiedSentence);

      clientSocket.close();
   }
}

UDP Socket Example

UDP client/server communication flow:

\begin{figure}\centering\includegraphics[width=5in]{Figures/fig02_34.eps}\end{figure}

UDPClient.java communication model:

\begin{figure}\centering\includegraphics[width=3.9in]{Figures/fig02_35.eps}\end{figure}

UDPServer.java communication model:

\begin{figure}\centering\includegraphics[width=4in]{Figures/fig02_36.eps}\end{figure}

UDP server code:

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

class UDPServer 
{
   public static void main(String args[]) throws Exception
   {

      DatagramSocket serverSocket = 
         new DatagramSocket(9876);

      byte[] receiveData = new byte[1024];
      byte[] sendData;

      while(true)
      {
         DatagramPacket receivePacket =
            new DatagramPacket(receiveData, receiveData.length);

         serverSocket.receive(receivePacket);

         String sentence = new String(receivePacket.getData(),
                                      0, receivePacket.getLength());

         InetAddress IPAddress = receivePacket.getAddress();

         int port = receivePacket.getPort();

         String capitalizedSentence = sentence.toUpperCase();

         sendData = capitalizedSentence.getBytes();

         DatagramPacket sendPacket =
            new DatagramPacket(sendData, sendData.length,
                               IPAddress, port);

         serverSocket.send(sendPacket);
      }
   }
}

UDP client code:

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

class UDPClient 
{
   public static void main(String args[]) throws Exception
   {
      BufferedReader inFromUser =
         new BufferedReader(
         new InputStreamReader(System.in));

      DatagramSocket clientSocket = new DatagramSocket();

      // Replace hostname with the FQDN of the server.

      InetAddress IPAddress = InetAddress.getByName("hostname");

      byte[] sendData;

      byte[] receiveData = new byte[1024];

      String sentence = inFromUser.readLine();

      sendData = sentence.getBytes();

      DatagramPacket sendPacket =
         new DatagramPacket(sendData, sendData.length,
                            IPAddress, 9876);

      clientSocket.send(sendPacket);

      DatagramPacket receivePacket =
         new DatagramPacket(receiveData, receiveData.length);

      clientSocket.receive(receivePacket);

      String modifiedSentence = new String(receivePacket.getData(),
                                           0, receivePacket.getLength());

      System.out.println("FROM SERVER: " + modifiedSentence);

      clientSocket.close();
   }
}



Thomas P. Kelliher 2011-02-25
Tom Kelliher