autoit版 TCP客戶端測試工具

一個腳本命令行工具

#NoTrayIcon
#Region ;**** 參數創建於 ACNWrapper_GUI ****
#PRE_icon=lemon.ico
#PRE_Change2CUI=y
#PRE_Res_requestedExecutionLevel=None
#EndRegion ;**** 參數創建於 ACNWrapper_GUI ****

#include <Array.au3>
#Include <String.au3>

Func get_cmd()
	Local $str

	While True
		$str &= ConsoleRead()
		If @error Then
			Return Null
		EndIf
		If (StringRight($str, 2) = @CRLF) Then
			$str = StringTrimRight($str, 2)
			ExitLoop
		EndIf
		Sleep(100)
	WEnd

	Return $str
EndFunc

Func printf($format, $var1, $var2 = -1, $var3 = -1)
    If $var2 = -1 Then
        ConsoleWrite(StringFormat($format, $var1))
    Else
        ConsoleWrite(StringFormat($format, $var1, $var2, $var3))
    EndIf
EndFunc

Func print_hex($buf)
	Local $len = UBound($buf, 1)
	Local $i, $j, $l = 0
	
	If $len = 0 Then Return

	ConsoleWrite("             0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F");    
    For $i = 0 To $len - 1
        If Mod($i, 16) = 0 Then
            printf("\n %08Xh: ", $i)
        EndIf
        printf("%02X ", $buf[$i])
        $l = $l + 1
        If (Mod($l, 16) == 0 Or ($len == $i+1)) Then
            ConsoleWrite("    ")
            If ($len == $i+1) Then
                For $j=0 To 15-$l
                    ConsoleWrite("   ")
                Next
            EndIf
            For $j=$i-$l+1 To $i
                If ($buf[$j] >= 0x20 And $buf[$j] <= 0x7E) Then
                    ;printf("%c", $buf[$j])
					ConsoleWrite(Chr($buf[$j]))
                Else
                    ConsoleWrite(".")
                EndIf
            Next
            $l = 0
        EndIf
    Next

	ConsoleWrite(@CRLF)
EndFunc

Local $cmd
Local $socket = -1

TCPStartup()

While True
	ConsoleWrite("--------------------------" & @CRLF)
	ConsoleWrite("  1 ip port : connect" & @CRLF)
	ConsoleWrite("  2 string  : send string" & @CRLF)
	ConsoleWrite("  3 hex     : send hex" & @CRLF)
	ConsoleWrite("  4 file    : send file" & @CRLF)
	ConsoleWrite("  5         : recv" & @CRLF)
	ConsoleWrite("  6         : close" & @CRLF)
	ConsoleWrite("  0         : exit" & @CRLF)
	ConsoleWrite("  ------------------------" & @CRLF)
	ConsoleWrite("  socket: " & $socket & @CRLF)

	ConsoleWrite(@CRLF & ">")
	$cmd = get_cmd()

	If $cmd = Null Or StringLeft($cmd, 1) = "0" Then
		ExitLoop
	ElseIf StringLeft($cmd, 2) = "1 "  And StringLen($cmd) > 3 Then
		; 連接服務器
		If ($socket >= 0) Then
			ConsoleWrite(" socket is connected already" & @CRLF)
			ContinueLoop
		EndIf
		$cmd = StringTrimLeft($cmd, 2)
		$args = StringSplit($cmd, " ")
		If $args[0] = 2 Then
			$socket = TCPConnect($args[1], $args[2])
			If $socket = -1 Then 
				ConsoleWrite(" connect error: " & @error & @CRLF)
			EndIf
		EndIf
	ElseIf StringLeft($cmd, 2) = "2 "  And StringLen($cmd) > 2 Then
		; 發送字符串
		If ($socket < 0) Then
			ConsoleWrite(" no socket is connected" & @CRLF)
			ContinueLoop
		EndIf
		$data = StringTrimLeft($cmd, 2)
		$len = TCPSend($socket, $data)
		If $len = 0 Then
			ConsoleWrite(" send error, socket closed: " & @error & @CRLF)
			TCPCloseSocket($socket)
			$socket = -1
		Else
			ConsoleWrite(" send " & $len & " bytes" & @CRLF)
			$aa = StringToASCIIArray($data, 0, StringLen($data), 1)
			print_hex($aa)
		EndIf
	ElseIf StringLeft($cmd, 2) = "3 "  And StringLen($cmd) > 2 Then
		; 發送二進制數據
		If ($socket < 0) Then
			ConsoleWrite(" no socket is connected" & @CRLF)
			ContinueLoop
		EndIf
		$data = StringTrimLeft($cmd, 2)
		$data = StringStripWS($data, 8)
		If Mod(StringLen($data), 2) <> 0 Then
			ConsoleWrite(" data format error" & @CRLF)
			ContinueLoop
		EndIf
		$data = _HexToString($data)
		$len = TCPSend($socket, $data)
		If $len = 0 Then
			ConsoleWrite(" send error, socket closed: " & @error & @CRLF)
			TCPCloseSocket($socket)
			$socket = -1
		Else
			ConsoleWrite(" send " & $len & " bytes" & @CRLF)
			$aa = StringToASCIIArray($data, 0, StringLen($data), 1)
			print_hex($aa)
		EndIf
	ElseIf StringLeft($cmd, 2) = "4 "  And StringLen($cmd) > 2 Then
		; 發送文件
		If ($socket < 0) Then
			ConsoleWrite(" no socket is connected" & @CRLF)
			ContinueLoop
		EndIf
		$file_name = StringTrimLeft($cmd, 2)
		$file = FileOpen($file_name, 16)
		If $file = -1 Then
			ConsoleWrite(" file open error" & @CRLF)
			ContinueLoop
		EndIf
		
		$data_send = FileRead($file)
		FileClose($file)
		$len = TCPSend($socket, $data_send)
		ConsoleWrite($len & " " & @error & @CRLF)
		If $len = 0 Then
			ConsoleWrite(" send error, socket closed: " & @error & @CRLF)
			TCPCloseSocket($socket)
			$socket = -1
		Else
			ConsoleWrite(" send " & $len & " bytes" & @CRLF)
			$aa = BinaryToString($data_send)
			$aa = StringToASCIIArray($aa, 0, StringLen($aa), 1)
			print_hex($aa)
		EndIf
	ElseIf StringLeft($cmd, 1) = "5" Then
		; 接收數據
		If ($socket < 0) Then
			ConsoleWrite(" no socket is connected" & @CRLF)
			ContinueLoop
		EndIf
		$recv = TCPRecv($socket, 8192)
		If @error <> 0 And @error <> -1 Then
			ConsoleWrite(" recv error, socket closed: " & @error & @CRLF)
			TCPCloseSocket($socket)
			$socket = -1
		ElseIf StringLen($recv) > 0 Then
			$aa = BinaryToString($recv)
			$aa = StringToASCIIArray($aa, 0, StringLen($recv), 1)
			ConsoleWrite(" recv " & UBound($aa) & " bytes" & @CRLF)
			print_hex($aa)
		EndIf
	ElseIf StringLeft($cmd, 1) = "6" Then
		; 關閉連接
		If ($socket < 0) Then
			ConsoleWrite(" no socket is connected" & @CRLF)
			ContinueLoop
		EndIf
		TCPCloseSocket($socket)
		$socket = -1
	EndIf
WEnd

If $socket <> -1 Then
	TCPCloseSocket($socket)
EndIf
TCPShutdown()

--------------------------
  1 ip port : connect
  2 string  : send string
  3 hex     : send hex
  4 file    : send file
  5         : recv
  6         : close
  0         : exit
  ------------------------
  socket: -1

>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章