題目:模擬網站的登錄,客戶端錄入賬號密碼,然后服務器端進行驗證(TCP)(完善)
完善(加入完整的處理異常的方式、多線程接收用戶請求)(TCP)
封裝的類
package com.gao.Project.Pro5;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -2201118496650087459L;
//快捷鍵Alt+Enter---->選第一個
//如果沒有出現,需設置
//File-->Settings--->Inspections--->Serializable class without 'serialVersionUID'--->勾選--->OK
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
}
線程
package com.gao.Project.Pro5;
import java.io.*;
import java.net.Socket;
public class ServerTread extends Thread {//
InputStream is = null;
ObjectInputStream ois = null;
OutputStream os = null;
DataOutputStream dos = null;
Socket s = null;
public ServerTread(Socket s){
this.s=s;
}
@Override
public void run() {
try {
is = s.getInputStream();
ois = new ObjectInputStream(is);
User user = (User)(ois.readObject());
boolean flag = false;
if(user.getName().equals("甜甜")&&user.getPwd().equals("12323")){
flag = true;
}
//向客戶端輸出一句話:---->操作流---->輸出流
os = s.getOutputStream();
dos =new DataOutputStream(os);
dos.writeUTF("你好,我是服務器端,我接收到你的請求了");
}catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}finally {
try {
if(dos!=null){
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客戶端
package com.gao.Project.Pro5;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class TestClient {//客戶端
public static void main(String[] args){
Socket s = null;
OutputStream os = null;
InputStream is= null;
ObjectOutputStream oos = null ;
DataInputStream dis = null;
try {
//錄入用戶的賬號密碼:
s = new Socket("192.168.2.142",8888);
Scanner sc = new Scanner(System.in);
System.out.println("請輸入您的賬號:");
String name = sc.next();
System.out.println("請輸入您的密碼:");
String pwd = sc.next();
//將賬號和密碼封裝成User的對象:
User user = new User(name,pwd);
//2.對于程序員來說,向外發送數據 感受--->利用輸出流
os = s.getOutputStream();
oos = new ObjectOutputStream(os);
oos.writeObject(user);
//接收服務器端的回話--->利用輸入流:
is = s.getInputStream();
dis = new DataInputStream(is);
boolean b = dis.readBoolean();
if(b){
System.out.println("登錄成功");
}else {
System.out.println("登錄失敗");
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//3.關閉流 + 關閉網絡資源:
try {
if(dis!=null){
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(oos!=null){
oos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(s!=null){
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服務器端
package com.gao.Project.Pro5;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServer {//服務器
public static void main(String[] args){
System.out.println("服務器已啟動");
ServerSocket ss = null;
Socket s = null;
int count = 0;//定義一個計算器,用來記錄客戶端的請求
try {
ss = new ServerSocket(8888);
while (true){//加入死循環,服務器一直監聽客戶端是否發送數據
s = ss.accept();
//每次過來的客戶端的請求 靠 線程處理:
new ServerTread(s).start();
count++;
System.out.println("當前是第"+count+"個用戶訪問服務器,訪問的用戶是:"+s.getInetAddress());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

浙公網安備 33010602011771號