I am trying to write a script to test and application using PowerShell. The test should consist of sending a string to a remote server over UDP, then reading the response from that server and doing something with the result. The only help I need is with the middle two ('send string', then 'receive response') steps of the script:
I am relatively familiar with PowerShell, but this is my first time having to deal with sockets, so I am in unfamiliar waters, and I can't seem to make sense of the few examples I have found on posts.
UDP sockets can be connected or unconnected - in the first case send should be used and in the second sendto . This does not affect the protocol used, i.e. it still is unreliable UDP. It only affects where the destination is taken from: from the socket or given as argument.
The QUdpSocket class can be used to send and receive UDP datagrams. UDP is an unreliable, datagram-oriented protocol. Some application-level protocols use UDP because it is more lightweight than TCP.
Normally a TCP connection to a server is needed and PowerShell comes with an appropriate cmdlet Test-NetConnection . However, by default Test-NetConnection only supports TCP connections and also tests an ICMP ping each time.
In UDP, the client does not form a connection with the server like in TCP and instead, It just sends a datagram. Similarly, the server need not to accept a connection and just waits for datagrams to arrive. We can call a function called connect() in UDP but it does not result anything like it does in TCP.
Some time back, I wrote a simple PowerShell script to send a UDP Datagram. See: http://pshscripts.blogspot.co.uk/2008/12/send-udpdatagramps1.html which will get you half way there. I never did do the other half and write the server side of this though!
<# .SYNOPSIS Sends a UDP datagram to a port .DESCRIPTION This script used system.net.socckets to send a UDP datagram to a particular port. Being UDP, there's no way to determine if the UDP datagram actually was received. for this sample, a port was chosen (20000). .NOTES File Name : Send-UDPDatagram Author : Thomas Lee - [email protected] Requires : PowerShell V2 CTP3 .LINK http://www.pshscripts.blogspot.com .EXAMPLE #> ### # Start of Script ## # Define port and target IP address # Random here! [int] $Port = 20000 $IP = "10.10.1.100" $Address = [system.net.IPAddress]::Parse($IP) # Create IP Endpoint $End = New-Object System.Net.IPEndPoint $address, $port # Create Socket $Saddrf = [System.Net.Sockets.AddressFamily]::InterNetwork $Stype = [System.Net.Sockets.SocketType]::Dgram $Ptype = [System.Net.Sockets.ProtocolType]::UDP $Sock = New-Object System.Net.Sockets.Socket $saddrf, $stype, $ptype $Sock.TTL = 26 # Connect to socket $sock.Connect($end) # Create encoded buffer $Enc = [System.Text.Encoding]::ASCII $Message = "Jerry Garcia Rocks`n"*10 $Buffer = $Enc.GetBytes($Message) # Send the buffer $Sent = $Sock.Send($Buffer) "{0} characters sent to: {1} " -f $Sent,$IP "Message is:" $Message # End of Script
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With