The first is to create a class that inherits the thread class
Package com.zzq.socket;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
Import Java.net.Socket;
public class Serverthread extends thread{
Socket socket = NULL;
/**
* Each thread shares the current socket
* @param socket
*/
Public serverthread (socket socket) {
This.socket = socket;
}
@Override
public void Run () {
Get input stream read client information
InputStream is = null;
InputStreamReader ISR = null;
BufferedReader br = null;
Gets the output stream sent to the client
OutputStream OS = null;
PrintWriter pw = null;
try {
is = Socket.getinputstream ();
ISR = new InputStreamReader (IS);
br = new BufferedReader (ISR);
String info = null;
while (info = Br.readline ())!=null) {
SYSTEM.OUT.PRINTLN ("Server client:" +info);
}
Socket.shutdowninput ();
OS = Socket.getoutputstream ();
Send a message to the client
PW = new PrintWriter (OS);
Pw.write ("200");
Pw.flush ();
} catch (IOException e) {
E.printstacktrace ();
}finally{
Close Resource
try {
if (os!=null)
Os.close ();
if (pw!=null)
Pw.close ();
if (br!=null)
Br.close ();
if (isr!=null)
Isr.close ();
if (is!=null)
Is.close ();
if (socket!=null)
Socket.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
Then write the service-side code
Package com.zzq.socket;
Import java.io.IOException;
Import java.net.InetAddress;
Import Java.net.ServerSocket;
Import Java.net.Socket;
public class Tcpserversocket {
public static void Main (string[] args) {
try {
Set up the server Socket monitor 8888 port
ServerSocket serversocket = new ServerSocket (8888);
Socket socket = NULL;
int count = 0;
Loop Get Client Connection
while (true) {
Socket = Serversocket.accept ();
Client connection uses Serverthread thread to handle socket read and input
Serverthread serverthread = new Serverthread (socket);
Serverthread.start ();
count++;
System.out.println ("Number of client connections-(" +count+ ")");
InetAddress address = socket.getinetaddress ();
SYSTEM.OUT.PRINTLN ("Client IP" +address.gethostaddress ());
}
} catch (IOException e) {
E.printstacktrace ();
}
}
}
Next down is the client
Package com.zzq.socket;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
Import Java.net.Socket;
Import java.net.UnknownHostException;
public class Client {
public static void Main (string[] args) {
try {
To create a client socket specify the server address and port
Socket socket = new socket ("localhost", 8888);
OutputStream OS = Socket.getoutputstream ();
PrintWriter pw = new PrintWriter (OS);
Pw.write ("Username:root password:123");
Pw.flush ();
Socket.shutdownoutput ();
InputStream is = Socket.getinputstream ();
BufferedReader br = new BufferedReader (new InputStreamReader (IS));
String info = null;
while (info = Br.readline ())!=null) {
SYSTEM.OUT.PRINTLN ("Server client:" +info);
}
Close Resource
Br.close ();
Is.close ();
Pw.close ();
Os.close ();
Socket.close ();
} catch (Unknownhostexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
This article is from the "The_monster" blog, make sure to keep this source http://zzqnobody.blog.51cto.com/7059797/1625841
Java multithreaded ServerSocket Communication Simple instance (based on TCP protocol)