473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

UdpClient problems

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
Nov 16 '05 #1
2 11864
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" wrote:
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

Nov 16 '05 #2
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" <Da*******@discussions.microsoft.com> wrote in message
news:D6**********************************@microsof t.com...
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" wrote:
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

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephan Steiner | last post by:
Hi The project I'm currently working on involves sending large UDP broadcasts. As the .NET framework already provides an easy facility for sending and receiving UDP packets I thought it was a...
4
by: Morten Overgaard | last post by:
Hi I'm listening on the SysLog port (514) through UDP. The problem is that I am not receiving anything nut I know that i get messages on the port. When I use KIWI to listen on the same port via...
2
by: Richy Rich | last post by:
Hi, I've developed an application in C# which spawns a thread to receive datagrams. If I use the socket receive method, there are no problems when using the application under NT or XP. I...
1
by: Dan Kelley | last post by:
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...
2
by: Emilio | last post by:
Question about UdpClient sample ' This constructor arbitrarily assigns the local port number. Dim udpClient As New UdpClient() Try udpClient.Connect("www.contoso.com", 11000) ' Sends a...
3
by: D. André Dhondt | last post by:
In VB.NET 2003, is there a way to create a System.Net.Sockets.UDPClient to listen to any address AND any port? I can get it to listen to any address, but only if I specify a port (for example,...
0
by: tccode97 | last post by:
To whom it may concern, I am developing a socket application in VC++ that uses asynchronous connnection. After doing search on google, I found the following link ...
1
by: tccode97 | last post by:
Hi, I need an urgent help. I am developing a socket application in VC++ that uses asynchronous connnection. After doing search on google, I found the following link ...
0
by: matt | last post by:
Hi I built a class to poll an app on a udp port using the sync methods which worked fine. I've since changed to using beginreceive and now the poll works but I don't receive the response from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.