udp query on localhost

edman747

New Member
Hello,
I have a couple of small powershell functions. One sends a ping and receives a response, the other sends a request for info and receives a response.

At first I had two separate functions to do the send/receive (create a udpclient do the send, close the udpclient. Then create another udpclient and do the receive).

But I found that sometimes when sending an info query to a server running on the localhost it would respond so quickly that the receive would miss the server info. I would only get some junk at the end of the response.

So now I create a udpclient do the send/receive then close udpclient. And this works just fine for a remoteip.

Most of the time on localhost the response comes so fast that the receive misses it. And just holds the port open forever.

Any idea how to get this to work consistently for a local or remote server? TIA

$localIP = '10.0.0.3'
$remoteIP = 'any remote server'
$script:ipAddr = $localIP
$script:port = 27025

function hlinfo
{
$endpoint = new-object System.Net.IPEndPoint ([IPAddress]$ipAddr,$port)
$udpclient = new-Object System.Net.Sockets.udpclient
# 25 byte array.
$a = New-Object byte[] 25

# 255255255255TSource Engine Query0, from developer.valvesoftware wiki
$a = 0xff,0xff,0xff,0xff,0x54,0x53,0x6f,0x75,0x72,0x63,0x65,0x20,0x45,0x6e,0x67,0x69,0x6e,0x65,0x20,0x51,0x75,0x65,0x72,0x79,0x00

# send query
$bytesSent = $udpclient.Send($a,$a.length,$endpoint)

# save the sent from port number.
$script:lepp = $udpclient.client.localendpoint.port

$c = [Text.Encoding]::ASCII.GetString($a)
echo "`"$c`" Sent to: $endpoint From Local UDP Port: $lepp"

# receive response, the server info
echo "Listening for response on: $ipaddr`:$lepp"

# store response in a byte array.
$content = $udpclient.Receive([ref]$endpoint)

# make it a string and find the map name.
$c = [Text.Encoding]::ASCII.GetString($content)
$d = $c.split([char][byte]"0x20")
$e = $d[3] | select-string "svencoop4" | % { $_ -replace "svencoop4","" }
$f = $e.split("`]") | select-string "Sven" | % { $_ -replace "Sven","" }
echo "map$f"
$f | set-content c:\scripts\lastmap.txt
$udpclient.Close()
}

function hlping
{
$endpoint = new-object System.Net.IPEndPoint ([IPAddress]$ipAddr,$port)
$udpclient = new-Object System.Net.Sockets.udpclient

# 5 byte array.
$a = New-Object byte[] 5

# ff ff ff ff 69
$a = 0xff,0xff,0xff,0xff,0x69

# send ping
$bytesSent = $udpclient.Send($a,$a.length,$endpoint)

# save the sent from port number.
$script:lepp = $udpclient.client.localendpoint.port

$c = [Text.Encoding]::ASCII.GetString($a)
echo "`"$c`" Sent to: $endpoint From Local UDP Port: $lepp"

# ping reply
echo "Listening for response on: $ipaddr`:$lepp"
$content = $udpclient.Receive([ref]$endpoint)
$c = [Text.Encoding]::ASCII.GetString($content)
$d = $c.split("?")
if ($d -eq "j"){ echo alive } else { echo "server down" }
$udpclient.Close()
}

# main
hlping
hlinfo
 

My Computer

System One

  • Manufacturer/Model
    hp/a6700f
    CPU
    amd phenom 9150e quad core 1.8gz
    Motherboard
    acpi x64
    Memory
    4gb
    Graphics Card(s)
    nvidia geforce 6150se nforce 430
    Sound Card
    realtek high def audio
    Screen Resolution
    1440x900
    Hard Drives
    st350062 oas scsi
    Keyboard
    logitec wireless
    Mouse
    dito
    Internet Speed
    nvidia nforce 10/100 mbps ethernet, wireless 802.11 b/g
solution: set local IP to 127.0.0.1

Works fine. Wonder why it did not work using my computers lan IP?
 

My Computer

System One

  • Manufacturer/Model
    hp/a6700f
    CPU
    amd phenom 9150e quad core 1.8gz
    Motherboard
    acpi x64
    Memory
    4gb
    Graphics Card(s)
    nvidia geforce 6150se nforce 430
    Sound Card
    realtek high def audio
    Screen Resolution
    1440x900
    Hard Drives
    st350062 oas scsi
    Keyboard
    logitec wireless
    Mouse
    dito
    Internet Speed
    nvidia nforce 10/100 mbps ethernet, wireless 802.11 b/g
Back
Top