Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send and Receive data via UDP in PowerShell

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:

  1. Send string "ABCDEFG" to server 10.10.10.1 on port UDP 5000
  2. Receive response from server 10.10.10.1

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.

like image 326
CRCerr0r Avatar asked Aug 27 '12 19:08

CRCerr0r


People also ask

Can you use Send with UDP?

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.

Is used for sending and receiving UDP packets in socket programming?

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.

Does test NetConnection test UDP?

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.

How do I connect to UDP?

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.


1 Answers

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 
like image 123
Thomas Lee Avatar answered Sep 23 '22 12:09

Thomas Lee