UdpClient.Receive problem - equivalent using Socket class works!

G

Guest

I have 2 projects - 1 Winform project that sends Udp messages using the
UdpClient class when a button is clicked, and a Console application that
listens for these Udp messages.

If I try to use the UdpClient class to receive, it never works. The messages
are never received (no exceptions). If I use a normal socket configured to
listen for Udp messages, it works perfectly.

Below is my sample code. It is only test code - I know the receive logic
will need to be re-written. This is only a simple test. The commented code in
the listener doesn't work, the uncommented code works.

All code is running on my machine, and the ip address XX.XX.XX.XX is my
machines IP address.

Thanks
Dan

/******* LISTENER *******/

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
// using (UdpClient udpClient = new UdpClient("XX.XX.XX.XX", 10000))
// {
// IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
//
// byte[] receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
//
// string returnData = Encoding.Default.GetString(receiveBytes);
//
// Console.WriteLine(returnData);
// Console.ReadLine();
// }
using (Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp))
{
socket.Bind(new IPEndPoint(IPAddress.Parse("XX.XX.XX.XX"), 10000));

byte[] data = new byte[1024];

socket.Receive(data, 0, 1024, SocketFlags.None);

Console.WriteLine(Encoding.Default.GetString(data));
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
Console.ReadLine();
}
}
}
}

/******* SENDER *******/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Text;

namespace Udp_Sender
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button sendButton;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.sendButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// sendButton
//
this.sendButton.Location = new System.Drawing.Point(56, 16);
this.sendButton.Name = "sendButton";
this.sendButton.Size = new System.Drawing.Size(88, 23);
this.sendButton.TabIndex = 0;
this.sendButton.Text = "Send Message";
this.sendButton.Click += new System.EventHandler(this.sendButton_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(200, 53);
this.Controls.Add(this.sendButton);
this.Name = "Form1";
this.Text = "Udp Message Sender";
this.ResumeLayout(false);

}
#endregion

public static void Main(string[] args)
{
Application.Run(new Form1());
}

private void sendButton_Click(object sender, System.EventArgs e)
{
try
{
using (UdpClient udpClient = new UdpClient("30.254.204.79", 10000))
{
// Sends a message to the host to which you have connected.
byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");

udpClient.Send(sendBytes, sendBytes.Length);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
 
S

sadhu

Did you try using UdpClient's default constructor and passing your IP
address, port to the Receive method instead?

Regards
Senthil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top