本文整理汇总了C#中System.Net.Sockets.NetworkStream.WriteTimeout属性的典型用法代码示例。如果您正苦于以下问题:C# NetworkStream.WriteTimeout属性的具体用法?C# NetworkStream.WriteTimeout怎么用?C# NetworkStream.WriteTimeout使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Net.Sockets.NetworkStream
的用法示例。
在下文中一共展示了NetworkStream.WriteTimeout属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Examples.System.Net
{
public class TCPListenerExample
{
public static void Main()
{
// Create the server side connection and
// start listening for clients.
TcpListener tcpListener = new TcpListener(IPAddress.Any,11000);
tcpListener.Start();
Console.WriteLine("Waiting for a connection....");
// Accept the pending client connection.
TcpClient tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine("Connection accepted.");
// Get the stream to write the message
// that will be sent to the client.
NetworkStream networkStream = tcpClient.GetStream();
string responseString = "Hello.";
// Set the write timeout to 10 millseconds.
networkStream.WriteTimeout = 10;
// Convert the message to a byte array and sent it to the client.
Byte[] sendBytes = Encoding.UTF8.GetBytes(responseString);
networkStream.Write(sendBytes, 0, sendBytes.Length);
Console.WriteLine("Message Sent.");
// Close the connection to the client.
tcpClient.Close();
// Stop listening for incoming connections
// and close the server.
tcpListener.Stop();
}
}
}