對於聊天程式來說,登陸和註冊是很重要的,今天我就來講講登入和註冊功能。
第一步當然是新建個資料庫 。開啟microsoft access,新建資料庫,儲存為user.mdb,建表user,新建兩個欄位username和password記錄使用者名稱和密碼。然後在伺服器端中放乙個tadoquery和乙個tadoconnection。在formcreate中連線資料庫
const
dbname = 'user.mdb';
begin
....
tryadoconnection.close;
adoconnection.connectionstring := 'provider=microsoft.jet.oledb.4.0;'
+ 'data source=' + extractfilepath(paramstr(0)) + dbname
+ ';persist security info=false';
adoconnection.open();
except
end;
end;
客戶端在登入時會向伺服器端傳送cmd_login命令,所以我們在serverexecute進行處理
function sqlsearch(sqltext: string; sqlconnection: tadoconnection): boolean;
var adoquery: tadoquery;
begin
adoquery := tadoquery.create(nil);
adoquery.connection := sqlconnection;
with adoquery do
begin
close;
sql.clear;
sql.add(sqltext);
open;
result := not adoquery.isempty;
end;
end;
function tmainform.checkuser(username, password: string): boolean;
var str: string;
begin
str := 'select * from [user] where username=' + quotedstr(username) +
'and password=' + quotedstr(password);
result := sqlsearch(str, adoconnection);
end;
procedure tmainform.tcpserverexecute(acontext: tidcontext);
var s: string;
username, password: string;
begin
s := acontext.connection.socket.readln();
if s = 'cmd_login' then
begin
username := acontext.connection.socket.readln(); //讀取使用者名稱,密碼
password := acontext.connection.socket.readln();
if checkuser(username, password) then //檢查是否正確並回應客戶端
begin
acontext.connection.socket.writeln('login_successfully');
endelse
begin
acontext.connection.socket.writeln('login_failed');
end;
exit;
end;
.....
end;
同樣的,註冊也可以很簡單的實現procedure tmainform.tcpserverexecute(acontext: tidcontext);
var s: string;
username, password: string;
begin
... if s = 'cmd_register' then
begin
username := acontext.connection.socket.readln(); //讀取使用者名稱,密碼
password := acontext.connection.socket.readln();
if sqlsearch('select * from [user] where username=' + quotedstr(username), adoconnection) then
begin
acontext.connection.socket.writeln('register_failed'); //假如使用者名稱已被註冊,傳送register_failed
acontext.connection.socket.writeln('username is already registered.');
exit;
end;
adoquery.connection := adoconnection;
with adoquery do
begin
close;
sql.clear;
sql.add('insert into [user]([username], [password]) values(''' + username +
''', ''' + password + ''')');
execsql;
end;
acontext.connection.socket.writeln('register_successfully');
exit;
end;
...end;
修改一下客戶端的**function login(username, password: string): boolean;
var ret: string;
begin
suspendthread(listenthread);
log('username: '+ username);
tcpclient.socket.writeln('cmd_login'); //傳送cmd_login命令,使用者名稱,密碼
tcpclient.socket.writeln(username);
tcpclient.socket.writeln(password);
ret := tcpclient.socket.readln(); //接受伺服器端回應
if ret = 'login_failed' then //登陸失敗
begin
log('login failed.');
resumethread(listenthread);
result := false;
exit;
end;
resumethread(listenthread);
result := true;
end;
procedure reg(username, password: string);
var ret: string;
begin
suspendthread(listenthread);
log('username: '+ username);
tcpclient.socket.writeln('cmd_register'); //傳送cmd_login命令,使用者名稱,密碼
tcpclient.socket.writeln(username);
tcpclient.socket.writeln(password);
ret := tcpclient.socket.readln();
if ret = 'register_failed' then //註冊失敗
begin
ret := tcpclient.socket.readln();
log('register failed, error msg: ' + ret);
showmessage('register failed, error msg: ' + ret);
resumethread(listenthread);
exit;
end;
log('register successfully, ' + username);
resumethread(listenthread);
end;
在登入註冊時我將listenthread暫時掛起,目的是為了讓處理函式能接收到伺服器端的回應 Delphi簡單TCP聊天程式
首先上截圖 最近有點無聊,於是將以前寫到一半的聊天程式拿出來改進了一下。delphi自帶兩個控制項 tidtcpserver,tidtcpclient。顧名思義,乙個是伺服器端用的,另乙個是客戶端用的。程式主要思路如下,客戶端程式開始執行時與伺服器端建立連線,使用者登入時向伺服器端傳送login命令...
udp與tcp 簡單程式實現聊天
1.用udp進行交流 2.udp資料報模式 寫信的模型 from socket import def main 建立套接字 udp socket socket af inte,sock dgram 傳送方的ip與埠 send addr ip 埠號 繫結埠號 udp socket.bind send ...
TCP多主機聊天程式
多程序實現 1 include 2 include 3 include 4 include 5 include 6 include 7 include 8 include 9 10 void serverio int fd 11 21 else if s 0 22 26 else 27 32 33 ...