本文整理汇总了C#中TcpListener.Listen方法的典型用法代码示例。如果您正苦于以下问题:C# TcpListener.Listen方法的具体用法?C# TcpListener.Listen怎么用?C# TcpListener.Listen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TcpListener
的用法示例。
在下文中一共展示了TcpListener.Listen方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
byte[] quote = Encoding.ASCII.GetBytes("Insanity: doing the same thing over and over again and expecting different results. - Albert Einstein");
var loop = new UVLoop();
var listener = new TcpListener("0.0.0.0", 17, loop);
listener.ConnectionAccepted += (Tcp connection) =>
{
connection.ReadCompleted += (ByteSpan data) =>
{
unsafe
{
fixed (byte* pQuote = quote)
{
connection.TryWrite(new ByteSpan(pQuote, quote.Length));
}
}
};
connection.ReadStart();
};
listener.Listen();
loop.Run();
}
示例2: TestCanSendHandles
void TestCanSendHandles(string pipename, IPEndPoint ipep)
{
int count = 0;
Loop.Default.Run(async () => {
var handles = new Stack<Handle>();
var pipelistener = new IPCPipeListener();
pipelistener.Bind(pipename);
pipelistener.Connection += () => {
var client = pipelistener.Accept();
client.Resume();
client.HandleData += (handle, data) => {
handles.Push(handle);
count++;
if (count == 3) {
foreach (var h in handles) {
h.Close();
}
pipelistener.Close();
}
};
};
pipelistener.Listen();
var pipe = new IPCPipe();
await pipe.ConnectAsync(pipename);
var tcplistener = new TcpListener();
tcplistener.Bind(ipep);
tcplistener.Connection += () => {
var client = tcplistener.Accept();
pipe.Write(client, new byte[1], (ex) => {
client.Close();
tcplistener.Close();
});
};
tcplistener.Listen();
var tcp = new Tcp();
await tcp.ConnectAsync(ipep);
tcp.Write("HELLO WORLD");
var udp = new Udp();
udp.Bind(ipep);
pipe.Write(udp, Encoding.Default.GetBytes("UDP"), (ex) => udp.Close());
pipe.Write(pipe, Encoding.Default.GetBytes("pipe"), (ex) => pipe.Close());
});
Assert.Equal(3, count);
}
示例3: RunLoop
static void RunLoop(bool log)
{
var loop = new UVLoop();
var listener = new TcpListener("127.0.0.1", 8080, loop);
var formatter = new BufferFormatter(512, FormattingData.InvariantUtf8);
listener.ConnectionAccepted += (Tcp connection) =>
{
if (log)
{
Console.WriteLine("connection accepted");
}
connection.ReadCompleted += (ByteSpan data) =>
{
if (log)
{
unsafe
{
var requestString = new Utf8String(data.UnsafeBuffer, data.Length);
Console.WriteLine("*REQUEST:\n {0}", requestString.ToString());
}
}
formatter.Clear();
formatter.Append("HTTP/1.1 200 OK");
formatter.Append("\r\n\r\n");
formatter.Append("Hello World!");
if (log)
{
formatter.Format(" @ {0:O}", DateTime.UtcNow);
}
var response = formatter.Buffer.Slice(0, formatter.CommitedByteCount); // formatter should have a property for written bytes
GCHandle gcHandle;
var byteSpan = response.Pin(out gcHandle);
connection.TryWrite(byteSpan);
connection.Dispose();
gcHandle.Free(); // TODO: formatter should format to ByteSpan, to avoid pinning
};
connection.ReadStart();
};
listener.Listen();
loop.Run();
}
示例4: Main
static void Main(string[] args)
{
var loop = new UVLoop();
var listener = new TcpListener("0.0.0.0", 7, loop);
listener.ConnectionAccepted += (Tcp connection) =>
{
connection.ReadCompleted += (data) =>
{
connection.TryWrite(data);
};
connection.ReadStart();
};
listener.Listen();
loop.Run();
}
示例5: RunLoop
static void RunLoop(bool log)
{
var loop = new UVLoop();
var listener = new TcpListener(s_ipAddress, s_port, loop);
var formatter = new BufferFormatter(512, FormattingData.InvariantUtf8);
listener.ConnectionAccepted += (Tcp connection) =>
{
if (log)
{
Console.WriteLine("connection accepted");
}
connection.ReadCompleted += (Span<byte> data) =>
{
if (log)
{
unsafe
{
var requestString = new Utf8String(data);
Console.WriteLine("*REQUEST:\n {0}", requestString.ToString());
}
}
formatter.Clear();
formatter.Append("HTTP/1.1 200 OK");
formatter.Append("\r\n\r\n");
formatter.Append("Hello World!");
if (log)
{
formatter.Format(" @ {0:O}", DateTime.UtcNow);
}
var response = formatter.Buffer.Slice(0, formatter.CommitedByteCount); // formatter should have a property for written bytes
connection.TryWrite(response);
connection.Dispose();
};
connection.ReadStart();
};
listener.Listen();
loop.Run();
}
示例6: RunLoop
static void RunLoop(bool log)
{
var loop = new UVLoop();
var listener = new TcpListener(s_ipAddress, s_port, loop);
listener.ConnectionAccepted += (Tcp connection) =>
{
if (log)
{
Console.WriteLine("connection accepted");
}
connection.ReadCompleted += (data) =>
{
if (log)
{
unsafe
{
var requestString = new Utf8String(data.Span);
Console.WriteLine("*REQUEST:\n {0}", requestString.ToString());
}
}
var formatter = new ArrayFormatter(512, EncodingData.InvariantUtf8);
formatter.Clear();
formatter.Append("HTTP/1.1 200 OK");
formatter.Append("\r\n\r\n");
formatter.Append("Hello World!");
if (log)
{
formatter.Format(" @ {0:O}", DateTime.UtcNow);
}
var segment = formatter.Formatted;
using (var memory = new OwnedPinnedArray<byte>(segment.Array)) {
connection.TryWrite(memory.Memory.Slice(segment.Offset, segment.Count));
connection.Dispose();
}
};
connection.ReadStart();
};
listener.Listen();
loop.Run();
}
示例7: Run
public static void Run(string[] args, Action<ApplicationBuilder> config)
{
// Build middleware pipeline
var appBuilder = new ApplicationBuilder();
config(appBuilder);
var appDelegate = appBuilder.Build();
var loop = new UVLoop();
var listener = new TcpListener("0.0.0.0", 5000, loop);
listener.ConnectionAccepted += (Tcp connection) =>
{
connection.ReadCompleted += (ByteSpan data) =>
{
//unsafe
//{
// var requestString = Encoding.UTF8.GetString(data.UnsafeBuffer, data.Length);
// Console.WriteLine("*REQUEST:\n {0}", requestString.ToString());
//}
var context = new LibuvHttpContext();
// BAD: Single threaded for now, we're ignoring the result of the task
// because we know it's synchronous
appDelegate(context);
context.Response.ContentType = context.Response.ContentType ?? "text/plain";
if (context.Response.StatusCode == 0)
{
context.Response.StatusCode = 200;
}
// Flush and dispose (keep alive not supported)
var contentLength = (int)context.Response.ContentLength;
var responseBuffer = new byte[1024];
var written = 0;
Append(responseBuffer, ref written, "HTTP/1.1 ");
Append(responseBuffer, ref written, context.Response.StatusCode);
Append(responseBuffer, ref written, " ");
Append(responseBuffer, ref written, GetStatusText(context.Response.StatusCode));
Append(responseBuffer, ref written, "\r\n");
Append(responseBuffer, ref written, "Content-Length:");
Append(responseBuffer, ref written, contentLength);
Append(responseBuffer, ref written, "\r\n");
Append(responseBuffer, ref written, "Content-Type:");
Append(responseBuffer, ref written, context.Response.ContentType);
Append(responseBuffer, ref written, "\r\n");
Append(responseBuffer, ref written, "Connection:Close\r\n\r\n");
Append(responseBuffer, ref written, ((LibuvStream)context.Response.Body).GetBuffer());
connection.TryWrite(responseBuffer, written);
connection.Dispose();
};
connection.ReadStart();
};
listener.Listen();
// Console.WriteLine("Listening on ::5000");
loop.Run();
}