本文整理汇总了C#中System.Net.Sockets.TcpListener.AcceptTcpClient方法的典型用法代码示例。如果您正苦于以下问题:C# TcpListener.AcceptTcpClient方法的具体用法?C# TcpListener.AcceptTcpClient怎么用?C# TcpListener.AcceptTcpClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.TcpListener
的用法示例。
在下文中一共展示了TcpListener.AcceptTcpClient方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
/**
* The following sample is intended to demonstrate how to use a
* TcpListener for synchronous communcation with a TCP client
* It creates a TcpListener that listens on the specified port (13000).
* Any TCP client that wants to use this TcpListener has to explicitly connect
* to an address obtained by the combination of the server
* on which this TcpListener is running and the port 13000.
* This TcpListener simply echoes back the message sent by the client
* after translating it into uppercase.
* Refer to the related client in the TcpClient class.
*/
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class TcpListenerSample
{
static void Main(string[] args)
{
try
{
// set the TcpListener on port 13000
int port = 13000;
TcpListener server = new TcpListener(IPAddress.Any, port);
// Start listening for client requests
server.Start();
// Buffer for reading data
byte[] bytes = new byte[1024];
string data;
//Enter the listening loop
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length);
while (i != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine(String.Format("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(String.Format("Sent: {0}", data));
i = stream.Read(bytes, 0, bytes.Length);
}
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("Hit enter to continue...");
Console.Read();
}
}
示例2: TcpListener.AcceptTcpClient()
//引入命名空间
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class NetworkOrderSrvr
{
public static void Main()
{
int recv;
byte[] data = new byte[1024];
TcpListener server = new TcpListener(9050);
server.Start();
TcpClient client = server.AcceptTcpClient();
NetworkStream ns = client.GetStream();
string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
ns.Write(data, 0, data.Length);
ns.Flush();
data = new byte[2];
recv = ns.Read(data, 0, data.Length);
short test1t = BitConverter.ToInt16(data, 0);
short test1 = IPAddress.NetworkToHostOrder(test1t);
Console.WriteLine("received test1 = {0}", test1);
data = new byte[4];
recv = ns.Read(data, 0, data.Length);
int test2t = BitConverter.ToInt32(data, 0);
int test2 = IPAddress.NetworkToHostOrder(test2t);
Console.WriteLine("received test2 = {0}", test2);
data = new byte[8];
recv = ns.Read(data, 0, data.Length);
long test3t = BitConverter.ToInt64(data, 0);
long test3 = IPAddress.NetworkToHostOrder(test3t);
Console.WriteLine("received test3 = {0}", test3);
ns.Close();
client.Close();
server.Stop();
}
}