Skip to content

Modernizing code and remove extra spaces #15252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
345 changes: 177 additions & 168 deletions docs/framework/network-programming/using-tcp-services.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,172 +19,181 @@ ms.assetid: d2811830-3bcb-495c-b82d-cda9cf919aad
---
# Using TCP Services

The <xref:System.Net.Sockets.TcpClient> class requests data from an Internet resource using TCP. The methods and properties of **TcpClient** abstract the details for creating a <xref:System.Net.Sockets.Socket> 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;
}
}
```

<xref:System.Net.Sockets.TcpListener> 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 <xref:System.Net.Sockets.TcpListener.Start%2A> method enables listening, and the <xref:System.Net.Sockets.TcpListener.Stop%2A> method disables listening on the port. The <xref:System.Net.Sockets.TcpListener.AcceptTcpClient%2A> method accepts incoming connection requests and creates a **TcpClient** to handle the request, and the <xref:System.Net.Sockets.TcpListener.AcceptSocket%2A> 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 <xref:System.Net.Sockets.TcpClient> class requests data from an Internet resource using TCP. The methods and properties of **TcpClient** abstract the details for creating a <xref:System.Net.Sockets.Socket> 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;
}
}
```

<xref:System.Net.Sockets.TcpListener> 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 <xref:System.Net.Sockets.TcpListener.Start%2A> method enables listening, and the <xref:System.Net.Sockets.TcpListener.Stop%2A> method disables listening on the port. The <xref:System.Net.Sockets.TcpListener.AcceptTcpClient%2A> method accepts incoming connection requests and creates a **TcpClient** to handle the request, and the <xref:System.Net.Sockets.TcpListener.AcceptSocket%2A> 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;
}

}
```