C# TcpListener TcpClient

1 篇文章 0 订阅
1 篇文章 0 订阅

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

测试结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值