ASP.NET TCP 简单的客户端与服务器

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:23   1631   0

TCP协议适合稳定性,要求较高的情形

其实就是两个控制台要引入:

using System.Net;
using System.Net.Sockets;
using System.IO;

服务器:

 class Program
    {
        static void Main(string[] args)
        {
            int port = 888;//端口号
            TcpClient tcpClient;
            IPAddress [] ipList= Dns.GetHostAddresses("127.0.0.1");
            IPAddress localAddress = ipList[0];//ip地址
            TcpListener tcpListener = new TcpListener(localAddress, port);
            tcpListener.Start();
            Console.WriteLine("服务器启动成功,等待用户接入....");
            while (true)
            {
                try
                {
                    tcpClient = tcpListener.AcceptTcpClient();//每接受一个挂起请求生成一个TcpClient
                    NetworkStream networkStream = tcpClient.GetStream();
                    BinaryReader reader = new BinaryReader(networkStream);
                    BinaryWriter writer = new BinaryWriter(networkStream);
                    while (true)
                    {
                        try
                        {
                            string strReader = reader.ReadString();//接收消息
                            string[] strReaders = strReader.Split(new char[] {' '}); //按空格拆分
                            Console.WriteLine("有客户端接入.客户Id:"+ strReaders[0]);
                            Console.WriteLine("消息:" + strReaders[1]);
                            string retMsg = "我是服务器,欢迎";
                            writer.Write(retMsg);
                        }
                        catch (Exception)
                        {

                            break;
                        }

                    }
                }
                catch (Exception)
                {

                    break;
                }
            }
                 


        }

客户端:

 class Program
    {
        static void Main(string[] args)
        {
            TcpClient tcpClient = new TcpClient();//创建一个对象,自动分配主机IP和端口号
            tcpClient.Connect("127.0.0.1", 888);
            if (tcpClient!= null)
            {
                Console.WriteLine("连接服务器成功");
                NetworkStream networkStream = tcpClient.GetStream();
                BinaryReader reader = new BinaryReader(networkStream);
                BinaryWriter writer = new BinaryWriter(networkStream);
                string local = "127.0.0.1";//存储本地IP,默认值为127.0.0.1
                IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
                foreach (var item in ips)
                {
                    if (!item.IsIPv6LinkLocal && !item.IsIPv6Teredo)
                    {
                        local = item.ToString();
                    }
                }
                writer.Write(local + " 你好服务器,我是客户端");//向服务器发消息
                    while (true)
                    {
                        try
                        {
                            string strReader = reader.ReadString();
                            if (strReader!=null)
                            {
                                Console.WriteLine("来自服务器的消息:" + strReader);
                            }
                        }
                        catch (Exception)
                        {

                            break;
                        }
                    }
               
            } 

        }
    }

注意:内容以空格截断为字符串,所以在IP地址和内容之间留空格

运行效果:

打开服务器:


打开客户端:


切换到服务器:




分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP