應用多線程來實現server與多線程之間的通信的基本步驟
1、server端創建ServerSocket,循環調用accept()等待client鏈接
2、client創建一個Socket並請求和server端鏈接
3、server端接受client請求,創建socekt與該client建立專線鏈接
4、建立鏈接的socket在一個單獨的線程上對話
5、server繼續等待新的鏈接
server端Server.java
package test.concurrent.socket;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by dong on 15-6-22.
* 基於TCP協議的Socket通信。實現用戶登錄
* 服務器端
*/
public class Server {
public static void main(String[] args) {
try {
//1、創建一個服務器端Socket,即ServerSocket, 指定綁定的端口,並監聽此端口
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = null;
//記錄client的數量
int count = 0;
System.out.println("***服務器即將啟動,等待client的鏈接***");
//循環監聽等待client的鏈接
while (true){
//調用accept()方法開始監聽,等待client的鏈接
socket = serverSocket.accept();
//創建一個新的線程
ServerThread serverThread = new ServerThread(socket);
//啟動線程
serverThread.start();
count++; //統計client的數量
System.out.println("client的數量: " + count);
InetAddress address = socket.getInetAddress();
System.out.println("當前client的IP : " + address.getHostAddress());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
服務器端線程處理類ServerThread.java
package test.concurrent.socket;
import java.io.*;
import java.net.Socket;
/**
* Created by dong on 15-6-22.
* server端線程處理類
*/
public class ServerThread extends Thread {
//和本線程相關的Socket
Socket socket = null;
public ServerThread(Socket socket){
this.socket = socket;
}
//線程運行的操作,響應client的請求
public void run(){
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
OutputStream os = null;
PrintWriter pw = null;
try {
//獲取一個輸入流,並讀取client的信息
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(); //關閉輸入流
//獲取輸出流。響應client的請求
os = socket.getOutputStream();
pw = new PrintWriter(os); //包裝為打印流
pw.write("歡迎你");
pw.flush(); //將緩存輸出
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//關閉資源
if (pw != null)
pw.close();
if (os != null)
os.close();
if (is != null)
is.close();
if (isr != null)
isr.close();
if (br != null)
br.close();
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
clientClient.java
package test.concurrent.socket;
import java.io.*;
import java.net.Socket;
/**
* Created by dong on 15-6-22.
* client
*/
public class Client {
public static void main(String[] args) {
try {
//1、創建clientSocket,指定server端口號和地址
Socket socket = new Socket("localhost",8888);
//2、獲取輸出流,向server發送信息
OutputStream os = socket.getOutputStream(); //字節輸出流
PrintWriter pw = new PrintWriter(os); //將輸出流包裝為打印流
pw.write("username:tom; password:456");
pw.flush();
socket.shutdownOutput(); //關閉輸出流
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String info = null;
//循環讀取
while ((info = br.readLine()) != null){
System.out.println("我是client:server說:" + info);
}
br.close();
is.close();
isr.close();
pw.close();
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
