S
solarzzy
Unregistered / Unconfirmed
GUEST, unregistred user!
定义对得到的数据包的处理:
type
TDbServerThread = class(TServerClientThread)
private
strCommand: string;
strFeedback: string;
public
procedure ClientExecute; override;
procedure Log;
procedure LogFeedback;
procedure AddRecord;
end;
一些具体的处理:
procedure TDbServerThread.Log;
begin
Form1.lbLog.Items.Add ('Request: ' + strCommand);
end;
procedure TDbServerThread.LogFeedback;
begin
Form1.lbLog.Items.Add ('Response: ' + strFeedback);
end;
procedure TDbServerThread.AddRecord;
var
Data: TStringList;
begin
Data := TStringList.Create;
try
Data.Text := strCommand;
{
// new record
Form1.Table1.Insert;
// set the fields using the strings
for I := 0 to Form1.Table1.FieldCount - 1 do
Form1.Table1.Fields .AsString :=
Data.Values [Form1.Table1.Fields.FieldName];
// complete with random ID, sender, and date
Form1.Table1CompID.AsInteger := ID;
Inc(ID);
Form1.Table1LoggedBy.AsString := ClientSocket.RemoteAddress;
Form1.Table1LoggetOn.AsDateTime := Date;
Form1.Table1.Post;
}
// get the value to return
//strFeedback := Form1.Table1CompID.AsString;
finally
Data.Free;
end;
end;
procedure TDbServerThread.ClientExecute;
var
Stream:TWinSocketStream;
Buffer, strIn:string;
nRead:Integer;
begin
//keep going
Stream := TWinSocketStream.Create(ClientSocket, 1);
try
while not Terminated and ClientSocket.Connected do
begin
//initialize (thread might be reused)
Buffer := '';
strIn := '';
SetLength(Buffer, 128);
repeat
nRead := Stream.Read(Buffer[1], 128);
if nRead = 0 then
begin
ClientSocket.Close;
Break;
end;
SetLength(Buffer, nRead);
StrIn := StrIn + Buffer;
until(Pos(#13, Buffer) > 0);
if strIn = '' then
begin
Continue; // keep going
end
else
begin
//handle the request, if anything arrived
StrCommand := Copy(strIn, 1, 2);
showmessage(strcommand);
Synchronize(Log);
Synchronize(AddRecord);
//send results back
Synchronize(LogFeedback);
// Stream.Write(strFeedback[1], Length (strFeedback));
end;
end;
finally
Stream.Free;
end;
end;
procedure TForm1.ss_ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
lbLog.Items.Add('Connected: ' + Socket.RemoteHost + ' (' + Socket.RemoteAddress + ')');
end;
procedure TForm1.ss_ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
lbLog.Items.Add ('Accepted: ' + Socket.RemoteHost + ' (' + Socket.RemoteAddress + ')');
end;
procedure TForm1.ss_ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
lbLog.Items.Add ('Disconnected: ' + Socket.RemoteHost + ' (' + Socket.RemoteAddress + ')');
end;
procedure TForm1.ss_ServerSocketGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
lbLog.Items.Add ('GetThread: ' + ClientSocket.RemoteHost + ' (' + ClientSocket.RemoteAddress + ')' );
SocketThread := TDbServerThread.Create(False, ClientSocket);
end;
procedure TForm1.ss_ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
lbLog.Items.Add('Connected: ' + Socket.RemoteHost + ' (' + Socket.RemoteAddress + ')');
end;
哪位大佬帮我看看,指点一下,为什么我接收数据特别的慢!?要几十秒,我要达到不间断
及时的接收,应该怎么修改?(注:threadcachsize = 1000)
type
TDbServerThread = class(TServerClientThread)
private
strCommand: string;
strFeedback: string;
public
procedure ClientExecute; override;
procedure Log;
procedure LogFeedback;
procedure AddRecord;
end;
一些具体的处理:
procedure TDbServerThread.Log;
begin
Form1.lbLog.Items.Add ('Request: ' + strCommand);
end;
procedure TDbServerThread.LogFeedback;
begin
Form1.lbLog.Items.Add ('Response: ' + strFeedback);
end;
procedure TDbServerThread.AddRecord;
var
Data: TStringList;
begin
Data := TStringList.Create;
try
Data.Text := strCommand;
{
// new record
Form1.Table1.Insert;
// set the fields using the strings
for I := 0 to Form1.Table1.FieldCount - 1 do
Form1.Table1.Fields .AsString :=
Data.Values [Form1.Table1.Fields.FieldName];
// complete with random ID, sender, and date
Form1.Table1CompID.AsInteger := ID;
Inc(ID);
Form1.Table1LoggedBy.AsString := ClientSocket.RemoteAddress;
Form1.Table1LoggetOn.AsDateTime := Date;
Form1.Table1.Post;
}
// get the value to return
//strFeedback := Form1.Table1CompID.AsString;
finally
Data.Free;
end;
end;
procedure TDbServerThread.ClientExecute;
var
Stream:TWinSocketStream;
Buffer, strIn:string;
nRead:Integer;
begin
//keep going
Stream := TWinSocketStream.Create(ClientSocket, 1);
try
while not Terminated and ClientSocket.Connected do
begin
//initialize (thread might be reused)
Buffer := '';
strIn := '';
SetLength(Buffer, 128);
repeat
nRead := Stream.Read(Buffer[1], 128);
if nRead = 0 then
begin
ClientSocket.Close;
Break;
end;
SetLength(Buffer, nRead);
StrIn := StrIn + Buffer;
until(Pos(#13, Buffer) > 0);
if strIn = '' then
begin
Continue; // keep going
end
else
begin
//handle the request, if anything arrived
StrCommand := Copy(strIn, 1, 2);
showmessage(strcommand);
Synchronize(Log);
Synchronize(AddRecord);
//send results back
Synchronize(LogFeedback);
// Stream.Write(strFeedback[1], Length (strFeedback));
end;
end;
finally
Stream.Free;
end;
end;
procedure TForm1.ss_ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
lbLog.Items.Add('Connected: ' + Socket.RemoteHost + ' (' + Socket.RemoteAddress + ')');
end;
procedure TForm1.ss_ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
lbLog.Items.Add ('Accepted: ' + Socket.RemoteHost + ' (' + Socket.RemoteAddress + ')');
end;
procedure TForm1.ss_ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
lbLog.Items.Add ('Disconnected: ' + Socket.RemoteHost + ' (' + Socket.RemoteAddress + ')');
end;
procedure TForm1.ss_ServerSocketGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
lbLog.Items.Add ('GetThread: ' + ClientSocket.RemoteHost + ' (' + ClientSocket.RemoteAddress + ')' );
SocketThread := TDbServerThread.Create(False, ClientSocket);
end;
procedure TForm1.ss_ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
lbLog.Items.Add('Connected: ' + Socket.RemoteHost + ' (' + Socket.RemoteAddress + ')');
end;
哪位大佬帮我看看,指点一下,为什么我接收数据特别的慢!?要几十秒,我要达到不间断
及时的接收,应该怎么修改?(注:threadcachsize = 1000)