转自:https://gist.github.com/jamesmanning/2622054
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
StartListener();
ConnectAsTcpClient();
Console.ReadLine();
} private static async void ConnectAsTcpClient()
{
using (var tcpClient = new TcpClient())
{
Console.WriteLine("[Client] Connecting to server");
await tcpClient.ConnectAsync("127.0.0.1", );
Console.WriteLine("[Client] Connected to server");
using (var networkStream = tcpClient.GetStream())
{
Console.WriteLine("[Client] Writing request {0}", ClientRequestString);
await networkStream.WriteAsync(ClientRequestBytes, , ClientRequestBytes.Length); var buffer = new byte[];
var byteCount = await networkStream.ReadAsync(buffer, , buffer.Length);
var response = Encoding.UTF8.GetString(buffer, , byteCount);
Console.WriteLine("[Client] Server response was {0}", response);
}
}
} private static readonly string ClientRequestString = "Some HTTP request here";
private static readonly byte[] ClientRequestBytes = Encoding.UTF8.GetBytes(ClientRequestString); private static readonly string ServerResponseString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><document><userkey>key</userkey> <machinemode>1</machinemode><serial>0000</serial><unitname>Device</unitname><version>1</version></document>\n";
private static readonly byte[] ServerResponseBytes = Encoding.UTF8.GetBytes(ServerResponseString); private static async void StartListener()
{
var tcpListener = TcpListener.Create();
tcpListener.Start();
var tcpClient = await tcpListener.AcceptTcpClientAsync();
Console.WriteLine("[Server] Client has connected");
using (var networkStream = tcpClient.GetStream())
{
var buffer = new byte[];
Console.WriteLine("[Server] Reading from client");
var byteCount = await networkStream.ReadAsync(buffer, , buffer.Length);
var request = Encoding.UTF8.GetString(buffer, , byteCount);
Console.WriteLine("[Server] Client wrote {0}", request);
await networkStream.WriteAsync(ServerResponseBytes, , ServerResponseBytes.Length);
Console.WriteLine("[Server] Response has been written");
}
}
}
}