Android 发送数据包 int8 到 TCP 服务器
Android sending packetets int8 to tcp server
如何将数据包从 android 发送到等待 int8 变量的 SFML tcp 服务器。
服务器端:
sf:Int8 liczbunia;
listener.listen(55555);
listener.accept(socket);
cout << "connected";
sf::sleep(sf::milliseconds(500));
socket.receive( paket );
paket >> liczba;
我在客户端试过这段代码:
Socket socketClient = new Socket("192.168.0.100",55555);
PrintWriter(socketClient.getOutputStream(),true);
DataOutputStream dout = new DataOutputStream(socketClient.getOutputStream());
dout.writeInt(9);
dout.flush();
虽然客户端连接到服务器,但服务器没有收到数据包。
从 android 发送数据包的最佳方式是什么?
Packets solve the "message boundaries" problem, which means that when you send a packet on a TCP socket, you receive the exact same packet on the other end, it cannot contain less bytes, or bytes from the next packet that you send. However, it has a slight drawback: To preserve message boundaries, sf::Packet has to send some extra bytes along with your data, which implies that you can only receive them with a sf::Packet if you want them to be properly decoded. Simply put, you can't send an SFML packet to a non-SFML packet recipient, it has to use an SFML packet for receiving too. Note that this applies to TCP only, UDP is fine since the protocol itself preserves message boundaries.
解决方案:从套接字中读取原始数据。
如何将数据包从 android 发送到等待 int8 变量的 SFML tcp 服务器。
服务器端:
sf:Int8 liczbunia;
listener.listen(55555);
listener.accept(socket);
cout << "connected";
sf::sleep(sf::milliseconds(500));
socket.receive( paket );
paket >> liczba;
我在客户端试过这段代码:
Socket socketClient = new Socket("192.168.0.100",55555);
PrintWriter(socketClient.getOutputStream(),true);
DataOutputStream dout = new DataOutputStream(socketClient.getOutputStream());
dout.writeInt(9);
dout.flush();
虽然客户端连接到服务器,但服务器没有收到数据包。 从 android 发送数据包的最佳方式是什么?
Packets solve the "message boundaries" problem, which means that when you send a packet on a TCP socket, you receive the exact same packet on the other end, it cannot contain less bytes, or bytes from the next packet that you send. However, it has a slight drawback: To preserve message boundaries, sf::Packet has to send some extra bytes along with your data, which implies that you can only receive them with a sf::Packet if you want them to be properly decoded. Simply put, you can't send an SFML packet to a non-SFML packet recipient, it has to use an SFML packet for receiving too. Note that this applies to TCP only, UDP is fine since the protocol itself preserves message boundaries.
解决方案:从套接字中读取原始数据。