• 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;
                }
            }
        }
    }

    测试结果如下:

  • 相关阅读:
    聪明的质检员 (二分)
    分巧克力(二分)
    产生冠军 HDU
    Legal or Not HDU
    确定比赛名次 HDU
    最短路径问题 HDU
    dijkstra算法为什么不能有负边?
    最短路 HDU
    dijkstra算法 模板
    Floyd算法模板--详解
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/12016842.html
Copyright © 2020-2023  润新知