Some knowledge about socket thread communication
In general we need to make two machines communicate, need to create a server class, a client class, but also need to create a thread class
Server
public class Server {
public static void Main (string[] args) throws IOException {
ServerSocket ss = new ServerSocket (8888);
int num=0;
SYSTEM.OUT.PRINTLN ("The server is about to start, wait for the client to start ... ");
while (true) {
Socket s = ss.accept ();
Serverthread t = new Serverthread (s);
Start thread
T.start ();
num++;
InetAddress ad= inetaddress.getlocalhost ();
SYSTEM.OUT.PRINTLN (AD + "current number of visitors to the website:" +num);
}
}
Then create the client
public class Client {
public static void Main (string[] args) throws Unknownhostexception, IOException {
Socket s=new socket ("localhost", 8888);
Get byte output stream
S.geto
OutputStream str= S.getoutputstream ();
PrintWriter pw=new printwriter (str);
Pw.write ("user:admin:1112, password:12364");
Pw.flush ();
S.shutdownoutput ();
InputStream st= S.getinputstream ();
InputStreamReader read=new InputStreamReader (ST);
BufferedReader buf=new BufferedReader (read);
String S1=buf.readline ();
System.out.println ("I am the client, the server returns the corresponding data:" +S1);
Buf.close ();
Read.close ();
St.close ();
Pw.close ();
Str.close ();
}
Finally create a thread class
public class Serverthread extends thread{
Public Serverthread (Socket sockrt) {
Super ();
THIS.SOCKRT = SOCKRT;
}
Socket Sockrt;
public void Run ()
{
Server s=new server ();
InputStream str = NULL;
try {
str = Sockrt.getinputstream ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
InputStreamReader read = null;
try {
Read = new InputStreamReader (str, "GBK");
} catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
BufferedReader buf=new BufferedReader (read);
String lin = null;
try {
Lin = Buf.readline ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
while (Lin!=null)
{
System.out.println ("I am the server: Client said:" +lin);
try {
Lin=buf.readline ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
try {
Sockrt.shutdowninput ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
OutputStream stream = null;
try {
stream = Sockrt.getoutputstream ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
PrintWriter pw=new PrintWriter (stream);
Pw.write ("welcome You");
Pw.flush ();
Pw.close ();
try {
Stream.Close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
Read.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
Str.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
Sockrt.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
This enables multi-user access to the server
Java Socket thread Communication