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 {}}}