UdpClient problems

R

Rick

Hi,

I'm trying to get a simple UdpClient app working. I've been looking at the
MSDN info regarding UdpClient. When I set it up on my own PC and send
messages to myself it works OK. If I try to send messages to another PC with
another IP address I get multiple exception, the first one being "The
requested address is not valid in its context". I get this when I create my
UdpClient(iplistener) where iplistener is an IPEndPoint. It doesn't like the
IPEndPoint since it's another PC. I tried a different approach using a
Socket instead of a UdpClient and had the same problem with
socket.ReceiveFrom(endPoint).

I don't think there's a firewall issue since I was able to send TCP traffic
between the two PCs. If anyone has ideas I'd appreciate hearing them. Thank
you.

Rick
 
G

Guest

I too seemed to have problems with the code samples on MSDN. Try the following:

RECEIVER CODE (commented code uses Socket class acting as Udp listener):

try
{
using (UdpClient udpClient = new UdpClient(10000))
{
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 10000);
byte[] receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);

Console.WriteLine(returnData);
Console.ReadLine();
}

// using (Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp))
// {
// IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
// EndPoint senderRemote = (EndPoint)sender;
// socket.Bind(new IPEndPoint(IPAddress.Parse("XX.XX.XX.XX"), 10000));
//
// byte[] data = new byte[1024];
// socket.ReceiveFrom(data, 0, 1024, SocketFlags.None, ref senderRemote);
//
// Console.WriteLine(Encoding.Default.GetString(data));
// Console.ReadLine();
// }
}
catch (SocketException sockEx)
{
Console.WriteLine(sockEx.Message);
Console.WriteLine(sockEx.ErrorCode);
Console.WriteLine(sockEx.StackTrace);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
Console.ReadLine();
}

SENDER CODE:

try
{
using (UdpClient udpClient = new UdpClient(10000))
{
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("XX.XX.XX.XX"), 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, iep);
}
}
catch (SocketException sockEx)
{
MessageBox.Show(string.Format("Error code: {0}, message: {1}",
sockEx.Message, sockEx.ErrorCode));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

May need some modifying, but hopefully helps.
Dan
 
R

Rick

Dan,

Thanks for the response. I got the code working today by using the same
constructor you are using below. If I use the constructor passing a port it
works fine. If I use the default constructor or passing an IPEndPoint I
would get socket exceptions all over the place. The MSDN docs use these
constructors but they didn't work for me.

Rick

Dan Kelley said:
I too seemed to have problems with the code samples on MSDN. Try the
following:

RECEIVER CODE (commented code uses Socket class acting as Udp listener):

try
{
using (UdpClient udpClient = new UdpClient(10000))
{
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 10000);
byte[] receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);

Console.WriteLine(returnData);
Console.ReadLine();
}

// using (Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp))
// {
// IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
// EndPoint senderRemote = (EndPoint)sender;
// socket.Bind(new IPEndPoint(IPAddress.Parse("XX.XX.XX.XX"), 10000));
//
// byte[] data = new byte[1024];
// socket.ReceiveFrom(data, 0, 1024, SocketFlags.None, ref senderRemote);
//
// Console.WriteLine(Encoding.Default.GetString(data));
// Console.ReadLine();
// }
}
catch (SocketException sockEx)
{
Console.WriteLine(sockEx.Message);
Console.WriteLine(sockEx.ErrorCode);
Console.WriteLine(sockEx.StackTrace);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
Console.ReadLine();
}

SENDER CODE:

try
{
using (UdpClient udpClient = new UdpClient(10000))
{
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("XX.XX.XX.XX"), 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, iep);
}
}
catch (SocketException sockEx)
{
MessageBox.Show(string.Format("Error code: {0}, message: {1}",
sockEx.Message, sockEx.ErrorCode));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

May need some modifying, but hopefully helps.
Dan

Rick said:
Hi,

I'm trying to get a simple UdpClient app working. I've been looking at
the
MSDN info regarding UdpClient. When I set it up on my own PC and send
messages to myself it works OK. If I try to send messages to another PC
with
another IP address I get multiple exception, the first one being "The
requested address is not valid in its context". I get this when I create
my
UdpClient(iplistener) where iplistener is an IPEndPoint. It doesn't like
the
IPEndPoint since it's another PC. I tried a different approach using a
Socket instead of a UdpClient and had the same problem with
socket.ReceiveFrom(endPoint).

I don't think there's a firewall issue since I was able to send TCP
traffic
between the two PCs. If anyone has ideas I'd appreciate hearing them.
Thank
you.

Rick
 

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