1 / 26

Lector: Aliyev H.U.

TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES. THE DEPARTMENT OF DATA COMMUNICATION NETWORKS AND SYSTEMS. Lector: Aliyev H.U. Lecture № 6 Design of client-server communication software. TCP-based network programming. Introduction.

fngo
Download Presentation

Lector: Aliyev H.U.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES THE DEPARTMENT OF DATA COMMUNICATION NETWORKS AND SYSTEMS Lector: Aliyev H.U. Lecture №6 Design of client-server communication software.TCP-based network programming

  2. Introduction • In this lesson, we will look in detail at the higher-level network classes provided in the .NET Framework. We'll start with client-server architecture, general introduction to TCP, and its architecture and data structures. • Next, we'll explore the TcpClient and TcpListener classes provided in the .NET Framework for working with TCP. Finally, we'll discuss using the TCP channel with .NET Remoting.

  3. Client-server communication architecture(connection-oriented)

  4. Overview of TCP • TCP, or Transmission Control Protocol, is used as a reliable protocol to communicate across an interconnected network of computers. TCP verifies that the data is properly delivered to the destination in the correct sequence. We'll look briefly at how TCP achieves this in a moment. • TCP is a connection-oriented protocol designed to provide reliable transmission of data from one process to another process running on the same or different computers. The term 'connection-oriented' means that the two processes or applications must establish a TCP connection prior to exchanging any data. This is in contrast to UDP (which we'll look at in the next chapter), which is a 'connection-less' protocol, allowing data to be broadcast to an unknown number of clients.

  5. Encapsulation • When an application sends data using TCP, it travels down the protocol stack. The data is passed through each of the layers and finally transferred across the network as stream of bits. • Each layer in the TCP/IP protocol suite adds some information in the form of headers and/or trailers: When the packet arrives on the other side of the network, it is again passed through each layer from bottom to top. Each layer strips out its header/trailer information to verify the data and finally it reaches the server application in the same form as it left the client application.

  6. TCP Terminology • Before looking at how TCP establishes a connection with another TCP host, there are a number of terms that we need to define: • Segment • The unit of data that TCP sends to IP is called a TCP segment. • Datagram • The unit of data that IP sends to the Network Interface Layer is called an IP datagram. • Sequence Number • Every TCP segment sent over a connection has a number assigned to it, which is called the 'Sequence Number'. This is used to ensure that the data arrives in the correct order.

  7. TCP Headers • To understand how TCP works, we also need to look quickly at the structure of a TCP header:

  8. The sequence and acknowledgement of TCP • The sequence and acknowledgement numbers are used by TCP to ensure that all the data arrives in the correct order, and the control bits contain various flags to indicate the status of the data. There are six of these control bits (usually represented by three-letter abbreviations): • URG-indicates that the segment contains urgent data • ACK-indicates that the segment contains an acknowledgement number • PSH-indicates the data is to be pushed through to the receiving user • RST-resets the connection • SYN-used to synchronize sequence numbers • FIN-indicates the end of data

  9. TCP Connections

  10. TCP Operations

  11. Flow Control • TCP governs the amount of data sent to it by returning a 'window size' with every acknowledgement. A 'window' is the amount of data that the receiver can accept. A data buffer is placed between the application program and the network data flow. The 'window size' is actually the difference between the size of the buffer and the amount of data stored in it. This number is sent to inform the remote host about the current window size. This is called a 'Sliding Window'. The Sliding Window algorithms control the flow for network data transfers:

  12. TCP in .NET programming • .NET support for TCP sockets is a great improvement on the previous programming model. Previously, most developers using Visual C++ have either employed the CSocket and CAsyncSocket classes for manipulating all types of socket communication, or used third-party programming libraries. There was almost no built-in support for higher-level TCP programming. In .NET, there is a separate namespace provided for working with sockets-the System.Net.Sockets namespace (as discussed in the previous chapter). This namespace contains low-level classes such as Socket as well as higher-level classes such as TcpClient and TcpListener to offer simple interfaces to TCP communication. • The TcpClient and TcpListener classes follow the stream model for sending and receiving data, as opposed to the Socket class, which employs the byte-level approach. In these classes, all communication between the client and the socket is based on a stream, using the NetworkStream class. However, we can also work with bytes where necessary.

  13. TCP based programming

  14. TCP Socket Functions

  15. The TcpClient Class • The TcpClient class provides client-side connections for TCP services. It is built upon the Socket class to provide TCP Services at a higher level-TcpClient has a private data member called m_ClientSocket, which is used to communicate with the TCP server. The TcpClient class provides simple methods for connecting to another socket application over the network, and for sending and receiving data to it Public Methods of TcpClient Class

  16. The TcpListener Class • Typically, a server-side application starts by binding to the local endpoint and listening to incoming requests from clients. As soon as a client is found knocking on the port, the application activates by accepting the request and then creating a channel that is then responsible for communicating with the client. • The application continues to listen for more incoming client requests on the main thread. The TcpListener class does exactly that-it listens to the client's request, accepts it, and then creates a new instance of the Socket class or the TcpClient class that we can use to communicate with the client. Just like the TcpClient, the TcpListener also encapsulates a private Socket object, m_ServerSocket, available only to derived classes.

  17. The TcpListener Class • Public Methods

  18. The server application diagram in TCP

  19. Primer on the TcpClient • 1. Passing in an IPEndPoint object representing the remote endpoint we want to connect to: • // Create a new instance of the TcpClient class TcpClientnewClient = new TcpClient(); • // Establish a connection with, the IPEndPoint • IPAddressipAddr = IPAddress.Parse("127.0.0.1"); IPEndPointendPoint = new IPEndPoint(ipAddr, 80); • // Connect with the host using IPEndPointnewClient.Connect(endPoint);

  20. Primer on the TcpClient • 2.Passing in an IPAddress object and a port number: • // Create a new instance of the TcpClient class • TcpClientnewClient = new TcpClient(); • // Establish a connection with the IPEndPoint • IPAddressipAddr = IPAddress.Parse("127.0.0.1"); • // Connect with the host using IPAddress and port number newClient.Connect(ipAddr, 80);

  21. Primer on the TcpClient • Passing in a hostname and port number: • // Create a new instance of the TcpClient class TcpClientnewClient = new TcpClient(); • // Connect with the host using hostname as string and port newClient.Connect("127.0.0.1", 80);

  22. Primer on the TcpClient • If there is a connection failure or other problem, a SocketException will be thrown: • try • { • TcpClientnewClient = new TcpClient(); • // Connect to the server newClient.Connect("192.168.0.1", 80); • // Socket raises exception here // if there is some problem with the // connection • } • catch(SocketExceptionse) • { • Console.WriteLine("Exception: " + se); • }

  23. Primer on the TcpClient • The NetworkStream class is used for stream-level processing as a communication channel between two connected applications. • NetworkStreamtcpStream= newClient,GetStream(); • After getting the stream, we can use the Read() and Write() methods of NetworkStream to actually read from the host application and write to it. • The Write() method takes three parameters-a byte array containing the data we want to send to the host, the position in the stream where we want to start writing, and the length of the data: • byte[] sendBytes = Encoding.ASCII.GetBytes("This is a Test<EOF>"); tcpStream.Write(sendBytes, 0, sendBytes.Length);

  24. Primer on the TcpClient • The Read() method has exactly the same set of parameters-a byte array to store the data that we read from the stream, the position to start reading, and the number of bytes to read: • byte[] bytes = new byte[newClient.ReceiveBufferSize]; intbytesRead = tcpStream.Read(bytes, 0, newClient.ReceiveBufferSize); • // Convert from bytes to string • // returnData will contain the incoming data from socket string returnData = Encoding.ASCII.GetString(bytes); • The TcpClient'sReceiveBufferSize property allows us to get or set the size of the receive buffer (in bytes), so we use it as the size of the byte array. Note that setting this property doesn't restrict the number of bytes we can read in each operation, as the buffer will be dynamically resized if necessary, but it does reduce overhead if we specify a buffer size.

  25. Primer on the TcpClient • Closing a TCP Socket • After communicating with the client, the Close() method should be called to free all resources: • // Close client socket • newClient.Close();

  26. Q&A?

More Related