3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Powershell > Link > UDPでの文字列送受信 > 送信コードを試した

Last updated at Posted at 2016-01-23

PowershellからESP8266へコマンドを送信するにはどういうコードが必要か。

以下のリンクを見つけた。
http://stackoverflow.com/questions/12148666/send-and-receive-data-via-udp-in-powershell

上記に書いているリンクが
http://pshscripts.blogspot.co.uk/2008/12/send-udpdatagramps1.html
こちらに送信に関する具体例が書かれている。

試した

動作環境

実際に試してみた

動作環境
- 送信側 > Windows 8.1 pro (64bit) : (192.168.10.11)
- 受信側 > CentOS 6.5 (192.168.10.10)

CentOS側で以下として準備

$ tcpdump -n udp portrange 7000

code

以下のコードで文字列"From powershell<LF>"10セットをまとめて送信。
動いた。

udpSend.ps1
# Define port and target IP address   
# Random here!   
$Port = 7000
$IP = "192.168.10.10"   
$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 = "From powershell`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
3
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?