C# TCP《五》TCPClient 与TCPListener 同时收发

 

//-----------这是服务端 TcpListener------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;

namespace ConsoleApplication15
{
    class Program
    {
       public static byte[] data = new byte[1024];
        public static void printReceiveMsg(object Obj_tcpClient)
        {
            TcpClient tcpClient = (TcpClient)Obj_tcpClient;
            NetworkStream stream = tcpClient.GetStream();

            while (true)
            {
                if (stream.DataAvailable)
                {
                    int length = stream.Read(data, 0, data.Length);
                    string receiveMsg = Encoding.UTF8.GetString(data, 0, length);
                    Console.WriteLine("\n接收客户端发的数据: " + receiveMsg);
                    Console.Write("请输入您要发送的数据: ");
                }
            }
        }
        static void Main(string[] args)
        {
           
            TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8888);
            listener.Start();
            Console.WriteLine("启动监听成功");

            TcpClient tcpClient = listener.AcceptTcpClient();
            NetworkStream stream = tcpClient.GetStream();
            
            Thread T = new Thread(new ParameterizedThreadStart(printReceiveMsg));
            T.Start(tcpClient);

            while (true)
            {
                Console.Write("请输入您要发送的数据: ");
                string msg = Console.ReadLine();
                data = Encoding.UTF8.GetBytes(msg);
                stream.Write(data, 0, data.Length);
            }
            
          
        }
    }
}

 

 

//--------- TcpClient 端 -----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ConsoleApplication16
{
    class Program
    {
        public static byte[] data = new byte[1024];
        public static void printReceiveMsg(object Obj_tcpClient)
        {
            TcpClient tcpClient = (TcpClient)Obj_tcpClient;
            NetworkStream stream = tcpClient.GetStream();

            while (true)
            {
                if (stream.DataAvailable)
                {
                    int length = stream.Read(data, 0, data.Length);
                    string receiveMsg = Encoding.UTF8.GetString(data, 0, length);
                    Console.WriteLine("\n接收服务端发来的数据: " + receiveMsg);
                    Console.Write("请输入您要发送的数据: ");
                }
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("这是 TcpClient 端. ");
            //声明一个客户端
            TcpClient tcpClient = new TcpClient("127.0.0.1",8888);
            //声明一个流用来写数据
            NetworkStream stream = tcpClient.GetStream();

            //通过线程循环接收
            Thread T = new Thread(new ParameterizedThreadStart(printReceiveMsg));
            T.Start(tcpClient);

            while (true)
            {
                Console.Write("请输入您要发送的数据: ");
                string msg = Console.ReadLine();

                byte[] data = Encoding.UTF8.GetBytes(msg);
                stream.Write(data, 0, data.Length);
            }
        }
    }
}

 

 

相关文章: