UDP Client Problem

G

Guest

Can anyone tell my what is wrong with this snippet of code?

//Creates a UdpClient for reading incoming data.
UdpClient receivingUdpClient = new UdpClient();

//Creates an IPEndPoint to record the IP Address and port number of the sender.
// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.101"), 11000);

try
{

// Blocks until a message returns on this socket from a remote host.
ERROR at this line ---------->>> Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

string returnData = Encoding.ASCII.GetString(receiveBytes);

Console.WriteLine("This is the message you received " +

The ERROR I receive is:

System.Net.Sockets.SocketException: An invalid argument was supplied
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP)


TIA,
Cory Nemelka
 
A

AirPete

Does this work?

UdpClient receivingUdpClient = new UdpClient();
receivingUdpClient.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.101"),
11000));
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
string returnData = System.Text.Encoding.ASCII.GetString(receiveBytes);
receivingUdpClient.Close();


- Pete

Cory Nemelka said:
Can anyone tell my what is wrong with this snippet of code?

//Creates a UdpClient for reading incoming data.
UdpClient receivingUdpClient = new UdpClient();

//Creates an IPEndPoint to record the IP Address and port number of the sender.
// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new
IPEndPoint(IPAddress.Parse("192.168.1.101"), 11000);
try
{

// Blocks until a message returns on this socket from a remote host.
ERROR at this line ---------->>> Byte[] receiveBytes =
receivingUdpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);

Console.WriteLine("This is the message you received " +

The ERROR I receive is:

System.Net.Sockets.SocketException: An invalid argument was supplied
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
 
G

Guest

No, same problem as before

----- AirPete wrote: ----

Does this work

UdpClient receivingUdpClient = new UdpClient()
receivingUdpClient.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.101")
11000))
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0)
Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint)
string returnData = System.Text.Encoding.ASCII.GetString(receiveBytes)
receivingUdpClient.Close()


- Pet

Cory Nemelka said:
Can anyone tell my what is wrong with this snippet of code
UdpClient receivingUdpClient = new UdpClient() sender
// The IPEndPoint will allow you to read datagrams sent from any source
IPEndPoint RemoteIpEndPoint = ne
IPEndPoint(IPAddress.Parse("192.168.1.101"), 11000)
// Blocks until a message returns on this socket from a remote host
ERROR at this line ---------->>> Byte[] receiveBytes
receivingUdpClient.Receive(ref RemoteIpEndPoint)
string returnData = Encoding.ASCII.GetString(receiveBytes)
Console.WriteLine("This is the message you received "
The ERROR I receive is
System.Net.Sockets.SocketException: An invalid argument was supplie
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP
 
A

AirPete

Sorry, then I don't know what the problem is.
It works great on my machine.

- Pete


Cory Nemelka said:
No, same problem as before.

----- AirPete wrote: -----

Does this work?

UdpClient receivingUdpClient = new UdpClient();
receivingUdpClient.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.101"),
11000));
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
string returnData = System.Text.Encoding.ASCII.GetString(receiveBytes);
receivingUdpClient.Close();


- Pete

Cory Nemelka said:
Can anyone tell my what is wrong with this snippet of code?
UdpClient receivingUdpClient = new UdpClient();
of the
sender.
// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new
IPEndPoint(IPAddress.Parse("192.168.1.101"), 11000);
try {
// Blocks until a message returns on this socket from a remote
host.
ERROR at this line ---------->>> Byte[] receiveBytes =
receivingUdpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("This is the message you received " +
The ERROR I receive is:
System.Net.Sockets.SocketException: An invalid argument was
supplied
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32
offset,
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP)
Cory Nemelka
 
S

Sherif ElMetainy

Hello

The problem is that you specify the UdbClient is not bound to a local end
point. You didn't specify which port and IP address (on the local computer)
the client should be listening on. Assuming that 192.168.0.101 is the ip
address of the local computer that you want to use for receiving message,
and port 11000 is the port you want to use.

UdpClient receivingUdpClient = new UdpClient(new
IPEndPoint(IPAddress.Parse("192.168.1.101"), 11000));
IPEndPoint RemoteIpEndPoint = null;
try
{
byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("This is the message you received " +


This should work,

Best regards
Sherif

Cory Nemelka said:
Can anyone tell my what is wrong with this snippet of code?

//Creates a UdpClient for reading incoming data.
UdpClient receivingUdpClient = new UdpClient();

//Creates an IPEndPoint to record the IP Address and port number of the sender.
// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new
IPEndPoint(IPAddress.Parse("192.168.1.101"), 11000);
try
{

// Blocks until a message returns on this socket from a remote host.
ERROR at this line ---------->>> Byte[] receiveBytes =
receivingUdpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);

Console.WriteLine("This is the message you received " +

The ERROR I receive is:

System.Net.Sockets.SocketException: An invalid argument was supplied
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
 

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