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

实践出真知,还得要多动手才行。今天做的放上来,实现了客户端的登陆功能,慢慢加功能,锻炼自己的j2se水平。。。功能太简单了(本人水平有限^o^)。
(一)客户端:
login.java
None.gif package vitaminclient;
None.gif
None.gif
import java.awt. * ;
None.gif
None.gif
import javax.swing. * ;
None.gif
import java.awt.event.ActionEvent;
None.gif
import java.awt.event.ActionListener;
None.gif
import java.awt.event.WindowEvent;
None.gif
import java.awt.event.WindowAdapter;
None.gif
import java.net. * ;
None.gif
import java.io. * ;
None.gif
import java.util. * ;
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/***/ /**
InBlock.gif*<p>Title:</p>
InBlock.gif*
InBlock.gif*<p>Description:</p>
InBlock.gif*
InBlock.gif*<p>Copyright:Copyright(c)2006</p>
InBlock.gif*
InBlock.gif*<p>Company:</p>
InBlock.gif*
InBlock.gif*
@authornotattributable
InBlock.gif*
@version1.0
ExpandedBlockEnd.gif
*/

None.gif
public class Login extends JFrame
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
privateStringuserName="";//用户名
InBlock.gif
privateStringpassword="";//密码
InBlock.gif
privateSocketsocket=null;//客户端socket
InBlock.gif
privatejava.io.BufferedReaderin=null;//读数据的
InBlock.gif
privatejava.io.PrintWriterout=null;//向服务器写数据
InBlock.gif
privatestaticfinalintSeverPort=6018;//服务器端口
InBlock.gif
privateStringclientCmd="";
InBlock.gif
privateStringserverMsg="";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicLogin()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
trydot.gif{
InBlock.gifjbInit();
ExpandedSubBlockStart.gifContractedSubBlock.gif}
catch(Exceptionexception)dot.gif{
InBlock.gifexception.printStackTrace();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
privatevoidjbInit()throwsExceptiondot.gif{
InBlock.gifgetContentPane().setLayout(
null);
InBlock.gifjPanel1.setBounds(
newRectangle(0,0,435,327));
InBlock.gifjPanel1.setLayout(
null);
InBlock.gif
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
InBlock.gif
this.setResizable(false);
InBlock.gif
this.setTitle("登陆");
InBlock.gif
InBlock.gif
InBlock.gifbtnLogin.addActionListener(
newLogin_btnLogin_actionAdapter(this));
InBlock.gifbtnReset.addActionListener(
newLogin_btnReset_actionAdapter(this));
InBlock.gif
InBlock.gif
this.getContentPane().add(jPanel1);
InBlock.giftfName.setBounds(
newRectangle(139,75,178,41));
InBlock.gifjLabel2.setText(
"密码:");
InBlock.gifjLabel2.setBounds(
newRectangle(56,162,74,38));
InBlock.giftfPassword.setBounds(
newRectangle(137,156,182,41));
InBlock.gifbtnLogin.setBounds(
newRectangle(90,254,87,32));
InBlock.gifbtnLogin.setText(
"登陆");
InBlock.gifbtnReset.setBounds(
newRectangle(238,251,84,34));
InBlock.gifbtnReset.setText(
"重置");
InBlock.gifjPanel1.add(jLabel1);
InBlock.gifjPanel1.add(jLabel2);
InBlock.gifjPanel1.add(tfName);
InBlock.gifjPanel1.add(tfPassword);
InBlock.gifjPanel1.add(btnLogin);
InBlock.gifjPanel1.add(btnReset);
InBlock.gifjLabel1.setText(
"用户名:");
InBlock.gifjLabel1.setBounds(
newRectangle(56,76,71,38));
InBlock.gif
this.setLocation(310,200);
InBlock.gif
this.setSize(400,400);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicstaticvoidmain(String[]args)dot.gif{
InBlock.gifLoginlogin
=newLogin();
InBlock.giflogin.setVisible(
true);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifJPaneljPanel1
=newJPanel();
InBlock.gifJLabeljLabel1
=newJLabel();
InBlock.gifJTextFieldtfName
=newJTextField();
InBlock.gifJLabeljLabel2
=newJLabel();
InBlock.gifJPasswordFieldtfPassword
=newJPasswordField();
InBlock.gifJButtonbtnLogin
=newJButton();
InBlock.gifJButtonbtnReset
=newJButton();
InBlock.gif
publicvoidbtnLogin_actionPerformed(ActionEvente)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//用户登陆
InBlock.gif
intnumRead;
InBlock.gif
if(tfName.getText().trim().length()==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifjavax.swing.JOptionPane.showMessageDialog(
this,"请输入用户名!!!","用户登陆",JOptionPane.WARNING_MESSAGE);
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
if(tfPassword.getPassword().length==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifjavax.swing.JOptionPane.showMessageDialog(
this,"请输入密码!!!","用户登陆",JOptionPane.WARNING_MESSAGE);
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
this.userName=tfName.getText().trim();//获取用户名
InBlock.gif
this.password=String.valueOf(tfPassword.getPassword());//获取密码
InBlock.gif
this.clientCmd="login"+this.userName+""+this.password;
InBlock.gif
InBlock.gif
//this.clientCmd="login";
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.socket=newSocket(InetAddress.getLocalHost(),SeverPort);//连接服务器
InBlock.gif
this.in=newBufferedReader(newInputStreamReader(socket.getInputStream()));//从服务器读数据的
InBlock.gif
this.out=newPrintWriter(newBufferedWriter(newOutputStreamWriter(socket.getOutputStream())),true);//向数据库写数据的
InBlock.gif
out.println(this.clientCmd);
InBlock.gif
InBlock.gif
this.serverMsg=in.readLine();
InBlock.gif
if(this.serverMsg.equals(newString("LoginBad")))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifjavax.swing.JOptionPane.showMessageDialog(
this,"登陆失败!!!");
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
elseif(this.serverMsg.equals(newString("LoginGood")))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifvitaminclient.clientMaincm
=newclientMain();
InBlock.gifcm.setSize(
200,500);
InBlock.gifcm.setVisible(
true);
InBlock.gif
this.dispose();
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(java.io.IOExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage().toString());
InBlock.gifex.printStackTrace();
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(java.lang.Exceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage().toString());
InBlock.gifex.printStackTrace();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidbtnReset_actionPerformed(ActionEvente)dot.gif{
InBlock.gif
this.tfName.setText("");
InBlock.gif
this.tfPassword.setText("");
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
class Login_btnReset_actionAdapter implements ActionListener dot.gif {
InBlock.gif
privateLoginadaptee;
ExpandedSubBlockStart.gifContractedSubBlock.gifLogin_btnReset_actionAdapter(Loginadaptee)
dot.gif{
InBlock.gif
this.adaptee=adaptee;
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidactionPerformed(ActionEvente)dot.gif{
InBlock.gifadaptee.btnReset_actionPerformed(e);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
class Login_btnLogin_actionAdapter implements ActionListener dot.gif {
InBlock.gif
privateLoginadaptee;
ExpandedSubBlockStart.gifContractedSubBlock.gifLogin_btnLogin_actionAdapter(Loginadaptee)
dot.gif{
InBlock.gif
this.adaptee=adaptee;
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidactionPerformed(ActionEvente)dot.gif{
InBlock.gifadaptee.btnLogin_actionPerformed(e);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

clientMain.java(开发中。。。)

(二)服务器端
数据库用的是Access,图个简单,反正是做着练手用的,能偷懒就尽量吧。。。。
业务逻辑和数据访问分开的,数据访问我封装了一个javaBean类来实现:
DBbase.java
None.gif package com.vitamin.DataAccess;
None.gif
None.gif
import java.sql. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class DBbase dot.gif {
InBlock.gifStringsDBDriver
="sun.jdbc.odbc.JdbcOdbcDriver";
InBlock.gifStringsConnstr
="jdbc:odbc:myDB";
InBlock.gifConnectionconnect
=null;
InBlock.gifResultSetrs
=null;
InBlock.gifStatementstmt
=null;
InBlock.gif
InBlock.gif
InBlock.gif
publicDBbase()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gifClass.forName(sDBDriver);
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(ClassNotFoundExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage());
InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
publicResultSetexecuteQuery(Stringsql)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.connect=DriverManager.getConnection(sConnstr);
InBlock.gif
this.stmt=this.connect.createStatement();
InBlock.gifrs
=stmt.executeQuery(sql);
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(SQLExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage());
ExpandedSubBlockEnd.gif}

InBlock.gif
returnrs;
ExpandedSubBlockEnd.gif}

InBlock.gif
publicintexecuteUpdate(Stringsql)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
intresult=0;
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.connect=DriverManager.getConnection(sConnstr);
InBlock.gif
this.stmt=this.connect.createStatement();
InBlock.gifresult
=stmt.executeUpdate(sql);
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(SQLExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage());
ExpandedSubBlockEnd.gif}

InBlock.gif
returnresult;
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif

服务器端
为了简单,连GUI都不弄了,等后期再来完善吧,先把主要功能做出来再说:
server.java
None.gif package com.vitamin.vitaminserver;
None.gif
None.gif
import java.net. * ;
None.gif
import java.io. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/***/ /**
InBlock.gif*<p>Title:</p>
InBlock.gif*
InBlock.gif*<p>Description:</p>
InBlock.gif*
InBlock.gif*<p>Copyright:Copyright(c)2006</p>
InBlock.gif*
InBlock.gif*<p>Company:</p>
InBlock.gif*
InBlock.gif*
@authornotattributable
InBlock.gif*
@version1.0
ExpandedBlockEnd.gif
*/

None.gif
public class server
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
publicstaticvoidmain(String[]args)throwsjava.io.IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifjava.net.ServerSockets
=newServerSocket(6018);
InBlock.gifSystem.out.println(
"服务器启动:"+s);
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifjava.net.Socketsocket
=s.accept();
InBlock.gifSystem.out.println(
"连接接受"+socket);
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
newServerThread(socket);
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(java.io.IOExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsocket.close();
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
catch(java.lang.Exceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage().toString());
InBlock.gifex.printStackTrace();
ExpandedSubBlockEnd.gif}

InBlock.gif
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifs.close();
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

ServerThread.java
None.gif package com.vitamin.vitaminserver;
None.gif
None.gif
import java.io. * ;
None.gif
import java.net. * ;
None.gif
import java.util. * ;
None.gif
import com.vitamin.DataAccess. * ;
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/***/ /**
InBlock.gif*<p>Title:</p>
InBlock.gif*
InBlock.gif*<p>Description:</p>
InBlock.gif*
InBlock.gif*<p>Copyright:Copyright(c)2006</p>
InBlock.gif*
InBlock.gif*<p>Company:</p>
InBlock.gif*
InBlock.gif*
@authornotattributable
InBlock.gif*
@version1.0
ExpandedBlockEnd.gif
*/

None.gif
public class ServerThread extends java.lang.Thread
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
privatejava.net.Socketsocket=null;
InBlock.gif
privatejava.io.BufferedReaderin=null;//读数据的
InBlock.gif
privatejava.io.PrintWriterout=null;//向客户写数据
InBlock.gif
privateStringclientMsg="";
InBlock.gif
privateStringsql="";
InBlock.gif
privatejava.sql.ResultSetrs=null;
InBlock.gif
InBlock.gif
publicServerThread()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
super();
ExpandedSubBlockEnd.gif}

InBlock.gif
publicServerThread(Sockets)throwsjava.io.IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.socket=s;
InBlock.gif
this.in=newBufferedReader(newInputStreamReader(this.socket.getInputStream()));
InBlock.gif
this.out=newPrintWriter(newBufferedWriter(newOutputStreamWriter(this.socket.getOutputStream())),true);
InBlock.gif
this.start();//启动线程
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
publicvoidrun()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifString[]msgTmp;
InBlock.gifStringspliter
="";
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.clientMsg=in.readLine();
InBlock.gifSystem.out.println(
this.clientMsg);
InBlock.gifmsgTmp
=this.clientMsg.split(spliter);
InBlock.gifSystem.out.println(msgTmp[
0]);
InBlock.gif
if(msgTmp[0].equals(newString("login")))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifStringname
="";
InBlock.gifStringpwd
="";
InBlock.gifSystem.out.println(
this.clientMsg);
InBlock.gifname
=msgTmp[1];
InBlock.gifpwd
=msgTmp[2];
InBlock.gifcom.vitamin.DataAccess.DBbasemyDb
=newDBbase();
InBlock.gif
this.sql="selectcount(*)ascountfromuserswhereusername='"+name+"'andpassword='"
InBlock.gif
+pwd+"'";
InBlock.gif
this.rs=myDb.executeQuery(this.sql);
InBlock.gif
intresult=0;
InBlock.gif
if(rs.next())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifresult
=rs.getInt("count");
ExpandedSubBlockEnd.gif}

InBlock.gif
if(result>=1)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.out.println("LoginGood");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.out.println("LoginBad");
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
catch(java.lang.Exceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.out.println(ex.getMessage().toString());
InBlock.gifex.printStackTrace();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
trydot.gif{
InBlock.gif
this.socket.close();
ExpandedSubBlockStart.gifContractedSubBlock.gif}
catch(IOExceptionex1)dot.gif{
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

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

自己水平有限,做的这个小东西实在拿不出手,但还是对自己有些帮助,我会继续努力的, hitwall.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值