<> Zero , discover problems

* use Java Wrote a UDP Transceiver , Found problems with Chinese ! package socket; import java.io.IOException; import
java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.
InetAddress; import java.net.SocketException; import java.net.
UnknownHostException; public class send { /** * send out UDP data * @param args */ public
static void main(String[] args) { String str = "hello This is a JavaUDP example !";
DatagramSocket socket= null; try { socket = new DatagramSocket();// Create packet socket
DatagramPacket packet= new DatagramPacket(str.getBytes(), str.length(),
InetAddress.getByName("127.0.0.1"), 7217);// Building packages socket.send(packet);// Send packet
socket.close();// Close socket } catch (SocketException e) { // TODO Auto-generated
catch block e.printStackTrace(); }catch (UnknownHostException e) { // TODO
Auto-generated catch block e.printStackTrace(); } catch (IOException e) { //
TODO Auto-generated catch block e.printStackTrace(); }finally { if(socket!=null)
{ socket.close();// Close socket } } } } package socket; import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.
InetAddress; import java.net.SocketException; public class recive { static int
port= 7217;// Listening port /** * receive UDP data * @param args */ public static void main(String
[] args) { DatagramSocket socket = null; try { socket = new DatagramSocket(port)
;// Start listening to port DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
// Create cache socket.receive(packet);// receive data , It's blocked here byte[] dataArray = packet.getData();
// Get received data int portSend = packet.getPort();// Get port to send InetAddress address = packet
.getAddress();// Get sent address int length = packet.getLength();// Get the length of data System.out.
println( " address :"+address.getHostAddress() +"\n port :"+portSend +"\n data :\t" + " length :"+
dataArray.length+"\n" + "\t content :"+new String(dataArray,0,length));// show contents socket.
close();// Close socket } catch (SocketException e) { // TODO Auto-generated catch block
e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch
block e.printStackTrace(); }finally { if(socket!=null) { socket.close();// Close socket
} } } }
<> One , Display incomplete

resolvent : Send the
DatagramPacket packet = new DatagramPacket(str.getBytes(), str.length(),
InetAddress.getByName("127.0.0.1"), 7217);// Building packages
Change to
DatagramPacket packet = new DatagramPacket(str.getBytes(), str.length(),
InetAddress.getByName("127.0.0.1"), 7217);// Building packages
summary : Both parties shall use it uniformly
str.getBytes().length
Get length

<> Two , Display garbled code

resolvent : Use unified coding
Sender's
DatagramPacket packet = new DatagramPacket(str.getBytes(), str.getBytes().
length, InetAddress.getByName("127.0.0.1"), 7217);// Building packages
Change to
DatagramPacket packet = new DatagramPacket(str.getBytes(), str.getBytes("UTF-8"
).length, InetAddress.getByName("127.0.0.1"), 7217);// Building packages
Receiver's
System.out.println( " address :"+address.getHostAddress() +"\n port :"+portSend +"\n data :\t"
+ " length :"+dataArray.length+"\n" + "\t content :"+new String(dataArray,0,length));// show contents
Change to
System.out.println( " address :"+address.getHostAddress() +"\n port :"+portSend +"\n data :\t"
+ " length :"+dataArray.length+"\n" + "\t content :"+new String(dataArray,0,length,"UTF-8"));
// show contents
summary : Sender and receiver String Following Byte Encoding should be specified during array conversion

<> Three , Received a lot of diamonds

In solving the second problem , It's been a problem :

Because from the beginning Byte go to String Code specified when no length specified
new String(dataArray,"UTF-8");
Just specify the length
new String(dataArray,0,length,"UTF-8");
<> Four , summary

Basically, the scrambling is related to the coding or caused by the inconsistent processing between the sender and the receiver during the conversion . In the future development, we need to pay attention to !

Technology
©2019-2020 Toolsou All rights reserved,
" Cephalosporin wine Say go and go "? DANGER ! Don't drink alcohol when taking these drugs Programmer Tanabata Valentine's Day confession code China Lunar Rover “ Moon rabbit No.2 ” A strange rock was found on the moon use C++ I want to talk to you “ Prototype mode ” ( copy / copy constructor ) Software testing BUG describe Random forest R Language implementation TP6 Application examples of verifier and correct verification data Google says home office affects work efficiency !2021 Return to offline office in 2010 Message quality platform series | Full link troubleshooting Free download of documents : To introduce you to a few useful free download URL