J
Jan Liße
Hi,
I just want to send from my client a udp-packet
that only contains a string to a server.
Everything works fine but when i receive the packet at my server
and convert the received byte[] to a string, the string is corrupted:
it contains the correct text but additionally there are strange characters
appended looking like squares. It seems that the conversion to string
is not abel to get the correct end of the string...
First i thought it might be an issue with encoding settings and i tried
beside
the default encoding on both sides UTF-8 and ISO-8859-1 but it didnt work
either.
When i use tcp instead of udp it the same problem...
Maybe i could do some string filtering but i want to grasp the reason
for this strange behaviour!
Any help appreciated,
Jan
My reduced source:
Client-side:
serverAddr = InetAddress.getByName("Server-IP");
udpSocket = new DatagramSocket();
String testString = "Test";
byte[] sendBuf = testString.getBytes();
DatagramPacket packet = new DatagramPacket(sendBuf, sendBuf.length,
serverAddr, udpPort);
udpSocket.send(packet);
udpSocket.close();
Server-side:
DatagramSocket udpSocket = new DatagramSocket(udpPort);
DatagramPacket p = new DatagramPacket(rcvBuffer,rcvBuffer.length);
udpSocket.receive(p);
rcvBuffer = p.getData();
dataStr = new String(rcvBuffer);
I just want to send from my client a udp-packet
that only contains a string to a server.
Everything works fine but when i receive the packet at my server
and convert the received byte[] to a string, the string is corrupted:
it contains the correct text but additionally there are strange characters
appended looking like squares. It seems that the conversion to string
is not abel to get the correct end of the string...
First i thought it might be an issue with encoding settings and i tried
beside
the default encoding on both sides UTF-8 and ISO-8859-1 but it didnt work
either.
When i use tcp instead of udp it the same problem...
Maybe i could do some string filtering but i want to grasp the reason
for this strange behaviour!
Any help appreciated,
Jan
My reduced source:
Client-side:
serverAddr = InetAddress.getByName("Server-IP");
udpSocket = new DatagramSocket();
String testString = "Test";
byte[] sendBuf = testString.getBytes();
DatagramPacket packet = new DatagramPacket(sendBuf, sendBuf.length,
serverAddr, udpPort);
udpSocket.send(packet);
udpSocket.close();
Server-side:
DatagramSocket udpSocket = new DatagramSocket(udpPort);
DatagramPacket p = new DatagramPacket(rcvBuffer,rcvBuffer.length);
udpSocket.receive(p);
rcvBuffer = p.getData();
dataStr = new String(rcvBuffer);