C#教程 socket编程

9 篇文章 0 订阅
2 篇文章 0 订阅

C#教程 socket编程

编程需要恒心和毅力,最主要的是要有信心,循序渐进的完成任务。

一、socket类用于网络通信
命名空间System.Net.Sockets,完整的类引用System.Net.Sockets.Socket。Socket类支持各种网络协议。
二、简单的控制台程序

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

namespace ConsoleApplicationSocket01
{
    class Program
    {
        static void Main(string[] args)
        {

        }
    }
}

三、添加socket变量

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace ConsoleApplicationSocket01
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket UnitySocketServer=
new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
            
        }
    }
}

然后bind服务器主机的 IP地址:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace ConsoleApplicationSocket01
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket UnitySocketServer=
new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
            IPAddress HostIpAddress = IPAddress.Parse("127.0.0.1");
            UnitySocketServer.Bind(new IPEndPoint(HostIpAddress,5600));

    }
}

四、让socket接收数据
UnitySocketServer.Receive();Receive()方法用于接收传入的数据,该方法有多个重载版本。
先定义接收数据的数组BytesOfReceived,然后调用Receive()接收数据

static void Main(string[] args)
        {
            Byte[] BytesOfReceived=new Byte[512];
            Socket UnitySocketServer=
new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
            IPAddress HostIpAddress = IPAddress.Parse("127.0.0.1");
            UnitySocketServer.Bind(new IPEndPoint(HostIpAddress,5600));
            UnitySocketServer.Receive(BytesOfReceived);
}

当然,上面的程序会存在很多问题,下面进行修正。先将Receive()放入无限循环结构,使socket不停的接收消息:

static void Main(string[] args)
        {
            Byte[] BytesOfReceived=new Byte[512];
            Socket UnitySocketServer=
new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
            IPAddress HostIpAddress = IPAddress.Parse("127.0.0.1");
            UnitySocketServer.Bind(new IPEndPoint(HostIpAddress,5600));
            while(true){
                UnitySocketServer.Receive(BytesOfReceived);
            }

接下来,继续修正,先判断每次循环是否接收到消息,如果接收到消息就显示它。Receive()方法会返回接收的数据长度,我们要利用这个值。

static void Main(string[] args)
        {
            Byte[] BytesOfReceived=new Byte[512];
            Socket UnitySocketServer=
new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
            IPAddress HostIpAddress = IPAddress.Parse("127.0.0.1");
            UnitySocketServer.Bind(new IPEndPoint(HostIpAddress,5600));
            while(true){
                int NumOfReceived=0;
                NumOfReceived=UnitySocketServer.Receive(BytesOfReceived);
                if(NumOfReceived>0){
                    Console.Write(Console.Write(Encoding.UTF8.GetString(BytesOfReceived)););
                }
            }

到此,该程序具有了基本功能,接下来可以测试它。
五、为了测试以上程序,我们再次创建控制台程序,这次创建的程序是客户端程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace ConsoleApplicationSocketClient01
{
    class Program
    {
        static void Main(string[] args)
        {
            Byte[] BytesOfSended =Encoding.UTF8.GetBytes("whtskjdkjfg");
            Socket UnitySocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPAddress HostIpAddress = IPAddress.Parse("127.0.0.1");
            UnitySocketClient.Bind(new IPEndPoint(HostIpAddress, 5601));
           while(true){
                int NumOfReceived = 0;
                NumOfReceived = UnitySocketClient.SendTo(BytesOfSended, new IPEndPoint(HostIpAddress, 5600));
                
           }

        }
    }
}

六、按Ctrl+F5,运行上面的两个程序。发现运行良好。
七、可以在此基础上做进一步的修正,以完成其他功能。

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#中进行Socket编程可以实现网络通信。Socket是一种用于在计算机之间进行通信的编程接口。C#提供了System.Net.Sockets命名空间来支持Socket编程。通过使用Socket类,可以创建和管理套接字,从而实现与其他计算机的数据交换。 C#中的Socket编程可以使用TCP或UDP协议进行数据传输。TCP协议提供可靠的、面向连接的通信,而UDP协议则提供无连接的通信。你可以根据具体的需求选择使用TCP还是UDP。 在C#中进行Socket编程的一般步骤包括以下几个方面: 1. 创建Socket对象:使用Socket类的构造函数创建一个Socket对象,可以指定使用的地址族、套接字类型和协议类型。 2. 绑定Socket到本地IP地址和端口:使用Socket类的Bind方法Socket绑定到本地的IP地址和端口。 3. 开始监听连接请求:使用Socket类的Listen方法开始监听传入的连接请求。 4. 接受连接请求:使用Socket类的Accept方法接受传入的连接请求,并创建一个新的Socket对象来处理该连接。 5. 通过Socket进行数据传输:使用Socket对象的Send和Receive方法发送和接收数据。 6. 关闭Socket连接:使用Socket对象的Close方法关闭Socket连接。 在C#中使用Socket编程可以实现各种类型的网络应用,例如客户端-服务器通信、文件传输、实时通信等。你可以根据具体的需求选择适当的Socket编程方式和数据传输方式。参考了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_40793198

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值