登錄案例
服務(wù)器端
package com.company;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* 基于TCP協(xié)議的Socket通信导帝,實(shí)現(xiàn)用戶登錄
* 服務(wù)器端
* @param args
*/
public static void main(String[] args) {
try {
//1客们、創(chuàng)建服務(wù)器Socket臭笆,指定端口
ServerSocket serverSocket = new ServerSocket(8888);
//2、使用accept監(jiān)聽互亮,等待客戶端連接,處于阻塞狀態(tài)
System.out.println("服務(wù)端即將啟動余素,等待客戶連接豹休。。桨吊。");
Socket acceptSocket = serverSocket.accept();
//3威根、獲取輸入流讀取客戶端發(fā)送的信息
InputStream inputStream = acceptSocket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while ((line=br.readLine())!=null){
System.out.println("我是服務(wù)器,客戶端說:"+line);
}
acceptSocket.shutdownInput();
//4视乐、響應(yīng)客戶端的請求
OutputStream outputStream = acceptSocket.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(outputStream));
bw.write("歡迎您洛搀!");
bw.flush();
acceptSocket.shutdownOutput();
//5、關(guān)閉資源
br.close();
bw.close();
acceptSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客戶端
package com.company;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
//1炊林、創(chuàng)建客戶端socket姥卢,指定服務(wù)器地址和端口
Socket socket = new Socket("localhost",8888);
// Socket socket = new Socket("127.0.0.1",8888);
//2卷要、獲取輸出流渣聚,發(fā)送信息
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(outputStream));
bw.write("admin;123");
bw.flush();
socket.shutdownOutput();
//3、獲取服務(wù)器端的信息僧叉,并響應(yīng)
InputStream inputStream = socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while ((line=br.readLine())!=null){
System.out.println("已收到服務(wù)器端的回復(fù):"+line);
}
socket.shutdownInput();
//4奕枝、關(guān)閉資源
bw.close();
br.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
多線程實(shí)現(xiàn)服務(wù)器端
修改服務(wù)端
package com.company;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* 基于TCP協(xié)議的Socket通信,實(shí)現(xiàn)用戶登錄
* 服務(wù)器端
* @param args
*/
public static void main(String[] args) {
try {
//1瓶堕、創(chuàng)建服務(wù)器Socket隘道,指定端口
ServerSocket serverSocket = new ServerSocket(8888);
Socket acceptSocket=null;
System.out.println("服務(wù)端即將啟動,等待客戶連接郎笆。谭梗。。");
int count=0;
//死循環(huán)
while (true){
//2宛蚓、使用accept監(jiān)聽激捏,等待客戶端連接,處于阻塞狀態(tài),
// 阻塞時(shí)不會循環(huán)凄吏,當(dāng)有客戶端連接時(shí)循環(huán)繼續(xù)
acceptSocket = serverSocket.accept();
new ServerThread(acceptSocket).run();
count++;//統(tǒng)計(jì)客戶端數(shù)量
System.out.println("客戶端的數(shù)量為"+count);
InetAddress address=acceptSocket.getInetAddress();
System.out.println("當(dāng)前客戶端IP地址為:"+address.getHostAddress());
}
// //死循環(huán)代碼無法跑到這
// serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
線程
package com.company;
import java.io.*;
import java.net.Socket;
public class ServerThread extends Thread {
//和本線程相關(guān)的Socket
Socket mSocket = null;
public ServerThread(Socket socket) {
mSocket = socket;
}
//執(zhí)行線程远舅,響應(yīng)客戶端
@Override
public void run() {
BufferedReader br = null;
BufferedWriter bw = null;
//3、獲取輸入流讀取客戶端發(fā)送的信息
InputStream inputStream = null;
try {
inputStream = mSocket.getInputStream();
br = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("我是服務(wù)器痕钢,客戶端說:" + line);
}
mSocket.shutdownInput();
//4图柏、響應(yīng)客戶端的請求
OutputStream outputStream = mSocket.getOutputStream();
bw = new BufferedWriter(new OutputStreamWriter(outputStream));
bw.write("歡迎您!");
bw.flush();
mSocket.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
} finally {
//5任连、關(guān)閉資源
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (mSocket != null) {
try {
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}