Click here to Skip to main content
14,635,966 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

How to send a message to this receiving function :
Socket sok = new Socket(AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Raw, ProtocolType.IP);
//Bind the socket to the selected IP address
sok.Bind(new IPEndPoint(IPAddress.Parse("192.168.1.21"), 6223));
//Set the socket  options
sok.SetSocketOption(SocketOptionLevel.IP,            //Applies only to IP packets
SocketOptionName.HeaderIncluded, //Set the include the header
                 true);          //option to true
byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
byte[] byOut = new byte[4] { 1, 0, 0, 0 }; //Capture outgoing packets

//Socket.IOControl is analogous to the WSAIoctl method of Winsock 2
sok.IOControl(IOControlCode.ReceiveAll, byTrue, byOut); //Equivalent to SIO_RCVALL constant of Winsock 2
while (true)
{
    byte[] buffer = new byte[4096];
    int read = sok.Receive(buffer);
    if (read > 42)
    {
      if ((long)BitConverter.ToUInt32(buffer, 12) == (long)BitConverter.ToUInt32(buffer, 16))
      {
         Console.WriteLine(Encoding.ASCII.GetString(buffer, 40, read));
      }
     }
}
Posted
Comments
Sandeep Mewara 12-May-11 1:39am
   
Elaborate!

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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();
        }
    }
}


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

Hope this helps,
Fredrik Bornander
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100