C#的TCP/UDP通信编程

C#提供了方便强大的各种网络编程类,直接上代码了。

A,TCP通信服务器端

class Program
{
    public static int port = 1987;
    public static string host = "127.0.0.1";

    static void Main(string[] args)
    {
        try
        {
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Bind(ipe);
            s.Listen(5);
            Console.WriteLine("start server");

            Socket t = s.Accept();
            byte[] data = new byte[1024];
            int len;

            len = t.Receive(data);
            string str = Encoding.ASCII.GetString(data, 0, len);
            Console.WriteLine("Get: " + str);

            str = "data received";
            data = Encoding.ASCII.GetBytes(str);
            t.Send(data);
            t.Close();
            Console.WriteLine("stop server");
        }
        catch (Exception err) {
            Console.WriteLine(err.ToString());
        }
    }
}

TCP通信客户端

class Program
{
    static string host = "127.0.0.1";
    static int port = 1987;

    static void Main(string[] args)
    {
        try
        {
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);

            TcpClient tc = new TcpClient();
            tc.Connect(ipe);

            if (tc.Connected)
            {
                string str = "this is udp connection";

                byte[] data = Encoding.ASCII.GetBytes(str);

                NetworkStream stream = tc.GetStream();
                stream.Write(data, 0, data.Length);

                int len = stream.Read(data, 0, data.Length);
                str = Encoding.ASCII.GetString(data, 0, data.Length);

                Console.WriteLine("Get " + str);

                tc.Close();
            }
        }
        catch (Exception err) {
            Console.WriteLine(err.ToString());
        }
    }
}

B,UDP通信服务器端

class Program
{
    public static int port = 1987;

    static void Main(string[] args)
    {
        try
        {
            IPEndPoint ipe = new IPEndPoint(IPAddress.Any, port);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            s.Bind(ipe);

            Console.WriteLine("start server");

            byte[] data = new byte[1024];

            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint remote = (EndPoint)sender;
            int len = s.ReceiveFrom(data, ref remote);

            string str = Encoding.ASCII.GetString(data, 0, len);
            Console.WriteLine("Get: " + str);

            str = "data received";
            data = Encoding.ASCII.GetBytes(str);
            s.SendTo(data, remote);
            s.Close();
            Console.WriteLine("stop server");
        }
        catch (Exception err) {
            Console.WriteLine(err.ToString());
        }
    }
}

UDP通信客户端

class Program
{
    static string host = "127.0.0.1";
    static int port = 1987;

    static void Main(string[] args)
    {
        try
        {
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            IPEndPoint ipe_rec = new IPEndPoint(IPAddress.Any, 10080);

            UdpClient uc = new UdpClient(10080);
            uc.Connect(ipe);

            string str = "this is udp connection";

            byte[] data = Encoding.ASCII.GetBytes(str);

            uc.Send(data, data.Length);
            data = uc.Receive(ref ipe);
            str = Encoding.ASCII.GetString(data, 0, data.Length);

            Console.WriteLine("Get " + str);

        }
        catch (Exception err) {
            Console.WriteLine(err.ToString());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值