From 30e4635b476f951b6f905857938cda713e775cbd Mon Sep 17 00:00:00 2001 From: Youssef Victor <31348972+Youssef1313@users.noreply.github.com> Date: Thu, 17 Oct 2019 19:37:02 +0200 Subject: [PATCH] Modernizing code and remove extra spaces --- .../network-programming/using-tcp-services.md | 345 +++++++++--------- 1 file changed, 177 insertions(+), 168 deletions(-) diff --git a/docs/framework/network-programming/using-tcp-services.md b/docs/framework/network-programming/using-tcp-services.md index a126589503d31..3ddf7ef3c66b1 100644 --- a/docs/framework/network-programming/using-tcp-services.md +++ b/docs/framework/network-programming/using-tcp-services.md @@ -19,172 +19,181 @@ ms.assetid: d2811830-3bcb-495c-b82d-cda9cf919aad --- # Using TCP Services -The class requests data from an Internet resource using TCP. The methods and properties of **TcpClient** abstract the details for creating a for requesting and receiving data using TCP. Because the connection to the remote device is represented as a stream, data can be read and written with .NET Framework stream-handling techniques. - - The TCP protocol establishes a connection with a remote endpoint and then uses that connection to send and receive data packets. TCP is responsible for ensuring that data packets are sent to the endpoint and assembled in the correct order when they arrive. - - To establish a TCP connection, you must know the address of the network device hosting the service you need and you must know the TCP port that the service uses to communicate. The Internet Assigned Numbers Authority (Iana) defines port numbers for common services (see [Service Name and Transport Protocol Port Number Registry](https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml)). Services not on the Iana list can have port numbers in the range 1,024 to 65,535. - - The following example demonstrates setting up a **TcpClient** to connect to a time server on TCP port 13. - -```vb -Imports System -Imports System.Net.Sockets -Imports System.Text - -Public Class TcpTimeClient - Private const portNum As Integer = 13 - Private const hostName As String = "host.contoso.com" - - ' Entry point that delegates to C-style main Private Function. - Public Overloads Shared Sub Main() - System.Environment.ExitCode = _ - Main(System.Environment.GetCommandLineArgs()) - End Sub - - Overloads Public Shared Function Main(args() As [String]) As Integer - Try - Dim client As New TcpClient(hostName, portNum) - - Dim ns As NetworkStream = client.GetStream() - - Dim bytes(1024) As Byte - Dim bytesRead As Integer = ns.Read(bytes, 0, bytes.Length) - - Console.WriteLine(Encoding.ASCII.GetString(bytes, 0, bytesRead)) - - Catch e As Exception - Console.WriteLine(e.ToString()) - End Try - - client.Close() - - Return 0 - End Function 'Main -End Class 'TcpTimeClient -``` - -```csharp -using System; -using System.Net.Sockets; -using System.Text; - -public class TcpTimeClient { - private const int portNum = 13; - private const string hostName = "host.contoso.com"; - - public static int Main(String[] args) { - try { - TcpClient client = new TcpClient(hostName, portNum); - - NetworkStream ns = client.GetStream(); - - byte[] bytes = new byte[1024]; - int bytesRead = ns.Read(bytes, 0, bytes.Length); - - Console.WriteLine(Encoding.ASCII.GetString(bytes,0,bytesRead)); - - client.Close(); - - } catch (Exception e) { - Console.WriteLine(e.ToString()); - } - - return 0; - } -} -``` - - is used to monitor a TCP port for incoming requests and then create either a **Socket** or a **TcpClient** that manages the connection to the client. The method enables listening, and the method disables listening on the port. The method accepts incoming connection requests and creates a **TcpClient** to handle the request, and the method accepts incoming connection requests and creates a **Socket** to handle the request. - - The following example demonstrates creating a network time server using a **TcpListener** to monitor TCP port 13. When an incoming connection request is accepted, the time server responds with the current date and time from the host server. - -```vb -Imports System -Imports System.Net.Sockets -Imports System.Text - -Public Class TcpTimeServer - - Private const portNum As Integer = 13 - - ' Entry point that delegates to C-style main Private Function. - Public Overloads Shared Sub Main() - System.Environment.ExitCode = _ - Main(System.Environment.GetCommandLineArgs()) - End Sub - - Overloads Public Shared Function Main(args() As [String]) As Integer - Dim done As Boolean = False - - Dim listener As New TcpListener(portNum) - - listener.Start() - - While Not done - Console.Write("Waiting for connection...") - Dim client As TcpClient = listener.AcceptTcpClient() - - Console.WriteLine("Connection accepted.") - Dim ns As NetworkStream = client.GetStream() - - Dim byteTime As Byte() = _ - Encoding.ASCII.GetBytes(DateTime.Now.ToString()) - - Try - ns.Write(byteTime, 0, byteTime.Length) - ns.Close() - client.Close() - Catch e As Exception - Console.WriteLine(e.ToString()) - End Try - End While - - listener.Stop() - - Return 0 - End Function 'Main -End Class 'TcpTimeServer -``` - -```csharp -using System; -using System.Net.Sockets; -using System.Text; - -public class TcpTimeServer { - - private const int portNum = 13; - - public static int Main(String[] args) { - bool done = false; - - TcpListener listener = new TcpListener(portNum); - - listener.Start(); - - while (!done) { - Console.Write("Waiting for connection..."); - TcpClient client = listener.AcceptTcpClient(); - - Console.WriteLine("Connection accepted."); - NetworkStream ns = client.GetStream(); - - byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString()); - - try { - ns.Write(byteTime, 0, byteTime.Length); - ns.Close(); - client.Close(); - } catch (Exception e) { - Console.WriteLine(e.ToString()); - } - } - - listener.Stop(); - - return 0; - } - -} +The class requests data from an Internet resource using TCP. The methods and properties of **TcpClient** abstract the details for creating a for requesting and receiving data using TCP. Because the connection to the remote device is represented as a stream, data can be read and written with .NET Framework stream-handling techniques. + +The TCP protocol establishes a connection with a remote endpoint and then uses that connection to send and receive data packets. TCP is responsible for ensuring that data packets are sent to the endpoint and assembled in the correct order when they arrive. + +To establish a TCP connection, you must know the address of the network device hosting the service you need and you must know the TCP port that the service uses to communicate. The Internet Assigned Numbers Authority (Iana) defines port numbers for common services (see [Service Name and Transport Protocol Port Number Registry](https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml)). Services not on the Iana list can have port numbers in the range 1,024 to 65,535. + +The following example demonstrates setting up a **TcpClient** to connect to a time server on TCP port 13: + +```vb +Imports System.Net.Sockets +Imports System.Text + +Public Class TcpTimeClient + Private const portNum As Integer = 13 + Private const hostName As String = "host.contoso.com" + + ' Entry point that delegates to C-style main Private Function. + Public Overloads Shared Sub Main() + System.Environment.ExitCode = _ + Main(System.Environment.GetCommandLineArgs()) + End Sub + + Overloads Public Shared Function Main(args As String()) As Integer + Try + Dim client As New TcpClient(hostName, portNum) + + Dim ns As NetworkStream = client.GetStream() + + Dim bytes(1024) As Byte + Dim bytesRead As Integer = ns.Read(bytes, 0, bytes.Length) + + Console.WriteLine(Encoding.ASCII.GetString(bytes, 0, bytesRead)) + + Catch e As Exception + Console.WriteLine(e.ToString()) + End Try + + client.Close() + + Return 0 + End Function +End Class +``` + +```csharp +using System; +using System.Net.Sockets; +using System.Text; + +public class TcpTimeClient +{ + private const int portNum = 13; + private const string hostName = "host.contoso.com"; + + public static int Main(String[] args) + { + try + { + var client = new TcpClient(hostName, portNum); + + NetworkStream ns = client.GetStream(); + + byte[] bytes = new byte[1024]; + int bytesRead = ns.Read(bytes, 0, bytes.Length); + + Console.WriteLine(Encoding.ASCII.GetString(bytes,0,bytesRead)); + + client.Close(); + + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + } + + return 0; + } +} +``` + + is used to monitor a TCP port for incoming requests and then create either a **Socket** or a **TcpClient** that manages the connection to the client. The method enables listening, and the method disables listening on the port. The method accepts incoming connection requests and creates a **TcpClient** to handle the request, and the method accepts incoming connection requests and creates a **Socket** to handle the request. + +The following example demonstrates creating a network time server using a **TcpListener** to monitor TCP port 13. When an incoming connection request is accepted, the time server responds with the current date and time from the host server. + +```vb +Imports System.Net.Sockets +Imports System.Text + +Public Class TcpTimeServer + + Private const portNum As Integer = 13 + + ' Entry point that delegates to C-style main Private Function. + Public Overloads Shared Sub Main() + System.Environment.ExitCode = _ + Main(System.Environment.GetCommandLineArgs()) + End Sub + + Overloads Public Shared Function Main(args As String()) As Integer + Dim done As Boolean = False + + Dim listener As New TcpListener(portNum) + + listener.Start() + + While Not done + Console.Write("Waiting for connection...") + Dim client As TcpClient = listener.AcceptTcpClient() + + Console.WriteLine("Connection accepted.") + Dim ns As NetworkStream = client.GetStream() + + Dim byteTime As Byte() = _ + Encoding.ASCII.GetBytes(DateTime.Now.ToString()) + + Try + ns.Write(byteTime, 0, byteTime.Length) + ns.Close() + client.Close() + Catch e As Exception + Console.WriteLine(e.ToString()) + End Try + End While + + listener.Stop() + + Return 0 + End Function +End Class +``` + +```csharp +using System; +using System.Net.Sockets; +using System.Text; + +public class TcpTimeServer +{ + + private const int portNum = 13; + + public static int Main(String[] args) + { + bool done = false; + + var listener = new TcpListener(portNum); + + listener.Start(); + + while (!done) + { + Console.Write("Waiting for connection..."); + TcpClient client = listener.AcceptTcpClient(); + + Console.WriteLine("Connection accepted."); + NetworkStream ns = client.GetStream(); + + byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString()); + + try + { + ns.Write(byteTime, 0, byteTime.Length); + ns.Close(); + client.Close(); + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + } + } + + listener.Stop(); + + return 0; + } + +} ```