UDP Communications

D

Dan

I'm attempting to write a program in C# (with Whidbey) that communicates
with Half-Life based servers via UDP to grab server info (hostname,
players connected, map, etc). I'm using the UdpClient class to do so as
of now, however once I send the query string
("\u00ff\u00ff\u00ff\u00ffinfostring\u0000") I seem to get no response
back (it's set to accept responses from any IP), thus freezing the
program. Any ideas? I think it may have to do with the port, does anyone
know how to convert a string-based IP to a long so I can use one of the
overloads of the IPEndPoint constructor to set any port? Thanks.
 
T

Tom Hall

I believe you are looking for the following:

IPEndPoint ep=new IPEndPoint(IPAddress.Parse("192.168.0.1"),2001);

Took me a while to figure that out too the first time.
Tom
 
D

Dan

Tom said:
I believe you are looking for the following:

IPEndPoint ep=new IPEndPoint(IPAddress.Parse("192.168.0.1"),2001);

Took me a while to figure that out too the first time.
Tom

Thanks, but it still doesn't work. Here's the culprit code, the app
locks up when it executes, I'm assuming because I'm getting no UDP
response. Also note that all vars used are declared at the top of the
parent class.

private void btnRefresh_Click(object sender, EventArgs e)
{
try
{
if (doneOnceSock == false)
{
byte[] sendBytes =
Encoding.ASCII.GetBytes("\u00ff\u00ff\u00ff\u00ffinfostring\u0000");
csConn.Send(sendBytes, sendBytes.Length,
txtIP.Text, 27015);
IPEndPoint AnyIP = new
IPEndPoint(IPAddress.Parse(txtIP.Text), 0);
byte[] receiveBytes = csConn.Receive(ref AnyIP);
string returnData =
Encoding.ASCII.GetString(receiveBytes);
csConn.Close();
doneOnceSock = true;
}
else
{
sendBytes =
Encoding.ASCII.GetBytes("\u00ff\u00ff\u00ff\u00ffinfostring\u0000");
csConn.Send(sendBytes, sendBytes.Length,
txtIP.Text, 27015);
AnyIP = new IPEndPoint(IPAddress.Parse(txtIP.Text), 0);
receiveBytes = csConn.Receive(ref AnyIP);
returnData = Encoding.ASCII.GetString(receiveBytes);
csConn.Close();
}
}
catch (Exception exc)
{
if (exc.Message == "An existing connection was forcibly
closed by the remote host")
{
MessageBox.Show("Invalid IP address or server busy.");
}
else
{
MessageBox.Show(exc.Message);
}
}
}
 
T

Tom Hall

Oh, I see the problem, to prevent the GUI from hanging you need to launch
this on a separate thread because (from the docs)

The Receive method will block until a datagram arrives from a remote host.
When data is available, the Receive method will read the first enqueued
datagram and return the data portion as a byte array. This method populates
the remoteEP parameter with the IPAddress and port number of the sender.
So, it blocks (hangs) until it receives a packet. Check out threading in
the docs. A good place to look for code is

www.codeproject.com

Also, using a packet sniffer such as Ethereal www.ethereal.com is a great
way to find out what's really going on - I use it all the time debugging
protocols.

HTH

Tom





Dan said:
Tom said:
I believe you are looking for the following:

IPEndPoint ep=new IPEndPoint(IPAddress.Parse("192.168.0.1"),2001);

Took me a while to figure that out too the first time.
Tom

Thanks, but it still doesn't work. Here's the culprit code, the app locks
up when it executes, I'm assuming because I'm getting no UDP response.
Also note that all vars used are declared at the top of the parent class.

private void btnRefresh_Click(object sender, EventArgs e)
{
try
{
if (doneOnceSock == false)
{
byte[] sendBytes =
Encoding.ASCII.GetBytes("\u00ff\u00ff\u00ff\u00ffinfostring\u0000");
csConn.Send(sendBytes, sendBytes.Length, txtIP.Text,
27015);
IPEndPoint AnyIP = new
IPEndPoint(IPAddress.Parse(txtIP.Text), 0);
byte[] receiveBytes = csConn.Receive(ref AnyIP);
string returnData =
Encoding.ASCII.GetString(receiveBytes);
csConn.Close();
doneOnceSock = true;
}
else
{
sendBytes =
Encoding.ASCII.GetBytes("\u00ff\u00ff\u00ff\u00ffinfostring\u0000");
csConn.Send(sendBytes, sendBytes.Length, txtIP.Text,
27015);
AnyIP = new IPEndPoint(IPAddress.Parse(txtIP.Text),
0);
receiveBytes = csConn.Receive(ref AnyIP);
returnData = Encoding.ASCII.GetString(receiveBytes);
csConn.Close();
}
}
catch (Exception exc)
{
if (exc.Message == "An existing connection was forcibly
closed by the remote host")
{
MessageBox.Show("Invalid IP address or server busy.");
}
else
{
MessageBox.Show(exc.Message);
}
}
}
 

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