Instructional Objectives
After participating in this practicum, students are expected to be able to:
- Understanding TCP sockets
- Create program implementation.
1. TCP Socket
Transmission Control Protocol (TCP) is a protocol that is in the transport layer that is connection-oriented and reliable. TCP has the following characteristics:
gis between two application layer protocols so that they can communicate with each other.
TCP does not provide one-to-many data delivery services.
- Connection-oriented: Before data can be transmitted between two hosts, two processes running at the application layer must first negotiate to establish a connection session.
TCP connections are closed using the TCP connection termination process. - Full-duplex: For each TCP host, the connection that occurs between the two hosts consists of two paths, namely the outgoing path and the incoming path.
By using lower layer technology that supports full-duplex, data can be simultaneously received and sent.
The TCP header contains the sequence number of the transmitted data and an acknowledgment of the incoming data. - Reliable: Data sent to a TCP connection will be sorted with a packet sequence number and will expect a positive acknowledgment packet from the receiver.
If there is no Acknowledgement packet from the receiver, then the TCP segment (protocol data unit in the TCP protocol) will be retransmitted.
At the receiver end, duplicate segments will be ignored and segments that come out of order will be placed at the back to sort the TCP segments.
To ensure the integrity of each TCP segment, TCP implements the TCP Checksum calculation. - Byte stream: TCP views the data sent and received over the two TCP ingress and egress paths as a contiguous byte stream.
The TCP sequence number and the acknowledgment number in each TCP header are also defined in terms of bytes.
However, TCP does not know the boundaries of the messages within the TCP byte stream.
To do this, it is left to the application layer protocols (in the DARPA Reference Model), which must translate the TCP byte stream into a "language" that they understand. - Having flow control services: To prevent too much data from being sent at one time, which would eventually "congest" the IP internetwork, TCP implements flow control services on the sending side that continuously monitor and limit the amount of data sent at one time.
To prevent the receiving side from receiving data that it cannot buffer, TCP also implements flow control on the receiving side, which indicates the amount of buffer that is still available on the receiving side. - Sending packets "one-to-one": this is because TCP must create a logical circuit between two application layer protocols in order to communicate with each other. TCP does not provide one-to-many data delivery services.
2. Server Program
import java.io.*
import java.net.*
public class simpleServer {
public final static int TESTPORT = 1234
public static void main(String args[]) {
ServerSocket checkServer = null
String line
BufferedReader is = null
DataOutputStream os = null
Socket clientSocket = null
try {
checkServer = new ServerSocket(TESTPORT)
System.out.println("Server Ready ...")
} catch (IOException e) {
System.out.println(e)
}
try {
clientSocket = checkServer.accept()
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))
os = new DataOutputStream(clientSocket.getOutputStream())
} catch (Exception ei) {
ei.printStackTrace()
}
try {
line = is.readLine()
System.out.println("From Client : " + line)
os.writeBytes(line)
if (line.compareTo("unisbank") == 0) {
os.writeBytes("Welcome To Unisbank.")
} else {
os.writeBytes("Sorry, this is private area.")
}
} catch (IOException e) { System.out.println(e)
}
try {
os.close()
is.close()
clientSocket.close()
} catch (IOException ic) {
ic.printStackTrace()
}
}
}
3. Client Program
import java.io.*;
import java.net.*;
public class simpleClient {
public final static int REMOTE_PORT = 1234;
public final static String host = "localhost";
public static void main(String args[]) throws Exception {
Socket cl = null;
BufferedReader is = null;
DataOutputStream os = null;
B ufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String userInput = null;
String output = null;
try {
cl = new Socket(host, REMOTE_PORT);
is = new BufferedReader(new InputStreamReader(cl.getInputStream()));
os = new DataOutputStream(cl.getOutputStream());
} catch(UnknownHostException e1) {
System.out.println("Unknown Host: " + e1);
} catch (IOException e2) {
System.out.println("Erorr io: " + e2);
}
try {
System.out.print("Your University ? ");
userInput = stdin.readLine();
os.writeBytes(userInput + "\n");
} catch (IOException ex) {
System.out.println("Error writing to server..." + ex);
}
try {
output = is.readLine();
System.out.println("From server: " + output);
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
os.close();
cl.close();
} catch (IOException x) {
System.out.println("Error writing...." + x);
}
}
}