新手练练----也做即时通信系统(1)

      实践出真知,还得要多动手才行。今天做的放上来,实现了客户端的登陆功能,慢慢加功能,锻炼自己的j2se水平。。。功能太简单了(本人水平有限^o^)。
(一)客户端:
login.java
package vitaminclient;

import java.awt.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.net.*;
import java.io.*;
import java.util.*;


/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * 
@author not attributable
 * 
@version 1.0
 
*/

public class Login extends JFrame
{
    
private String userName = "";//用户名
    private String password = "";//密码
    private Socket socket = null;//客户端socket
    private java.io.BufferedReader in = null;//读数据的
    private java.io.PrintWriter out = null;//向服务器写数据
    private static final int SeverPort = 6018;//服务器端口
    private String clientCmd = "";
    
private String serverMsg = "";

    
public Login() {
        
try {
            jbInit();
        }
 catch (Exception exception) {
            exception.printStackTrace();
        }

    }


    
private void jbInit() throws Exception {
        getContentPane().setLayout(
null);
        jPanel1.setBounds(
new Rectangle(00435327));
        jPanel1.setLayout(
null);
        
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
this.setResizable(false);
        
this.setTitle("登陆");


        btnLogin.addActionListener(
new Login_btnLogin_actionAdapter(this));
        btnReset.addActionListener(
new Login_btnReset_actionAdapter(this));

        
this.getContentPane().add(jPanel1);
        tfName.setBounds(
new Rectangle(1397517841));
        jLabel2.setText(
"密码:");
        jLabel2.setBounds(
new Rectangle(561627438));
        tfPassword.setBounds(
new Rectangle(13715618241));
        btnLogin.setBounds(
new Rectangle(902548732));
        btnLogin.setText(
"登陆");
        btnReset.setBounds(
new Rectangle(2382518434));
        btnReset.setText(
"重置");
        jPanel1.add(jLabel1);
        jPanel1.add(jLabel2);
        jPanel1.add(tfName);
        jPanel1.add(tfPassword);
        jPanel1.add(btnLogin);
        jPanel1.add(btnReset);
        jLabel1.setText(
"用户名:");
        jLabel1.setBounds(
new Rectangle(56767138));
        
this.setLocation(310,200);
        
this.setSize(400,400);
    }


    
public static void main(String[] args) {
        Login login 
= new Login();
        login.setVisible(
true);
    }


    JPanel jPanel1 
= new JPanel();
    JLabel jLabel1 
= new JLabel();
    JTextField tfName 
= new JTextField();
    JLabel jLabel2 
= new JLabel();
    JPasswordField tfPassword 
= new JPasswordField();
    JButton btnLogin 
= new JButton();
    JButton btnReset 
= new JButton();
    
public void btnLogin_actionPerformed(ActionEvent e)
    
{//用户登陆
        int numRead;
       
if(tfName.getText().trim().length()==0)
       
{
           javax.swing.JOptionPane.showMessageDialog(
this,"请输入用户名!!!","用户登陆",JOptionPane.WARNING_MESSAGE);
           
return;
       }

       
if(tfPassword.getPassword().length==0)
       
{
           javax.swing.JOptionPane.showMessageDialog(
this,"请输入密码!!!","用户登陆",JOptionPane.WARNING_MESSAGE);
           
return;
       }

       
this.userName = tfName.getText().trim();//获取用户名
       this.password = String.valueOf(tfPassword.getPassword());//获取密码
       this.clientCmd = "login " +this.userName+" " +this.password;

       
//this.clientCmd = "login";
       try
       
{
           
this.socket = new Socket(InetAddress.getLocalHost(),SeverPort);//连接服务器
           this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//从服务器读数据的
           this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);//向数据库写数据的
           out.println(this.clientCmd);

           
this.serverMsg = in.readLine();
           
if(this.serverMsg.equals(new String("LoginBad")))
           
{
               javax.swing.JOptionPane.showMessageDialog(
this,"登陆失败!!!");
               
return;
           }

           
else if(this.serverMsg.equals(new String("LoginGood")))
           
{
               vitaminclient.clientMain cm 
= new clientMain();
               cm.setSize(
200,500);
               cm.setVisible(
true);
               
this.dispose();

           }


          }

       
catch(java.io.IOException ex)
       
{
           System.err.println(ex.getMessage().toString());
           ex.printStackTrace();
       }

       
catch(java.lang.Exception ex)
       
{
           System.err.println(ex.getMessage().toString());
           ex.printStackTrace();
       }

    }

    
public void btnReset_actionPerformed(ActionEvent e) {
        
this.tfName.setText("");
        
this.tfPassword.setText("");
    }


}



class Login_btnReset_actionAdapter implements ActionListener {
    
private Login adaptee;
    Login_btnReset_actionAdapter(Login adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnReset_actionPerformed(e);
    }

}








class Login_btnLogin_actionAdapter implements ActionListener {
    
private Login adaptee;
    Login_btnLogin_actionAdapter(Login adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnLogin_actionPerformed(e);
    }

}


clientMain.java(开发中。。。)

(二)服务器端
   数据库用的是Access,图个简单,反正是做着练手用的,能偷懒就尽量吧。。。。
   业务逻辑和数据访问分开的,数据访问我封装了一个javaBean类来实现:
DBbase.java
package com.vitamin.DataAccess;

import java.sql.*;

public class DBbase {
        String sDBDriver 
= "sun.jdbc.odbc.JdbcOdbcDriver";
        String sConnstr 
= "jdbc:odbc:myDB";
        Connection connect 
= null;
        ResultSet rs 
= null;
        Statement stmt 
= null;


        
public DBbase()
        
{

                
try
                
{

                        Class.forName(sDBDriver);

                }

                
catch(ClassNotFoundException ex)
                
{
                        System.err.println(ex.getMessage());

                }

        }

        
public ResultSet executeQuery(String sql)
        
{

                
try
                
{
                        
this.connect = DriverManager.getConnection(sConnstr);
                        
this.stmt = this.connect.createStatement();
                        rs 
= stmt.executeQuery(sql);
                }

                
catch(SQLException ex)
                
{
                        System.err.println(ex.getMessage());
                }

                
return rs;
        }

        
public int executeUpdate(String sql)
        
{
                
int result = 0;
                
try
                
{
                        
this.connect = DriverManager.getConnection(sConnstr);
                        
this.stmt = this.connect.createStatement();
                        result 
= stmt.executeUpdate(sql);
                }

                
catch(SQLException ex)
                
{
                        System.err.println(ex.getMessage());
                }

                
return result;
        }


}



服务器端
   为了简单,连GUI都不弄了,等后期再来完善吧,先把主要功能做出来再说:
server.java
package com.vitamin.vitaminserver;

import java.net.*;
import java.io.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * 
@author not attributable
 * 
@version 1.0
 
*/

public class server
{



    
public static void main(String[] args) throws java.io.IOException
    
{
        java.net.ServerSocket s 
= new ServerSocket(6018);
        System.out.println(
"服务器启动:"+s);
        
try
        
{
            
while(true)
            
{
               java.net.Socket  socket 
= s.accept();
                System.out.println(
"连接接受"+socket);
                
try
                
{
                    
new ServerThread(socket);
                }

                
catch(java.io.IOException ex)
                
{
                    socket.close();
                }


            }

        }

        
catch(java.lang.Exception ex)
        
{
            System.err.println(ex.getMessage().toString());
            ex.printStackTrace();
        }

        
finally
        
{
            s.close();
        }


    }





}


ServerThread.java
package com.vitamin.vitaminserver;

import java.io.*;
import java.net.*;
import java.util.*;
import com.vitamin.DataAccess.*;


/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * 
@author not attributable
 * 
@version 1.0
 
*/

public class ServerThread extends java.lang.Thread
{
    
private java.net.Socket socket = null;
    
private java.io.BufferedReader in = null;//读数据的
    private java.io.PrintWriter out = null;//向客户写数据
    private String clientMsg = "";
    
private String sql = "";
    
private java.sql.ResultSet rs = null;

    
public ServerThread()
    
{
        
super();
    }

    
public ServerThread(Socket s)throws java.io.IOException
    
{
        
this.socket = s;
        
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
        
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())),true);
        
this.start();//启动线程
    }


    
public void run()
    
{
        String[] msgTmp;
        String spliter 
= " ";
        
try
        
{
            
while(true)
            
{
                
this.clientMsg = in.readLine();
                System.out.println(
this.clientMsg);
                 msgTmp 
= this.clientMsg.split(spliter);
                 System.out.println(msgTmp[
0]);
                
if(msgTmp[0].equals(new String("login")))
                
{
                    String name 
= "";
                    String pwd 
= "";
                    System.out.println(
this.clientMsg);
                    name 
= msgTmp[1];
                    pwd 
= msgTmp[2];
                    com.vitamin.DataAccess.DBbase myDb 
= new DBbase();
                    
this.sql = "select count(*) as count from users where username='"+name+"' and password = '"
                               
+pwd +"'";
                    
this.rs = myDb.executeQuery(this.sql);
                     
int result = 0;
                    
if(rs.next())
                    
{
                        result 
= rs.getInt("count");
                    }

                    
if(result>=1)
                    
{
                        
this.out.println("LoginGood");
                    }

                    
else
                   
{
                        
this.out.println("LoginBad");
                    }



                }

                
else
                
{


                }


            }

        }

        
catch(java.lang.Exception ex)
        
{
            System.out.println(ex.getMessage().toString());
            ex.printStackTrace();
        }

        
try {
            
this.socket.close();
        }
 catch (IOException ex1) {
        }

    }

}


运行结果:
6-20-1.GIF
6-20-2.GIF
6-20-3.GIF

自己水平有限,做的这个小东西实在拿不出手,但还是对自己有些帮助,我会继续努力的,

posted on 2006-06-22 00:00  Phinecos(洞庭散人)  阅读(848)  评论(0编辑  收藏  举报

导航