C#如何发送原始数据包? [英] c# how to send raw packets ?

查看:144
本文介绍了C#如何发送原始数据包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何向此接收功能发送消息:

Hi,

How to send a message to this receiving function :

<pre lang="cs"><br />
Socket sok = new Socket(AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Raw, ProtocolType.IP);<br />
//Bind the socket to the selected IP address<br />
sok.Bind(new IPEndPoint(IPAddress.Parse("192.168.1.21"), 6223));<br />
//Set the socket  options<br />
sok.SetSocketOption(SocketOptionLevel.IP,            //Applies only to IP packets<br />
SocketOptionName.HeaderIncluded, //Set the include the header<br />
                 true);          //option to true<br />
byte[] byTrue = new byte[4] { 1, 0, 0, 0 };<br />
byte[] byOut = new byte[4] { 1, 0, 0, 0 }; //Capture outgoing packets<br />
<br />
//Socket.IOControl is analogous to the WSAIoctl method of Winsock 2<br />
sok.IOControl(IOControlCode.ReceiveAll, byTrue, byOut); //Equivalent to SIO_RCVALL constant of Winsock 2<br />
while (true)<br />
{<br />
    byte[] buffer = new byte[4096];<br />
    int read = sok.Receive(buffer);<br />
    if (read > 42)<br />
    {<br />
      if ((long)BitConverter.ToUInt32(buffer, 12) == (long)BitConverter.ToUInt32(buffer, 16))<br />
      {<br />
         Console.WriteLine(Encoding.ASCII.GetString(buffer, 40, read));<br />
      }<br />
     }<br />
}</pre><br />

推荐答案

广播某些内容,应将该代码拾取;
工作示例:

Broadcast something and it should be picked up by that code;
Working example:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SocketSample
{
    class Program
    {
        private static void Server()
        {
            try
            {
                Socket sok = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Raw, ProtocolType.IP);
                sok.Bind(new IPEndPoint(IPAddress.Parse("10.201.170.106"), 6223));
                sok.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
                byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
                byte[] byOut =  new byte[4] { 1, 0, 0, 0 }; 
                sok.IOControl(IOControlCode.ReceiveAll, byTrue, byOut); 
                int c = 0;
                while (++c < 40) // Only run for a little while
                {
                    byte[] buffer = new byte[4096];
                    int read = sok.Receive(buffer);
                    
                    for (int i = 0; i < buffer.Length; ++i)
                        Console.Write((char)buffer[i]);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private static void Client()
        {
            try
            {
                byte[] data = Encoding.ASCII.GetBytes("The quick red fox jumps over the lazy brown dog");
                Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                sendSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 8);
                sendSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                EndPoint sendEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.21"), 62508);
                sendSocket.SendTo(data, data.Length, SocketFlags.None, sendEndPoint);
                sendSocket.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static void Main(string[] args)
        {
            Thread server = new Thread(Server);
            server.Start();
            Thread.Sleep(250); // Wait a while before sending;
            Thread client = new Thread(Client);
            client.Start();
            Console.ReadKey();
        }
    }
}



由于打开套接字的方式,您可能必须以管理员身份运行它.

希望这会有所帮助,
弗雷德里克·博纳德(Fredrik Bornander)



You probably will have to run it as administrator due to the way the socket is opened.

Hope this helps,
Fredrik Bornander


这篇关于C#如何发送原始数据包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆