Sunday, December 13, 2015

Java networking : UDP vs TCP

Related topic : Java networking : NIO

1. Intro 

Both TCP and UDP are protocols used for sending bits of data — known as packets — over the Internet. They both build on top of the Internet protocol. So, what's the difference ? 
In shorts, UDP - is more simple because it doesn't guarantee package delivery. 

Wiki : 
https://en.wikipedia.org/wiki/User_Datagram_Protocol
The User Datagram Protocol (UDP) is one of the core members of the Internet protocol suite. The protocol was designed by David P. Reed in 1980 and formally defined in RFC 768.
UDP uses a simple connectionless transmission model with a minimum of protocol mechanism. It has no handshaking dialogues, and thus exposes the user's program to any unreliability of the underlying network protocol. There is no guarantee of delivery, ordering, or duplicate protection. UDP provides checksums for data integrity, and port numbers for addressing different functions at the source and destination of the datagram.

https://en.wikipedia.org/wiki/Transmission_Control_Protocol
The Transmission Control Protocol (TCP) is a core protocol of the Internet protocol suite. It originated in the initial network implementation in which it complemented the Internet Protocol (IP). Therefore, the entire suite is commonly referred to as TCP/IP. TCP provides reliable, ordered, and error-checked delivery of a stream of octets between applications running on hosts communicating over an IP network. TCP is the protocol that major Internet applications such as the World Wide Webemailremote administration and file transferrely on. Applications that do not require reliable data stream service may use the User Datagram Protocol (UDP), which provides a connectionless datagram service that emphasizes reduced latency over reliability.




2. UDP Server.

Let's now create a server for UDP protocol. In Java we can use classes DatagramSocket and DatagramPacket. Also I had to create procedure to extract Strings for readed bytes. Server is reading data from socket and sending it back to client.

package com.demien.networking;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

public class UdpServer {
    public final static int PORT_NUMBER = 6666;

    public static String extractString(byte[] array) {
        List<Byte> wrapper=new ArrayList<>();
        for (int i=0; i<array.length;i++) {
            if (array[i]!=0) {
                wrapper.add(array[i]);
            } else {
                break;
            }
        }
        byte[] resultArray=new byte[wrapper.size()];
        for (int i=0; i<wrapper.size();i++) {
            resultArray[i]=wrapper.get(i);
        }


        return  new String(resultArray);
    }

    public static void main(String args[]) throws Exception {
        System.out.println("Starting.... waiting for client connections......");
        try (
                DatagramSocket serverSocket = new DatagramSocket(PORT_NUMBER);
        ) {
            byte[] receiveData = new byte[1024];
            byte[] sendData;
            while (true) {
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                serverSocket.receive(receivePacket);
                String inputLine = extractString(receivePacket.getData());
                System.out.println("received: " + inputLine);
                InetAddress IPAddress = receivePacket.getAddress();
                int port = receivePacket.getPort();
                sendData = inputLine.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
                serverSocket.send(sendPacket);
            }
        }
    }
}


3. UDP Client.

Client application is reading data from system.it(console), sending it to server and showing server responce.

package com.demien.networking;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UdpClient {
    public static void main(String args[]) throws Exception {
        System.out.println("Starting.... ");
        try (
                BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
                DatagramSocket clientSocket = new DatagramSocket();
        ) {
            InetAddress IPAddress = InetAddress.getByName("localhost");
            byte[] sendData;
            byte[] receiveData = new byte[1024];
            System.out.println("Enter message :  ");
            while (true) {
                String userInput = inFromUser.readLine();
                sendData = userInput.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, UdpServer.PORT_NUMBER);
                clientSocket.send(sendPacket);
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                clientSocket.receive(receivePacket);
                String receivedSentence = UdpServer.extractString(receivePacket.getData());
                System.out.println("response from server:" + receivedSentence);
            }
        }

    }
}



4. TCP Server. 

The same logic as for UDP server, but using another classes for TCP protocol.

package com.demien.networking;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {

    public final static int PORT_NUMBER=6666;

    public static void main(String[] args) throws IOException {
        System.out.println("Starting.... waiting for client connections......");
        try (
                ServerSocket serverSocket = new ServerSocket(PORT_NUMBER);
                Socket clientSocket = serverSocket.accept();
                PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        ) {
            System.out.println("Client is connected");
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println("received:"+inputLine);
                out.println(inputLine);
            }
        } catch (IOException e) {
            System.out.println("Exception caught when trying to listen on port "                    + PORT_NUMBER + " or listening for a connection");
            System.out.println(e.getMessage());
        }
    }
}

5. TCP Client.

Also, the same client logic : read from console, send to server, display response.

package com.demien.networking;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpClient {

    public final static String HOST_NAME="localhost";

    public static void main(String[] args) throws IOException {

        System.out.println("Starting.... ");
        try (
                Socket echoSocket = new Socket(HOST_NAME, TcpServer.PORT_NUMBER);
                PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
                BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in))
        ) {
            System.out.println("Connected to server. Enter message :  ");
            String userInput;
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput);
                System.out.println("response from server: " + in.readLine());
            }
        } catch (UnknownHostException e) {
            System.err.println("Unknown host:" + HOST_NAME);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " +
                    HOST_NAME);
            System.exit(1);
        }
    }
}


No comments:

Post a Comment