使用基于TCP协议的 Socket 编程模拟实现用户登陆功能

》》服务器端
public class Server {

    public static void main(String[] args) {
        try {
            //创建一个服务器端Socket,即ServerSocket,指定绑定端口,并监听此端口
            ServerSocket serverSocket = new ServerSocket(8800);
            System.out.println("*****服务器即将启动,等待客户端的连接*****");
            //调用accept()方法开始监听,等待客户端的连接
            Socket socket = serverSocket.accept();
            //获取输入流,并读取客户端信息
            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("我是服务器,客户端说:" + info);
            }
            socket.shutdownInput();  //关闭输入流
            
            
            //响应客户端的请求
            //获取字节输出流,并向客户端发送信息
            OutputStream os = socket.getOutputStream();  
            PrintWriter pw = new PrintWriter(os);  //将字节输出流包装成打印流
            pw.write("登陆成功,欢迎您!");
            pw.flush();  //刷新缓存
            socket.shutdownOutput();
            
            serverSocket.close();  //关闭资源
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

》》客户端

public class Client {

    public static void main(String[] args) {
        try {
            //创建客户端Socket,指定服务器地址的端口
            Socket socket =new Socket("localhost",8800);
            //获取输出流,向服务器端发送信息
            OutputStream os = socket.getOutputStream();  //字节输出流
            PrintWriter pw = new PrintWriter(os);  //将字节输出流包装为打印流
            pw.write("用户名:admin;密码:123");
            pw.flush();
            socket.shutdownOutput(); //关闭输出流
            System.out.println("****客户端已发出请求,等待服务器端的反馈*****");
            
            //接受服务器端的反馈
            //获取输入流,并读取服务端的反馈
            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("我是客户端,服务器端说:" + info);
            }
            socket.shutdownInput();  //关闭输入流
            
            socket.close();  //关闭资源
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) { 
            e.printStackTrace();
        }
    }
}

注意:启动的时候一定要先启动服务端。

 使用多线程实现多客户端登陆 

》》服务器端
public class ServerThread extends Thread {

    //和本线程相关的 Socket
    Socket socket = null;
    
    public ServerThread(Socket socket){
        this.socket = socket;
    }
    
    //线程执行操作,响应客户端的请求
    @Override
    public void run() {
        try {
            //获取输入流,并读取客户端的信息
            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("我是服务器,客户端说:" + info);
            }
            socket.shutdownInput();  //关闭输入流
            
            //获取字节输出流,并向客户端发送信息
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            pw.write("登陆成功,欢迎您!");
            pw.flush();
            socket.shutdownOutput(); //关闭输出流
            
            socket.close(); //关闭资源流
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        try {
            //创建一个服务器端 Socket,即ServerSocket,指定绑定的端口,并监听此端口
            ServerSocket serverSocket = new ServerSocket(8800);
            Socket socket = null;
            System.out.println("*****客户端已启动,等待客户端的连接*****");
            //循环监听等待客户端的连接
            while(true){
                //调用 accept()方法开始监听,等待客户端的连接
                socket = serverSocket.accept();
                //创建一个新的线程
                ServerThread serverThread = new ServerThread(socket);
                serverThread.start();  //
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

》》客户端
public class Client {

    public static void main(String[] args) {
        try {
            //创建客户端Socket,指定服务器地址的端口
            Socket socket =new Socket("localhost",8800);
            //获取输出流,向服务器端发送信息
            OutputStream os = socket.getOutputStream();  //字节输出流
            PrintWriter pw = new PrintWriter(os);  //将字节输出流包装为打印流
            //pw.write("用户名:admin;密码:123");
            pw.write("用户名:szj;密码:123");
            pw.flush();
            socket.shutdownOutput(); //关闭输出流
            System.out.println("****客户端已发出请求,等待服务器端的反馈*****");
            
            //接受服务器端的反馈
            //获取输入流,并读取服务端的反馈  
            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("我是客户端,服务器端说:" + info);
            }
            socket.shutdownInput();  //关闭输入流
                             
            socket.close();  //关闭资源
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) { 
            e.printStackTrace();
        }
    }
}

 基于 UDP 的 Socket

》》服务端
public class UDPServer {

    public static void main(String[] args) throws IOException {
        //创建服务器端DatagramSocket,指定端口
        DatagramSocket socket = new DatagramSocket(8800);
        //创建数据报,用于接收客户端的数据
        byte[] data = new byte[1024]; //创建字节数组,指定接收的数据报大小
        DatagramPacket packet = new DatagramPacket(data, data.length);
        System.out.println("*****服务器已经启动,等待客户端发送数据*****");
        //接收客户端发送的数据
        socket.receive(packet);  //此方法在接收到数据报之前会一直阻塞
        //读取数据
        String info = new String(data,0,packet.getLength());
        System.out.println("我是服务器,客户端说:" + info);
        
        /**
         * 向客户端响应数据
         */
        //定义客户端的地址、端口号、数据
        InetAddress address = packet.getAddress();
        int port = packet.getPort();
        byte[]  data1 = "欢迎您!".getBytes();
        //创建数据报,包含响应的数据信息
        DatagramPacket packet1 =new DatagramPacket(data1, data1.length, address, port);
        //响应客户端
        socket.send(packet1);
        socket.close();  //关闭资源
    }
}

 

》》客户端
public class UDPClient {

    public static void main(String[] args) throws IOException {
        InetAddress address = InetAddress.getByName("localhost");    
        int port = 8800;
        byte[] data = "用户名:admin;密码:123".getBytes(); 
        //创建数据报,包含发送的数据信息
        DatagramPacket packet = new DatagramPacket(data,data.length,address,port);
        //创建DatagramSocket对象
        DatagramSocket socket =new DatagramSocket();
        //向服务器发送数据报
        socket.send(packet);
        System.out.println("*****我是客户端,等待服务器的响应*****");
        /**
         * 接收服务器端的响应数据
         */
        //创建数据报,用于接收服务器响应的数据
        byte[] data2 =new byte[1024];
        DatagramPacket packet1 = new DatagramPacket(data2, data2.length);
        //接收服务器响应的数据     
        socket.receive(packet1);
        
        //读取数据
        String info = new String(data2,0,packet1.getLength());
        System.out.println("我是客户端,服务器说:" + info);
        
        socket.close();  //关闭资源
    }
}

 

 posted on 2017-09-21 12:00  【邵江】  阅读(183)  评论(0编辑  收藏  举报