好记性不如烂笔头。

TCP发送请求,启用后台线程侦听接收TcpClient传入请求

static void Main(string[] args)
{
    Thread thread = new Thread(new ThreadStart(Listen));
    thread.Start();
}

/// <summary>
/// 侦听接收TcpClient发送来的数据
/// </summary>
public static void Listen()
{
    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
    int port = 2112;
    TcpListener tcpListener = new TcpListener(localAddr,port);
    tcpListener.Start();

    TcpClient tcpClient = tcpListener.AcceptTcpClient();
    NetworkStream ns = tcpClient.GetStream();
    StreamReader sr = new StreamReader(ns);
    string result = sr.ReadToEnd();
    tcpClient.Close();
    tcpListener.Stop();

}

/// <summary>
/// Socket套接字侦听接收TcpClient发送来的数据
/// </summary>
public static void Listen2()
{
    Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    listener.Bind(new IPEndPoint(IPAddress.Any, 2112));
    listener.Listen(0);
    Socket socket = listener.Accept();
    Stream netStream = new NetworkStream(socket);
    StreamReader read = new StreamReader(netStream);

    string result = read.ReadToEnd();
    socket.Close();
    listener.Close();

}

/// <summary>
/// TcpClient 发送数据
/// </summary>
public static void Send()
{
    TcpClient tcpClient = new TcpClient("HostAddress",2112);
    NetworkStream ns = tcpClient.GetStream();
    FileStream fs = File.Open("Program.cs", FileMode.Open);
    int data = fs.ReadByte();
    while (data!=-1)
    {
        ns.WriteByte((byte)data);
        data = fs.ReadByte();
    }
    fs.Close();
    ns.Close();
    tcpClient.Close();
}