我有小型python tcp服务器和小型powershell tcp客户端。当我发送使用utf8编码的字符串时,其解码结果就像两个空格一样。我尝试了很多在网上找到的解决方案,但没有成功。我在哪里做错了?
Python服务器代码:
import socket
host = '192.168.0.11'
port = 12345
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))
sock.listen(1)
conn, address = sock.accept()
print('New client connected:', address)
while True:
data = conn.recv(1024)
print("Message:", data.decode('utf-8'),".")
if not data:
break
conn.close()
Powershell客户端脚本:
$addr = '192.168.0.11'
$port = '12345'
$conn = New-Object System.Net.Sockets.TcpClient($addr, $port)
$conn = $conn.GetStream()
$writer = New-Object System.IO.StreamWriter($conn)
$writer.AutoFlush = $true
$message = "hello"
if ($conn.Connected) {
$writer.WriteLine([system.text.encoding]::UTF8.GetBytes($message))
}
$writer.close()
$conn.close()