Json 字符串在 Socket PHP 和 Java 上损坏



这是我的php套接字服务器客户端发送代码。


namespace Sock;
class SocketClient {
private $connection;
private $address;
private $port;
public function __construct( $connection ) {
$address = ''; 
$port = '';
socket_getsockname($connection, $address, $port);
$this->address = $address;
$this->port = $port;
$this->connection = $connection;
}

public function send( $message ) {
print_r("Message Sent " . $message . "n");
socket_write($this->connection, $message . "n", strlen($message . "n"));
}

public function read($len = 1024) {
if ( ( $buf = @socket_read( $this->connection, $len, PHP_BINARY_READ  ) ) === false ) {
return null;
}

return $buf;
}
public function getAddress() {
return $this->address;
}

public function getPort() {
return $this->port;
}

public function close() {
socket_shutdown( $this->connection );
socket_close( $this->connection );
}
}

这是我的Java客户端代码

package me.proiezrush.virtualizorapi.socket;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class ClientConnection extends Thread {
private final Socket socket;
private final DataOutputStream dos;
private final InputStream inputStream;
private final DataInputStream dis;
public ClientConnection(Socket socket) throws Exception {
this.socket = socket;
dos = new DataOutputStream(socket.getOutputStream());
inputStream = socket.getInputStream();
dis = new DataInputStream(inputStream);
System.out.println("CONNECTED");
}
public void send(String msg) {
try {
dos.writeUTF(msg);
}
catch (IOException e) {
}
}
@Override
public void run() {
while (true) {
try {
String s = receive();
if (!s.equals("")) {

System.out.println(s);
if (s.startsWith("[VPSID] ")) {
//String a = s.replace("[VPSID] ", "");

}
}
}
catch (Exception e) {
}
}
}
private String receive() throws Exception {
int buflen = inputStream.available();
byte[] rawstring = new byte[buflen];
dis.read(rawstring, 0, buflen);
String str = new String(rawstring, StandardCharsets.UTF_8);
return str;
}
}

这是php服务器的输出

{"151":{"vpsid":"151","vps_name":"v1034","uuid":"btdryfgrv5nsmchw","serid":0,"time":"1622952141","edittime":"1623074735","virt":"kvm","uid":"2","plid":"2","hostname":"xxxx","osid":"909","os_name":"ubuntu-20.04-x86_64","iso":"","sec_iso":"","boot":"cda","space":"10","inodes":"0","ram":"2048","burst":"0","swap":"512","cpu":"100","cores":"1","cpupin":"-1","cpu_percent":"0.00","bandwidth":"2000","network_speed":"0","upload_speed":"-1","io":"0","ubc":"","acpi":"1","apic":"1","pae":"0","shadow":"0","vnc":"1","vncport":"5901","vnc_passwd":"","hvm":"0","suspended":"0","suspend_reason":"","nw_suspended":"","rescue":"0","band_suspend":"0","tuntap":"0","ppp":"0","ploop":"0","dns_nameserver":"a:0:{}","osreinstall_limit":"0","preferences":"","nic_type":"virtio","vif_type":"","virtio":"1","pv_on_hvm":"0","disks":"","kvm_cache":"0","io_mode":"0","cpu_mode":"","total_iops_sec":"0","read_bytes_sec":"0","write_bytes_sec":"0","kvm_vga":"0","acceleration":"0","vnc_keymap":"en-us","routing":"0","mg":"","used_bandwidth":"16.50","cached_disk":"a:2:{s:4:"disk";a:6:{s:10:"Filesystem";s:19:"/dev/mapper/loop0p1";s:9:"1K-blocks";s:7:"9740372";s:4:"Used";s:7:"2052964";s:9:"Available";s:7:"7173136";s:4:"Use%";s:3:"23%";s:10:"mounted_on";s:13:"/home/v1034/0";}s:5:"inode";a:6:{s:10:"Filesystem";s:19:"/dev/mapper/loop0p1";s:6:"Inodes";s:6:"615296";s:5:"IUsed";s:5:"79902";s:5:"IFree";s:6:"535394";s:5:"IUse%";s:3:"13%";s:10:"mounted_on";s:13:"/home/v1034/0";}}","webuzo":"0","disable_ebtables":"0","install_xentools":"0","admin_managed":"0","rdp":"0","topology_sockets":"0","topology_cores":"0","topology_threads":"0","mac":"00:16:3e:2a:1b:56","notes":"","disable_nw_config":"0","locked":"","openvz_features":"","speed_cap":"","numa":"0","bpid":"0","bserid":"0","timezone":"","ha":"0","data":null,"server_name":"localhost","email":"xxxxx@gmail.com","pid":"0","type":"0","os_distro":"ubuntu","stid":[35],"ips":{"155":"XXX.XXX.XXX.XX"}}}

但这就是我在Java输出中得到的

{"151":{"vpsid":"151","vps_name":"v1034","uuid":"btdryfgrv5nsmchw","serid":0,"time":"1622952141","edittime":"1623074735","virt":"kvm","uid":"2","plid":"2","hostname":"quboxhost.com","osid":"909","os_name":"ubuntu-20.04-x86_64","iso":"","sec_iso":"","boot":"cda","space":"10","inodes":"0","ram":"2048","burst":"0","swap":"512","cpu":"100","cores":"1","cpupin":"-1","cpu_percent":"0.00","bandwidth":"2000","network_speed":"0","upload_speed":"-1","io":"0","ubc":"","acpi":"1","apic":"1","pae":"0","shadow":"0","vnc":"1","vncport":"5901","vnc_passwd":"","hvm":"0","suspended":"0","suspend_reason":"","nw_suspended":"","rescue":"0","band_suspend":"0","tuntap":"0","ppp":"0","ploop":"0","dns_nameserver":"a:0:{}","osreinstall_limit":"0","preferences":"","nic_type":"virtio","vif_type":"","virtio":"1","pv_on_hvm":"0","disks":"","kvm_cache":"0","io_mode":"0","cpu_mode":"","total_iops_sec":"0","read_bytes_sec":"0","write_bytes_sec":"0","kvm_vga":"0","acceleration":"0","vnc_keymap":"en-us","routing":"0","mg":"","used_bandwidth":"16.50","cached_disk":"a:2:{s:4:"disk";a:6:{s:10:"Filesystem";s:19:"/dev/mapper/loop0p1";s:9:"1K-blocks";s:7:"9740372";s:4:"Used";s:7:"2052964";s:9:"Available";s:7:"7173136";s:4:"Use%";s:3:"23%";s:10:"mounted_on";s:13:"/home/v1034/0";}s:5:"inode";a:6:{s:10:"Filesystem";s:19:"/dev/mapper/loop0p1";s:6:"Inodes";s:6:"615296";s:5:"IUsed";s:5:"7
9902";s:5:"IFree";s:6:"535394";s:5:"IUse%";s:3:"13%";s:10:"mounted_on";s:13:"/home/v1034/0";}}","webuzo":"0","disable_ebtables":"0","install_xentools":"0","admin_managed":"0","rdp":"0","topology_sockets":"0","topology_cores":"0","topology_threads":"0","mac":"00:16:3e:2a:1b:56","notes":"","disable_nw_config":"0","locked":"","openvz_features":"","speed_cap":"","numa":"0","bpid":"0","bserid":"0","timezone":"","ha":"0","data":null,"server_name":"localhost","email":"edumaucherni@gmail.com","pid":"0","type":"0","os_distro":"ubuntu","stid":[35],"ips":{"155":"192.158.234.110"}}}

它被一分为二,所以JSON解析器给了我一个错误。

问题是:

  • Java没有接收到正确的字节长度。如何从PHP发送字节长度并用Java接收

在调用receive()方法之前,我刚刚添加了Thread.sleep(100);。这有助于程序获得所有可用的字节,所以现在我可以正确地获得字符串了。

最新更新