编程需要恒心和毅力,最主要的是要有信心,循序渐进的完成任务。 一、socket类用于网络通信 命名空间System.Net.Sockets,完整的类引用System.Net.Sockets.Socket。Socket类支持各种网络协议。 二、简单的控制台程序 using System;using System.Collections.Generic;us
编程需要恒心和毅力,最主要的是要有信心,循序渐进的完成任务。
一、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,运行上面的两个程序。发现运行良好。七、可以在此基础上做进一步的修正,以完成其他功能。
				 沃梦达教程
				
			本文标题为:C#之socket编程怎么做?
				
        
 
            
        
             猜你喜欢
        
	     - user32.dll 函数说明小结 2022-12-26
 - 在C# 8中如何使用默认接口方法详解 2023-03-29
 - C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
 - 如何使用C# 捕获进程输出 2023-03-10
 - Unity3D实现渐变颜色效果 2023-01-16
 - Unity Shader实现模糊效果 2023-04-27
 - Oracle中for循环的使用方法 2023-07-04
 - .NET CORE DI 依赖注入 2023-09-27
 - WPF使用DrawingContext实现绘制刻度条 2023-07-04
 - c# 模拟线性回归的示例 2023-03-14
 
						
						
						
						
						
				
				
				
				