A solution to the garbled character of Chinese characters in Java socket communication

Source: Internet
Author: User
Tags getmessage readline


You can use:
in = new BufferedReader (New InputStreamReader (Socket.getinputstream (), "UTF-8"));
out = new BufferedWriter (New OutputStreamWriter (Socket.getoutputstream (), "UTF-8"));

Socket Example:

Import java.io.*;

Import java.net.*;
		
		public class SocketServerEx1 {public static void main (String args[]) {System.out.println ("Server");
		ServerSocket serversocket = null;
		Socket clientsocket = null;
		int connects = 0;		try {serversocket = new ServerSocket (82, 5);
				Port: 82, Maximum number of links: 5//Maximum connection 10 times while (connects <) {connects++;
				System.out.println ("--------------------Waiting for Connection--------------------------");	Clientsocket = Serversocket.accept ();
				Wait for connection System.out.println ("first" + Connects + "secondary connection");
			ServiceClient (Clientsocket);
		} serversocket.close ();
		catch (IOException IoE) {System.out.println ("Error:" + IoE);
		
		The public static void ServiceClient (Socket client) throws IOException {System.out.println ("linked");
		InputStreamReader INSR = null;
		OutputStreamWriter OUTSW = null;
			try {//Read data INSR = new InputStreamReader (Client.getinputstream (), "UTF-8");
			
			BufferedReader br = new BufferedReader (INSR); OUTSW = new OutputStreamWriter (Client.getoutputstream (), "UTF-8");
			
			BufferedWriter bw = new BufferedWriter (OUTSW);
			String str = "";
				while (str = Br.readline ())!= null) {str = Str.trim ();
				
				System.out.println ("Received client message:" + str);	Bw.write ("received message:" + str + "\ r \ n");
			Feedback the message to the client, plus a line break for the client to receive Bw.flush ();
			finally {//system.out.println ("Cleaning up connection:" + client);
			Insr.close ();
			Outsw.close ();
		Client.close ();
	} System.out.println ("Disconnected");
 }
}



Import java.io.*;

Import java.net.*;
		public class SocketClientEx1 {public static void main (string[] args) {System.out.println ("Client");
			try {Socket clientsocket = new Socket ("localhost", 82);

			System.out.println ("Client1:" + clientsocket);
			DataInputStream Datais = new DataInputStream (Clientsocket.getinputstream ());
			InputStreamReader INSR = new InputStreamReader (Datais, "UTF-8");
			
			BufferedReader br = new BufferedReader (INSR);
			DataOutputStream Dataos = new DataOutputStream (Clientsocket.getoutputstream ());
			OutputStreamWriter OUTSW = new OutputStreamWriter (Dataos, "UTF-8");

			BufferedWriter bw = new BufferedWriter (OUTSW);
			Input information byte bytes[] = new BYTE[100];
				while (true) {System.out.println ("----------------------------------");
				System.in.read (bytes);
				String str = new string (bytes);
				str = Str.trim ();
				if (str = = "Exit") {break;		///Send data Bw.write (str + "\ r \ n");
				
				
			Plus line breaks so that the server reads Bw.flush () by row;	Receive data while ((str = br.readline ())!= null) {str = Str.trim ();
					SYSTEM.OUT.PRINTLN ("Server reply:" + str);
				Break
			} insr.close ();
			Datais.close ();
			Dataos.close ();
		Clientsocket.close ();
		catch (Unknownhostexception Uhe) {System.out.println ("Error:" + uhe.getmessage ());
		catch (connectexception CE) {System.out.println ("Error:" + ce.getmessage ());
		catch (IOException IoE) {System.out.println ("Error:" + ioe.getmessage ());
 } finally {}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.