TCP 登录实现代码

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

/**
 * 登录服务器
 * @author Administrator
 *
 */
public class LoginServerDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int port = 20000;
        try {
            ServerSocket  ss = new ServerSocket(port);
            System.out.println("服务器已启动");
            Socket socket = ss.accept();
            System.out.println("有客户连接进来");
            InputStream in = socket.getInputStream();
            BufferedReader br = 
                    new BufferedReader(new InputStreamReader(in));
            OutputStream out = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(out);
            String str = null;
            String name = "";
            String pwd = "";
            while( (str = br.readLine()) != null ){
                String tag = str.split(":")[0];
                if(tag.equals("name")){
                    name = str.split(":")[1];
                }else if(tag.equals("pwd")){
                    pwd = str.split(":")[1];
                }
                if((!name.equals(""))&&(!pwd.equals(""))){
                    //进行登录判断
                    if(name.equals("zhangsan") && pwd.equals("123456")){
                        pw.println("true");
                    }else{
                        pw.println("false");
                    }
                    pw.flush();
                    break;
                }
            }
            socket.close();//关闭连接
            ss.close();//关闭服务器
            System.out.println("服务器已关闭");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            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.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class LoginObjectServerDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int port = 20000;
        try {
            ServerSocket  ss = new ServerSocket(port);
            System.out.println("服务器已启动");
            Socket socket = ss.accept();
            System.out.println("有客户连接进来");
            InputStream in = socket.getInputStream();
            ObjectInputStream oin = new ObjectInputStream(in);
            OutputStream out = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(out);
            String str = null;
            String name = "zhangsan";
            String pwd = "123456";
            //接收客户端登录消息
            Object obj = null;
            if((obj = oin.readObject())!= null){
                UserInfo ui = (UserInfo)obj;
                if(name.equals(ui.getUsername())&&pwd.equals(ui.getPassword())){
                    pw.println("true");
                }else{
                    pw.println("false");
                }
                pw.flush();
            }

            socket.close();//关闭连接
            ss.close();//关闭服务器
            System.out.println("服务器已关闭");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-04-30 19:45  MrCharles在cnblogs  阅读(407)  评论(0编辑  收藏  举报

导航