1. 程式人生 > >JAVA學習筆記(六十)- 網路程式設計登入例項

JAVA學習筆記(六十)- 網路程式設計登入例項

客戶端

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/*
 * 客戶端
 */
public class
LoginClient {
public static void main(String[] args) { try { //1.建立客戶端Socket,指定伺服器地址和埠號,同時建立連線 Socket socket=new Socket("localhost", 8888); //2.獲取輸出流,向伺服器傳送資訊 OutputStream os=socket.getOutputStream(); //PrintWriter pw=new PrintWriter(os);//封裝為列印流
ObjectOutputStream oos=new ObjectOutputStream(os); User user=new User("admin", "123"); oos.writeObject(user);//序列化物件 socket.shutdownOutput();//關閉當前輸出流 //3.獲取輸入流,並讀取伺服器端的響應資訊 InputStream is=socket.getInputStream(); BufferedReader br=new
BufferedReader(new InputStreamReader(is)); String reply=br.readLine(); while(reply!=null){ System.out.println("我是客戶端,伺服器說:"+reply); reply=br.readLine(); } socket.shutdownInput(); //4.關閉資源 br.close(); is.close(); oos.close(); os.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

標題

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/*
 * 伺服器端
 */
public class LoginServer {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket=new ServerSocket(8888);
            System.out.println("******伺服器即將啟動,等待客戶端的連線");
            int count=0;//統計客戶端數量
            // 迴圈監聽等待客戶端的連線
            while(true){
                Socket socket=serverSocket.accept();
                //建立一個新的執行緒
                ServerThread thread=new ServerThread(socket);
                thread.start();//啟動執行緒

                count++;
                System.out.println("客戶端數量:"+count);
                System.out.println("客戶端IP:"+socket.getInetAddress().getHostAddress());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}

伺服器端執行緒處理類

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

/*
 * 伺服器端執行緒處理類
 */
public class ServerThread extends Thread {
    //與本執行緒相關的Socket
    Socket socket=null;

    public ServerThread(Socket socket){
        this.socket=socket;
    }

    //執行緒執行的操作
    @Override
    public void run() {
        InputStream is=null;
        ObjectInputStream ois=null;
        OutputStream os=null;
        PrintWriter pw=null;
        try {
            is=socket.getInputStream();//位元組流
            ois=new ObjectInputStream(is);
            User user=(User) ois.readObject();
            System.out.println("使用者名稱:"+user.getUsername());
            System.out.println("密碼:"+user.getPassword());
            socket.shutdownInput();//關閉當前輸入流
            os=socket.getOutputStream();
            pw=new PrintWriter(os);
            pw.println("歡迎您!");
            pw.flush();
            socket.shutdownOutput();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally{
            try {
                if(pw!=null)
                    pw.close();
                if(os!=null)
                    os.close();
                if(ois!=null)
                    ois.close();
                if(is!=null)
                    is.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

標題

import java.io.Serializable;

/*
 * 實體類User
 */
public class User implements Serializable {

    private static final long serialVersionUID = 6682618522883563864L;
    private int id;
    private String username;
    private String password;

    public User() {
        super();
    }

    public User(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }
}