Python's UDP Socket & Struct Pack equivalent code needed in C#

uvals
uvals used Ask the Experts™
on
Need equivalent C# code for the below Python code
#!/usr/bin/python
import socket
import struct
import sys
import time
 
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
 
s.sendto(struct.pack("8B",255,16,176,0,0,0,0,0),("192.168.1.3",8080))

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I don't think there is an equivalent to the struct.pack method, but the socket-based stuff should be reasonably straightforward:
using System.Net.Sockets;
using System.Net;
using System.Text;
 
public static class Program
{
    public static int Main(string[] args)
    {
        IPAddress ip = IPAddress.Parse("192.168.1.3");
        IPEndPoint ep = new IPEndPoint(ip, 8080);
 
        byte[] message = Encoding.ASCII.GetBytes("this is the text to send");
 
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Tcp);
        s.Connect(ep);
        s.Send(message);
        s.Disconnect(true);
    }
}

Open in new window

Looking into it a bit further, are you just trying to send 8 bytes of data?  If so, replace the Encoding.ASCII.GetBytes call with your own byte[].
byte[] message = new byte[] { 255, 16, 176, 0, 0, 0, 0, 0 };

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial