TcpClient和UdpClient

///

///TcpClient

///

namespace Tcp_Listener
{
    class Program
    {
        static void Main(string[] args)
        {
            //TcpListener 对 socket 进行了一层封装
            TcpListener listener = new TcpListener(IPAddress.Parse("10.246.40.205"), 9999);
            //开始监听
            listener.Start();
            //等待客户端来连接
            TcpClient client = listener.AcceptTcpClient();
            //取得客户端发来的数据
            NetworkStream stream = client.GetStream();
            //定义接收数组
            byte[] data = new byte[1024];
            while (true)
            {
                //data为接收容器 0代表从0索引开始存放数据 1024表示读取的最大字节
                int length = stream.Read(data, 0, 1024);
                //转为string
                string message = Encoding.UTF8.GetString(data, 0, length);
                Console.WriteLine("收到TCP消息:" + message);


            }
            //关闭
            //stream.Close();
            //client.Close();
            //listener.Stop();
            //Console.ReadKey();


        }
    }
}


namespace Tcp_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建TcpClient对象就会跟server去建立链接
            TcpClient client = new TcpClient("10.246.40.205", 9999);
            //通过网络流传递数据
            NetworkStream stream = client.GetStream();


            while (true)
            {
                string message = Console.ReadLine();
                byte[] data = Encoding.UTF8.GetBytes(message);
                //写入数据 ,data数组,0起始位,传入数组长度
                stream.Write(data, 0, data.Length);
            }


            //关闭
            //stream.Close();
            //client.Close();
        }
    }
}




///

///UdpClient

///

namespace Udp_Clinet_服务端
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建UDPClient绑定ip
            UdpClient udpclient = new UdpClient(new IPEndPoint(IPAddress.Parse("10.246.40.205"), 9988));


            while (true)
            {


                //接收数据
                IPEndPoint point = new IPEndPoint(IPAddress.Any, 0);
                //返回地址 和数据
                byte[] data = udpclient.Receive(ref point);
                //
                string message = Encoding.UTF8.GetString(data);
                Console.WriteLine("收到UDP消息:" + message);


            }


            //关闭
            //udpclient.Close();
        }
    }
}


namespace Udp_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建UDPClient对象
            UdpClient udpClient = new UdpClient();


            while (true)
            {
                //发送数据
                string message = Console.ReadLine();
                byte[] data = Encoding.UTF8.GetBytes(message);
                //send 参数 data数据 length长度 IPEndPoint地址
                udpClient.Send(data, data.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("10.246.40.205"), 9988));
            }


        }
    }
}

猜你喜欢

转载自blog.csdn.net/cxihu/article/details/77538813
今日推荐