5

The following code sends a packet on port 15000:

int port = 15000;
UdpClient udp = new UdpClient();
//udp.EnableBroadcast = true;  //This was suggested in a now deleted answer
IPEndPoint groupEP = new IPEndPoint(IPAddress.Broadcast, port);
string str4 = "I want to receive this!";
byte[] sendBytes4 = Encoding.ASCII.GetBytes(str4);
udp.Send(sendBytes4, sendBytes4.Length, groupEP);
udp.Close();

However, it's kind of useless if I can't then receive it on another computer. All I need is to send a command to another computer on the LAN, and for it to receive it and do something.

Without using a Pcap library, is there any way I can accomplish this? The computer my program is communicating with is Windows XP 32-bit, and the sending computer is Windows 7 64-bit, if it makes a difference. I've looked into various net send commands, but I can't figure them out.

I also have access to the computer (the XP one)'s local IP, by being able to physically type 'ipconfig' on it.

EDIT: Here's the Receive function I'm using, copied from somewhere:

public void ReceiveBroadcast(int port)
{
    Debug.WriteLine("Trying to receive...");
    UdpClient client = null;
    try
    {
        client = new UdpClient(port);
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }

    IPEndPoint server = new IPEndPoint(IPAddress.Broadcast, port);

    byte[] packet = client.Receive(ref server);
    Debug.WriteLine(Encoding.ASCII.GetString(packet));
}

I'm calling ReceiveBroadcast(15000) but there's no output at all.

PGR
  • 109
  • 1
  • 3
  • 10
  • 2
    Are you familiar with the significance of `new IPEndPoint(IPAddress.Broadcast, port)`? – Wug Oct 12 '12 at 18:30
  • Honestly, not really. I'm trying to understand what is mostly copy-pasted, but this line eludes me. IPAddress.Broadcast is 255.255.255.255, and my packet is being sent as Wireshark shows. Sorry! – PGR Oct 12 '12 at 18:32
  • well, what do things that broadcast generally do? If you're near someone that broadcasts something, what happens? – Wug Oct 12 '12 at 18:34
  • Broadcasts send a message that is visible to everybody on the LAN, as far as I know. I can use an IPAddress.Parse() with the exact IP of the computer, if that would be more secure, fast, or something else. I'm just getting into packets in C#, and not finding much success. – PGR Oct 12 '12 at 18:37
  • if wireshark is verifying that you're sending the packet, then its definitely leaving. You should make sure it's getting to the destination... try running wireshark on another computer. – Wug Oct 12 '12 at 18:39
  • 1
    for connectionless communication, you need to create socket object and bind to your IPEndPoint, will give you an example – Turbot Oct 12 '12 at 19:30
  • Thanks, Turbot! And Wug, I just tried it when I got home and both computers are receiving the packet. – PGR Oct 12 '12 at 19:39

1 Answers1

21

Here is the simple version of Server and Client to send/receive UDP packets

Server

IPEndPoint ServerEndPoint= new IPEndPoint(IPAddress.Any,9050);
Socket WinSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
WinSocket.Bind(ServerEndPoint);

Console.Write("Waiting for client");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0)
EndPoint Remote = (EndPoint)(sender);
int recv = WinSocket.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

Client

IPEndPoint RemoteEndPoint= new IPEndPoint(
IPAddress.Parse("ServerHostName"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork,
                           SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, RemoteEndPoint);
Turbot
  • 5,095
  • 1
  • 22
  • 30
  • 1
    Thank you! So far, I'm trying to get the client to work. I put byte[] in front of 'data' to make a byte array (in Client), but I'm not sure what to put as the ServerHostName. I'll try my own local IP. Thanks! – PGR Oct 12 '12 at 21:04
  • 1
    ServerHostName will be your target host to send the UDP data. you can put `localhost` if you want to run locally. – Turbot Oct 13 '12 at 03:12