C# TcpListener TcpClient

C# TcpListener TcpClient 使用,新建从控制台项目,引用System.Net

代码如下:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
//using System.IO.Pipes;
using System.Net;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
 
namespace LongtengSupremeConsole
{
    class Program
    {        
        static void Main(string[] args)
        {
             new Thread(new ThreadStart(TcpListenerMethod)).Start();
            Thread.Sleep(TimeSpan.FromSeconds(2));
            TcpClientSendDataMethod();
            Console.ReadKey();
        }
     public static void TcpClientSendDataMethod()
        {
            try
            {              
                int i = 0;
                while (true)
                {
                    TcpClient client = new TcpClient();
                    client.Connect("127.0.0.1", 49153);
                    using (NetworkStream n = client.GetStream())
                    {
                        BinaryWriter binaryWriter = new BinaryWriter(n);
                        binaryWriter.Write($"{i}");
                        binaryWriter.Flush();
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine($"客户端 发送数据是:{i}");
                        string  binaryReader = new BinaryReader(n).ReadString();
                        Console.ForegroundColor = ConsoleColor.Blue;
                        Console.WriteLine($"客户端 收到数据是:{binaryReader}");
                        //using (StreamWriter streamWriter = new StreamWriter(n))
                        //{
                        //    Console.ForegroundColor = ConsoleColor.Yellow;
                        //    Console.WriteLine($"客户端 发送数据是:{i}");
                        //    streamWriter.WriteLine("{i}");
                        //}
                        //using (StreamReader streamReader = new StreamReader(n))
                        //{
                        //    Console.ForegroundColor = ConsoleColor.Green;
                        //    Console.WriteLine($"客户端 收到数据是:{streamReader.ReadToEnd()}");
                        //}

                        i++;
                    }
                    Thread.Sleep(TimeSpan.FromSeconds(2));
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static TcpListener listener = null;

        public static void TcpListenerMethod()
        {
            try
            {
                //listener = new TcpListener(new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 49153));
                listener = new TcpListener(IPAddress.Any, 49153);
                //listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                listener.Start(20);
                while (true)
                {
                    using (TcpClient c = listener.AcceptTcpClient())
                    {
                        //c.LingerState.Enabled = false;
                        using (NetworkStream n = c.GetStream())
                        {
                            string binaryReader = new BinaryReader(n).ReadString();
                            Console.ForegroundColor = ConsoleColor.Blue;
                            Console.WriteLine($"服务端 收到数据是:{binaryReader}");

                            BinaryWriter binaryWriter = new BinaryWriter(n);
                            binaryWriter.Write($"服务端 回复 {binaryReader}");
                            binaryWriter.Flush();
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine($"服务端 回复 {binaryReader}");
                            
                            //using (StreamReader streamReader = new StreamReader(n))
                            //{
                            //    Console.ForegroundColor = ConsoleColor.Green;
                            //    Console.WriteLine($"服务端:收到数据:{streamReader.ReadToEnd()}");
                            //}
                            //using (StreamWriter streamWriter = new StreamWriter(n))
                            //{
                            //    Console.ForegroundColor = ConsoleColor.Yellow;
                            //    Console.WriteLine($"服务端:发送数据:我已经收到数据了");
                            //    streamWriter.WriteLine("我已经收到数据了。。。。");
                            //}
                        }
                    }
                }
            }
            catch (Exception)
            {
                listener.Stop();
                throw;
            }
        }

        public static void TcpListenerStopMethod()
        {
            try
            {
                listener.Stop();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

 

测试结果如下:

C# TcpListener TcpClient_第1张图片

 

你可能感兴趣的:(C# TcpListener TcpClient)