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;
    }
}

你可能感兴趣的:(java)