簡易聊天程序教程(二)服務器的基本功能

源代碼下載鏈接:http://download.csdn.net/detail/sky453589103/9514686

如果有什麼問題,歡迎留言。

我設計的時候是先從服務器端開始設計的,服務器端先給出接口,然後客戶端針對接口編程。


在說服務器的代碼之前先說說數據庫的設計吧。

數據的設計是簡單的,只有兩個表:

create database SimpleChat;
CREATE TABLE IF NOT EXISTS Account (
    username varchar(50) NOT NULL,
    userpassword varchar(20) NOT NULL,
    sex ENUM('male', 'female') NOT NULL,
    status ENUM('on', 'off', 'leave', 'busy') NOT NULL,
    constraint A_PK primary  key(username)
);

CREATE TABLE IF NOT EXISTS Friend(
    user_1 varchar(50) NOT NULL,
    user_2 varchar(50) NOT NULL,
    constraint F_PK primary key (user_1, user_2),
    constraint F_FK1 foreign key (user_1) references Account(username),
    constraint F_FK2 foreign key (user_2) references Account(username)
);


只有這兩個表,表中的字段還不多。很方便學習。關於怎麼用java連接數據庫的,請看代碼張的Main.java文件和SimpleChatServer.java中的SimpleChatServer類的構造函數。

在源代碼中,服務器端的項目名稱是SimpleChatServer,

服務器端功能模塊包括下面幾個:

1 註冊模塊;

2 登陸模塊;

3 註銷模塊;

4 發送模塊;

5 添加朋友模塊;

6 刪除朋友模塊;

7 獲取朋友列表模塊;

8 輸入檢測模塊;

上面這幾個模塊基本對應着一個函數,除了輸入檢測。輸入檢測只是簡單的檢測輸入的用戶名或者密碼中有沒空格,分號等字符,算是簡單的攔截一下SQL注入吧。

值得注意的是,我在SimpleChatServer中設置了一個成員變量userBuf,這個變量用來存放的是已經登陸成功的用戶已經對用的Socket,因爲不可能每次需要查看一個用戶是否登陸就要去查數據庫,這樣會影響效率。但這個做法也會有一個缺點,就是會增大程序需要的內存。


按慣例也是拿出一個函數來說一說就可以了。大部分的實現是還很相似的,只是細節不相同。下面就註冊這個函數來說一下吧。

public ResponseMessage SignUp(String name, String password, Sex sex) {
        String sexValue = "";
        String statusValue = "off";

        if (Sex.MALE == sex) {
            sexValue = "male";
        } else if (Sex.FEMALE == sex) {
            sexValue = "female";
        }
        ResponseMessage response = new ResponseMessage(0, "Success");
        try {
            if (username.matches("[; ,]") || password.matches("[; ,]")) {
                response.setCode(1);
                response.setDescription("username or password has illigal charactor!");
                return response;
            }
            final String sql = "insert into Account values(\'" + name + "\', \'" + password + "\', \'" + sexValue
                    + "\', \'" + statusValue + "\');";
            Statement statement = conn.createStatement();
            statement.executeUpdate(sql);
        } catch (SQLException e) {
            response.setCode(1);
            response.setDescription(e.getMessage());
        }

        return response;
    }


需要重點關注的其實也就下面三句:
            final String sql = "insert into Account values(\'" + name + "\', \'" + password + "\', \'" + sexValue
                    + "\', \'" + statusValue + "\');";
            Statement statement = conn.createStatement();
            statement.executeUpdate(sql);
第一條語句是設定一條數據庫的語法。
第二條是在已有的數據連接上創建一個Statement,通過Statement來執行sql語句。
第三條語句是執行sql語句,因爲是insert,所有執行的是executeUpdate方法。如果想要查看類似select語句的例子,那請看獲取朋友列表的函數。

最後要說明的是,Server提供了添加在線用戶的接口和刪除在線用戶的接口。
	public void AddUser(String name, Socket s) {
		userBuf.put(name, s);
	}

	public void RemoveUser(String name) {
		userBuf.remove(name);
	}
其實這兩個函數只是吧userBuf中的響應的鍵值對給刪除了。不會訪問數據庫。寫這兩個方法的原因是爲了以後如果要改變userBuf的表現形式,可以不用改變客戶端的代碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章