マルチスレッドの例は、超古典で勉強できます。
http://www.iteye.com/blogs/tag/java%E5%A4%9A%E7%BA%BF%E7%A8%8B
サーバ端:
package thread;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
*
* ,
*
* (ClientMsgCollectThread ),
*
* Socket (ServerMsgSendThread )
*
* @author Faue
*/
public class DTUServer extends ServerSocket {
private static final int SERVER_PORT = 10000;
private boolean isShakeHandsSuccess = false;
/**
* ,
*
* @throws IOException
*/
public DTUServer() throws IOException {
super(SERVER_PORT);
try {
while (true) {
Socket socket = super.accept();
//
Thread thread = new Thread(new TimerThread(socket),
"getAndShow" + socket.getPort());
thread.start();
//
new Thread(new SendInstructionThread(socket), "send"
+ socket.getPort()).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
new DTUServer();
}
/**
*
*
* BDR
*
* @author Faue
* @version 1.0.0
*/
class TimerThread implements Runnable {
private Socket socket;
private BufferedReader in;
private StringBuffer inputStringBuffer = new StringBuffer("Hello");
/**
* Socket
*
* @param s
* @throws IOException
*/
public TimerThread(Socket s) throws IOException {
socket = s;
System.out.println(" ");
in = new BufferedReader(new InputStreamReader(
socket.getInputStream(), "GBK"));
}
public void run() {
try {
while (!socket.isClosed()) {
if (!isShakeHandsSuccess) {
//
String s = in.readLine();
System.out.println("s:" + s);
if (s.equals(" ")) {
isShakeHandsSuccess = true;
}
}
if (isShakeHandsSuccess) {
inputStringBuffer.delete(0, inputStringBuffer.length());
System.out.println(" , ");
inputStringBuffer.append(in.readLine());
System.out
.println(getMsg(inputStringBuffer.toString()));
}
System.out.println("---------------");
}
} catch (Exception e) {
// e.printStackTrace();
System.out.println(socket.toString() + " is closed!");
}
}
/**
*
*
* @param line
* @return
*/
private String getMsg(String line) {
return socket.toString() + " :" + line;
}
}
/**
*
*
* @author Faue
* @version 1.0.0
*/
class SendInstructionThread implements Runnable {
private Socket client;
private PrintWriter out;
private BufferedReader keyboardInput;
private StringBuffer outputStringBuffer = new StringBuffer("Hello");
/**
*
*
* @param s
* @throws IOException
*/
public SendInstructionThread(Socket s) throws IOException {
client = s;
System.out.println("ServerMsgSendThread");
out = new PrintWriter(client.getOutputStream(), true);
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
}
public void run() {
try {
while (!client.isClosed()) {
outputStringBuffer.delete(0, outputStringBuffer.length());
System.out.println(" ...");
outputStringBuffer.append(keyboardInput.readLine());
System.out.println(" ...");
out.println(outputStringBuffer.toString());
}
} catch (IOException e) {
// e.printStackTrace();
System.out.println(client.toString() + " is closed!");
}
}
}
}
クライアント:package thread;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
*
* IP ,
*
* @author Faue
*/
public class Client {
private Socket mySocket;
/**
*
*
* @param IP
* @throws IOException
*/
public Client(String IP) throws IOException {
try {
mySocket = new Socket(IP, 10000);
//
shakeHands(mySocket);
new Thread(new ServerMsgCollectThread(mySocket), "getAndShow"
+ mySocket.getPort()).start();
new Thread(new ClientMsgSendThread(mySocket), "send"
+ mySocket.getPort()).start();
} catch (IOException e) {
// e.printStackTrace();
System.out.println("Server.IP:" + IP
+ " port:10000 can not be Connected");
}
}
public static void main(String[] args) throws IOException {
try {
new Client("127.0.0.1");
} catch (Exception e) {
System.out.println(" IP ");
}
}
/**
*
*
* @author Faue
* @version 1.0.0
*/
class ServerMsgCollectThread implements Runnable {
private Socket client;
private BufferedReader in;
private StringBuffer inputStringBuffer = new StringBuffer("Hello");
/**
* Socket
*
* @param s
* @throws IOException
*/
public ServerMsgCollectThread(Socket s) throws IOException {
client = s;
in = new BufferedReader(new InputStreamReader(
client.getInputStream(), "GBK"));
}
public void run() {
try {
System.out.println(" .....");
while (!client.isClosed()) {
inputStringBuffer.delete(0, inputStringBuffer.length());
System.out.println(" .....");
inputStringBuffer.append(in.readLine());
System.out.println(getMsg(inputStringBuffer.toString()));
}
} catch (IOException e) {
// e.printStackTrace();
System.out.println(client.toString() + " is closed!");
System.exit(0);
}
}
/**
*
*
* @param line
* @return
*/
private String getMsg(String line) {
return client.toString() + " says:" + line;
}
}
/**
*
*
* @author Faue
* @version 1.0.0
*/
class ClientMsgSendThread implements Runnable {
private Socket client;
private PrintWriter out;
private BufferedReader keyboardInput;
private StringBuffer outputStringBuffer = new StringBuffer("Hello");
/**
*
*
* @param s
* @throws IOException
*/
public ClientMsgSendThread(Socket s) throws IOException {
client = s;
out = new PrintWriter(client.getOutputStream(), true);
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
}
public void run() {
try {
while (!client.isClosed()) {
outputStringBuffer.delete(0, outputStringBuffer.length());
System.out.println(" .....");
outputStringBuffer.append(keyboardInput.readLine());
out.println(outputStringBuffer.toString());
}
out.println("--- See you, bye! ---");
} catch (Exception e) {
// e.printStackTrace();
System.out.println(client.toString() + " is closed!");
System.exit(0);
}
}
}
private void shakeHands(Socket s) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
s.getInputStream(), "GBK"));
StringBuffer outputStringBuffer = new StringBuffer("");
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
while (true) {
String hand = in.readLine();
System.out.println(" " + hand);
if (hand != null && "FE".equals(hand)) {
outputStringBuffer.delete(0, outputStringBuffer.length());
outputStringBuffer.append(" ");
out.println(outputStringBuffer.toString());
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}