
2. Tcp Listener
TCP Listener
Introduction
The Transmission Control Protocol (TCP) is a standard that defines how to establish and maintain a network conversation through which applications can exchange data. TCP works with the Internet Protocol (IP), which defines how computers send packets of data to each other.
Network Protocols
TCP
TCP stands for Transmission Control Protocol. This protocol ensures the successful delivery of data from sender to receiver. While sending, it divides the data into packets and numbers each packet, and ensures the sequence of data after reaching the receiver end. If a packet is lost during the transmission, TCP has a system to identify lost packets and sends a request to retransmit these lost packets. It provides extensive error checking mechanisms.
In .Net, we use TcpListener
and TcpClient
classes for TCP connection. TcpListener
has an option to limit the number of connections, for example, a maximum of 10 users can connect at a time.
UDP
UDP stands for User Datagram Protocol. This protocol simply sends the data without any acknowledgement like TCP. For example, if we send 10 packets of data, it doesn't check whether all packets have arrived. It just sends the data.
RDP
RDP (Reliable Data Protocol) is better than UDP. While sending data, it tags them (these tags are unique). Even if the sequence of the data is messed up during transmission, after all data packets arrive, it rearranges them in the correct order.
FTP
FTP stands for File Transfer Protocol. This protocol is used for transferring files from one location to another.
HTTP
HTTP stands for Hyper Text Transfer Protocol. This protocol is used for sending web requests over the internet.
Software side (OS)
Application Layer
The application layer is the OSI layer closest to the end user, which means that both the OSI application layer and the user interact directly with the software application.
Presentation Layer
The presentation layer is responsible for the delivery and formatting of information to the application layer for further processing or display. It relieves the application layer of concern regarding syntactical differences in data representation within the end-user systems. An example of a presentation service would be the conversion of an EBCDIC-coded text computer file to an ASCII-coded file.
Session Layer
The session layer is responsible for setting up, coordinating, and terminating conversations, exchanges, and dialogs between the applications at each end. It deals with session and connection coordination.
Transport Layer
The transport layer is responsible for providing the Application layer with session and datagram communication services. The transport layer provides services such as connection-oriented communication, reliability, flow control, and multiplexing.
// Create a TcpListener instance
TcpListener server = new TcpListener(IPAddress.Any, 8080);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while(true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
// Shutdown and end connection
client.Close();
}
This code demonstrates a basic TCP Listener in C#. It creates an instance of TcpListener that listens for incoming connection requests on a specified IP address and port number. It then enters a loop where it waits for a client to connect, reads data from the client, converts it to uppercase, and sends the converted data back to the client.
Reference
The content in this document is based on the original notes provided in Azerbaijani. For further details, you can refer to the original document using the following link:
Original Note - Azerbaijani Version