服务器与客户端异步---》的写法

本文介绍了一个简单的TCP客户端与服务器的实现案例,包括客户端连接、数据收发及服务器端的数据解析过程。通过C#代码展示了如何建立连接并进行数据交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

---------------------------------我们开始先写我们Tcp客户端的代码

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
namespace Tcp客户端
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            
            clientSocket.Connect(new IPEndPoint(IPAddress.Parse("10.80.11.60"),12345));
            byte[] data = new byte[1024];
            int count=clientSocket.Receive(data);
            string msg = Encoding.UTF8.GetString(data, 0, count);
            Console.Write(msg);
            //while (true)
            //{
            //    string s = Console.ReadLine();
            //    clientSocket.Send(Encoding.UTF8.GetBytes(s));
            //}


            for(int i = 0; i < 100; i++)
            {
                clientSocket.Send(Message.GetBytes(i.ToString()));
            }
            Console.ReadKey();
            clientSocket.Close();
            //先接收后发送
        }
    }

}

----------------------------------------------------------接下来我们服务器的代码----------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace TcpSokectSever
{
    class Message
    {
        private byte[] data = new byte[1024];
        private int startIndex = 0;//存取多少个数据
        public void AddCount(int count)
        {
            startIndex += count;
        }
        public byte[] Data
        {
            get
            {
                return data;
            }
        }
        public int StartIndex
        {
            get
            {
                return startIndex;
            }
        }
        public int RemainSize
        {
            get
            {
                return data.Length- startIndex;
            }
        }
        //解析数据 读取数据
        public void ReadMessage()
        {
            while (true)
            {
                if (startIndex <=4)
                {
                    return;
                }
                int count = BitConverter.ToInt32(data, 0);
                if ((startIndex - 4) >= count)
                {
                    Console.WriteLine(startIndex);
                    string s = Encoding.UTF8.GetString(data, 4, count);
                    Console.WriteLine("解析出来一条数据:" + s);
                    Array.Copy(data, count + 4, data, 0, startIndex - 4 - count);
                    startIndex -= (count+4);
                } else
                {
                    break;
                }
                
            }
            
        }
    }
}

------------------------------------------------------接下来我们写Message来接收消息--------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace TcpSokectSever
{
    class Message
    {
        private byte[] data = new byte[1024];
        private int startIndex = 0;//存取多少个数据
        public void AddCount(int count)
        {
            startIndex += count;
        }
        public byte[] Data
        {
            get
            {
                return data;
            }
        }
        public int StartIndex
        {
            get
            {
                return startIndex;
            }
        }
        public int RemainSize
        {
            get
            {
                return data.Length- startIndex;
            }
        }
        //解析数据 读取数据
        public void ReadMessage()
        {
            while (true)
            {
                if (startIndex <=4)
                {
                    return;
                }
                int count = BitConverter.ToInt32(data, 0);
                if ((startIndex - 4) >= count)
                {
                    Console.WriteLine(startIndex);
                    string s = Encoding.UTF8.GetString(data, 4, count);
                    Console.WriteLine("解析出来一条数据:" + s);
                    Array.Copy(data, count + 4, data, 0, startIndex - 4 - count);
                    startIndex -= (count+4);
                } else
                {
                    break;
                }
                
            }
            
        }
    }
}

----------------------------这样就可以解决-----------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值