First I describe the problem, I am doing UDP socket programming (a chat program), read Chinese from the console, and then print to the console, there is a garbled Chinese situation.
1, garbled the most fundamental reason is that the encoding and decoding inconsistent situation. Problem analysis, reading data from the console, the data source is the console, the output is garbled, the target source is also the console, but in the process of reading and output may appear garbled,
The simplest example is that the code on the console is encoded as GBK,GBK, which is two bytes, while Utf-8 is three bytes. So there will be garbled problems,
2. Solutions
(1) Open Eclipse or MyEclipse, click run-"Runconfigurations
Hint, change to UTF-8, but here to emphasize, some eclipse or myeclipse may be the form of GBK, different form change to different code, try more.
There is also a need to change this.
(2) Method two:
Open the Eclipse.ini file (in the installation directory of Eclipse) Add this sentence-dfile.encoding = utf-8, set the system Properties file.encoding to Utf-8.
2, attached I do a chat of the small program, the IP will be able to change to the person you want to chat the IP, both sides to run this code.
PackageCom.hzwealth.test.chat;ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress;/*** c&d Chat Room *@authorLixiaochao **/ Public classUdpchattest { Public Static voidMain (string[] args) {NewThread (NewUdpclientthread ()). Start (); NewThread (NewUdpserverthread ()). Start (); }}/*** Send-side *@authorLixiaochao **/classUdpserverthreadImplementsrunnable{@Override Public voidrun () {Try{datagramsocket ds=NewDatagramsocket (); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (system.in, "UTF-8")); String Line=NULL; while(line = Reader.readline ())! =NULL){
Note here that we need to write the IP address of the person you are talking to. InetAddress Address= Inetaddress.getbyname ("Your conversation person's IP address"); Datagrampacket DP=NewDatagrampacket (Line.getbytes (), line.getbytes (). Length, Address, 9999); Ds.send (DP); System.out.print ("I said," +line+ "\n\r"); if("~over". Equals (line)) {System.out.println ("Sender Exits"); Break; }} reader.close (); Ds.close (); } Catch(Exception e) {e.printstacktrace (); } } }/*** Receiving End *@authorLixiaochao **/classUdpclientthreadImplementsrunnable{@Override Public voidrun () {Try{datagramsocket ds=NewDatagramsocket (9999); System.out.println (System.getproperty ("File.encoding")); while(true){ byte[] buf =New byte[1024]; Datagrampacket DP=NewDatagrampacket (buf, buf.length); Ds.receive (DP); InetAddress Address=dp.getaddress (); String msg=NewString (buf, 0, Buf.length, "GBK"); SYSTEM.OUT.PRINTLN (Address+ "SAY:" +msg); if("~over". Equals (msg)) { Break; }} ds.close (); } Catch(Exception e) {e.printstacktrace (); } } }
If there is any problem, you need to correct me!
When your ambition can't hold up your dream, please calm down to learn!
When your economy can't afford your desires, please lower your head and work silently!
Myeclipse,eclipse control Console Output garbled problem