前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >学习记录——C#实现简单网络通信

学习记录——C#实现简单网络通信

原创
作者头像
MrLi001
修改2022-02-23 16:00:04
5740
修改2022-02-23 16:00:04
举报
文章被收录于专栏:xixixixi

方法1:Socket(套接字)编程(Tcp)

TCPServer代码

代码语言:c#
复制
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace TcpServer
{
    class Program
    {
        static void Main(string[] args)
        {
        //建立连接
            Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//使用IPV4插口,流类型进行数据传输,使用TCP协议

            IPAddress ipAddress = new IPAddress(new byte[] { 192, 168, 42, 104 });//ip地址

            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 7788);//端口号

            tcpServer.Bind(ipEndPoint);

            tcpServer.Listen(100);
        //检测客户端的连接
            Console.WriteLine("服务器已启动");
            Socket client = tcpServer.Accept();
            Console.WriteLine("一个客户端连接过来了");

        //接收到消息并输出
            byte []data =new byte[1024];
            int length = client.Receive(data);
            string message = Encoding.UTF8.GetString(data, 0, length);
            Console.WriteLine("接收到客户端的消息:"+message);
        //关闭连接  
            client.Close();
            tcpServer.Close();
        }
    }
}

TCPClient代码

代码语言:c#
复制
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace TcpClient
{
    class Program
    {
        static void Main(string[] args)
        {
        //建立连接
            Socket tcpClient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

            IPAddress ipAddress = new IPAddress(new byte[] { 192, 168, 42, 104 });//ip地址

            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 7788);//端口号

            tcpClient.Connect(ipEndPoint);
            Console.WriteLine("连接到服务器...");
            //tcpClient.Receive(new byte[]{22});
        //发送消息
            string message = "客户端发出了一条消息";
            tcpClient.Send(Encoding.UTF8.GetBytes(message));
        //关闭连接
            tcpClient.Close();
        }
    }
}

方法2:Socket(套接字)编程(Udp)

UdpServer代码

代码语言:c#
复制
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace UDPServer
{
    class Program
    {
        static void Main(string[] args)
        {
        //建立连接
            Socket Udpserver = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);//连接,socket类型,协议类型

            IPAddress ipAddress = new IPAddress(new byte[] { 192, 168, 42, 104 });//ip地址

            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 7788);//端口号

            Udpserver.Bind(ipEndPoint);
            
            Console.WriteLine("UDP服务器端已开启...");
         //接收数据
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
            EndPoint ep = (EndPoint) ipep;
            byte[] data = new byte[1024];
            int length = Udpserver.ReceiveFrom(data,ref ep);
            Console.WriteLine("接收到的数据:"+ Encoding.UTF8.GetString(data, 0, length));
        //关闭连接
            Udpserver.Close();
        }
    }
}

UdpClient代码

代码语言:c#
复制
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace UdpClient
{
    class Program
    {
        static void Main(string[] args)
        {
        //建立连接
            Socket UdpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//连接,socket类型,协议类型
            IPAddress ipAddress = new IPAddress(new byte[] { 192, 168, 42, 104 });//ip地址
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 7788);//端口号
        //发送数据
            byte[] data = Encoding.UTF8.GetBytes("你好,UDP客户端上线了");
            UdpClient.SendTo(data, ipEndPoint);
        //关闭连接
            UdpClient.Close();
        }
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档