Java Socket thread Communication

Source: Internet
Author: User

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

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.