UDPClient and my mental situaton.. Please Help

S

Serdar C.

im trying to write a really simple program that
communicates with udp packets between server and client
apps... but i cant receive (or send?) any data from any
of the applications.. what am i doin wrong? here are the
send and receive lines from my program:
Server:
ofcourse this void is in a system.thread and in a loop
---------------------------------------------------------
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any,2000);
try
{
while(true)
{
byte[] x = client.Receive(ref endpoint);
string s = Encoding.ASCII.GetString(x);
status.Text = s;
}
}
catch (Exception e)
{status.Text += e.ToString();}
----------------------------------------------------------
and the client is:
----------------------------------------------------------
UdpClient udpclient = new UdpClient();
IPEndPoint ipend2 = new
IPEndPoint(IPAddress.Parse("127.0.0.1"),2000);
byte[] sendbyte = Encoding.ASCII.GetBytes(textBox1.Text);
try
{
udpclient.Send(sendbyte,sendbyte.Length,ipend2);
}
catch(Exception exc)
{status.Text +=exc.ToString();}
 
R

Rich Blum

i forgot to write a line on the server app.:


If this is how the line is in your server app, then you did not
Bind() the local UDP socket to a specific port. It will not know what
UDP packets to accept. If you want your server to accept packets on
UDP port 2000, it should look something like this:

IPEndPoint iep = new IPEndPoint(IPAddress.Any, 2000);
UdpClient client = new UdpClient(iep);
byte[] data = new byte[1024];
IPEndPoint iep2 = new IPEndPoint(IPAddress.Any, 0);
data = client.Receive(ref iep2);
Console.WriteLine("The remote host is: {0}, port {1}",
iep2.Address, iep2.Port);
Console.WriteLine("The data is: {0}",
Encoding.ASCII.GetString(data));

When you call the Receive() method with an IPEndPoint object, it
is used to hold the address of the remote host sending the data, not
the local address.

Hope this helps solve your problem.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471433012.html
 

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