java中给出一个多线程TCP的ServerSocket例子?

以下的例子,是一个服务器对多个客户端。我们的客户端程序可以运行很多遍,代表多个客户。
/*in this frame work, many clients can access the server with many thread and many socket using only one port,
bbb client use bbb socket with bbb thread, by default, one port can accept 50 socket. */


import java.net.ServerSocket;
import java.io.*;
import java.net.Socket;

public class ThreadServers {

public static void main(String[] args) {
try {
/* public ServerSocket(int port)
throws IOExceptionCreates a server socket, bound to the specified port.
The maximum queue length for incoming connection indications (a request to connect) is set to 50. If a connection indication
arrives when the queue is full, the connection is refused.


public ServerSocket(int port,int backlog)
throws IOException Creates a server socket and binds it to the specified local port number, with the specified backlog.
The maximum queue length for incoming connection indications (a request to connect) is set to the backlog parameter. If a connection
indication arrives when the queue is full, the connection is refused.

*/
ServerSocket ss = new ServerSocket(8089);
for(;;){
/* here this ServerSocket can accept unlimited client socket.every time after
it accept one, it just come back from the loop, and block here on accept statement.
a new client corresponds to a new socket */
Socket s = ss.accept();
/* this reader get data from socket, then print in the console.
a new client corresponds to a new thread */
ReadThread reader = new ReadThread(s);
// WriteThread writer = new WriteThread(s);

/* WriteThread get the input from console, then write it to the network. */
reader.start();
/*in this case, we have to comment out the following statement because if there
are two clients, if we type in characters in the console, which clients do we
type in to send out to? so the example is made easier not to server to send
out, to make it work, you can make the server to pop up two windows, then one window
corresponds to one client, in window, if you type in some characters, you send them to this client. */
// writer.start();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}

}

 




以下是客户端程序:

import java.net.Socket;
import java.net.*;
import java.io.*;

public class ThreadClients {

public static void main(String[] args) {
Socket s = null;
try {
/*a new client corresponds to a new socket*/
s = new Socket("localhost", 8089);
/* this reader get data from socket, then print out in the console. */
/* WriteThread get the input from console, then write it to the network. */
// ReadThread reader = new ReadThread(s);
/*a new client corresponds to a new thread*/
WriteThread writer = new WriteThread(s);
// reader.start();
writer.start();
}
catch (Exception ex) {
ex.printStackTrace();
}


}

}

 

/*this reader get data from socket, then print in the console. */


import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;




public class ReadThread extends Thread {
private Socket socket;
public ReadThread(Socket socket) {
this.socket = socket;
}
public void run(){
try {
BufferedReader in = new BufferedReader
(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while(true){
String line = in.readLine();
out.println(line+" coming back from server");
System.out.println(line);
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}

 

/*WriteThread get the input from console, then write it to the network.*/

import java.net.Socket;
import java.io.PrintWriter;
import java.io.*;




public class WriteThread extends Thread {
private Socket socket;
public WriteThread(Socket socket) {
this.socket = socket;
}
public void run(){
try {
/*PrintWriter(OutputStream out, boolean autoFlush)
Create a new PrintWriter from an existing OutputStream.
public PrintWriter(Writer out,boolean autoFlush) Create a new PrintWriter.
autoFlush - A boolean; if true, the println() methods will flush the output buffer */
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader inn = new BufferedReader
(new InputStreamReader(socket.getInputStream()));
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
for(;;){
String line = in.readLine();
out.println(line);
String linen = inn.readLine();
System.out.println(linen);

}
}
catch (IOException ex) {
}
}
}

 
when run,start server, then start several clients.