[英]Python Sockets bytes decoding from Java client
從我的客戶端,我向服務器發送了一個 integer 的變量大小,當我在服務器上收到它時,它的數據格式與我需要的不同,我該如何解碼?
發送時的客戶端代碼
Socket s = new Socket();
s.connect(new InetSocketAddress(voids[1], 7800), 2000);
DataOutputStream writer = new DataOutputStream(s.getOutputStream());
data.remove("ObjectStream");
writer.write(9); // used 9 for testing
writer.flush();
服務器端接收
amt = client.recv(self.SIZE) # size is 1024
amt = amt.decode() # where it should have decode and get 9
integer 個值在服務器端接收到值:
已解決(歸功於@MarkRotteveel):
客戶端發送
Socket s = new Socket();
s.connect(new InetSocketAddress(voids[1], 7800), 2000);
DataOutputStream writer = new DataOutputStream(s.getOutputStream());
data.remove("ObjectStream");
writer.writeInt(9); // used 9 for testing
writer.flush();
套接字接收和解碼
amt = client.recv(self.SIZE)
amt = int.from_bytes(amt, byteorder="big")
amt = amt.decode()
對於您要使用 sockets 發送的任何內容,都必須轉換為特定格式,因此請使用 function writeInt() -
DataOutputStream writer =
(DataOutputStream)data.get("ObjectStream");
data.remove("ObjectStream");
writer.writeInt(9); // used 9 for testing
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.