2

My goal is to send and receive simple byte arrays to/from server application (simple Java tool, returning fixed response in 30 bytes to all requests).

I have completed the first part (sending byte array via TCP) with the following code:

 # Define port and target IP address 
  [int] $Port = 2222
  $IP = "172.16.0.51"
  $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]::Stream 
  $Ptype = [System.Net.Sockets.ProtocolType]::TCP
  $Sock = New-Object System.Net.Sockets.Socket $saddrf, $stype, $ptype 
  $Sock.TTL = 26 

  # Connect to socket 
  $sock.Connect($end)

  # Create byte array
  # TCP [Byte[]] $Message = 0xAA,0x55,0x00,0x12,0x00,0x00,0x00,0x7B,0x00,0x00,0x00,0x41,0x00,0x00,0x00,0x00,0xD6,0xDE,0xD5,0xA7,0x14,0x00
  [Byte[]] $Message = 0xAA,0x55,0x00,0x12,0x00,0x00,0x00,0x7B,0x00,0x00,0x00,0x41,0x00,0x00,0x00,0x00,0xD6,0xDE,0xD5,0xA7,0x14,0x00

 # for ($i=0; $i -le 1000; $i++)
 # {
  # Send the byte array 
  $Sent = $Sock.Send($Message)
  "{0} characters sent to: {1} " -f $Sent,$IP
  "Message is: $Message" 
  # End of Script
 # }

But I cannot find/add/create code able to receive responses just after sending such requests. Could anyone please assist me with that?

Thank you.

0

2 Answers 2

0

Check out the documentation for the System.Net.Sockets.Socket class on MSDN. That class has various Receive methods you can use to read data from the stream.

0
0

Thank you, finally I have the following code:

# Getting responses  
    $script:compare = new-object System.Byte[] 40  
    $end = 0  
    do  
    {  
            # Receive response  
        Start-Sleep -m 30  
        $buffer = new-object System.Byte[] 40  
        $Received = $Sock.Receive($buffer)  
        if (@(Compare-Object $buffer $compare).length -ne 0)  
        {     
            Write-Host "TCP Message received:" $buffer  
            # Encode received response to ASCII  
            $Encode = new-object "System.Text.ASCIIEncoding"  
            $test = $Encode.GetString($buffer)  
            Add-Content -value "$(Time) $test" -Path tcp_response.log  
            $end = 1  
        }  
    } while ($end -eq 0)'
2
  • Inadequate. You are ignoring the return value returned by the receive method, which is either an EOS indication, an error indication, or the length of the data received. Instead you are assuming the read has filled the entire buffer. You cannot assume that with TCP.
    – user207421
    Jun 9, 2012 at 21:33
  • EJP, thank you for good remark. I cannot post related code in this comment so will create a separate answer for that.
    – TShark
    Jun 11, 2012 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.