新手练练----也做即时通信系统(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(0, 0, 435, 327));
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(139, 75, 178, 41));
jLabel2.setText("密码:");
jLabel2.setBounds(new Rectangle(56, 162, 74, 38));
tfPassword.setBounds(new Rectangle(137, 156, 182, 41));
btnLogin.setBounds(new Rectangle(90, 254, 87, 32));
btnLogin.setText("登陆");
btnReset.setBounds(new Rectangle(238, 251, 84, 34));
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(56, 76, 71, 38));
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) {
}
}
}

运行结果:



自己水平有限,做的这个小东西实在拿不出手,但还是对自己有些帮助,我会继续努力的,
(一)客户端:
login.java


















































































































































































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





























































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




























































ServerThread.java




































































































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

作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2006-06-22 00:00 Phinecos(洞庭散人) 阅读(852) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述