C
CJ
Unregistered / Unconfirmed
GUEST, unregistred user!
学到用时方恨少
写 Socket 写程序,一向用 NonBlockMode,这次需要把一个程序改为 BlockMode
没什么概念,有一个例子,看明白了,但对整个BlockMode还是没有什么概念,看
Delphi的帮助实在是@$@#!$。所以,哪位能帮忙把整个BlockMode的大概念解释一下
还有注意事项之类。
我拿到的例子如下:
{ TFileServerThread }
TFileServerThread = Class(TServerClientThread)
public
Procedure ClientExecute; override;
//有其它什么游泳的方法吗?
End;
{ TFileServerThread }
Procedure TFileServerThread.ClientExecute;
Var
Data: Array[0..1023] Of char;
RecText: String;
SocketStream: TWinSocketStream;
Begin
While Not Terminated And ClientSocket.Connected Do
Try
//超过30000指定时间读不到数据就退出?
SocketStream := TWinSocketStream.Create(ClientSocket, 30000);
Try
FillChar(Data, SizeOf(Data), 0);
//每次读取指定长度的数据,如果只有一半数据呢?继续等待?
If SocketStream.Read(Data, SizeOf(Data)) = 0 Then
Begin
// If we didn't get any data after xx seconds then close the connection
ClientSocket.SendText('Timeout on Server'+#13#10);
//Wait a little time to allow sending of text before disconnect
sleep(1);
ClientSocket.Close;
Terminate;
End;
RecText := Data;
If Length(RecText) > 2 Then
Delete(RecText, Pos(#13#10, RecText), 2); // Delete #13#10
If ClientSocket.Connected Then
Begin
ClientSocket.SendText(RecText);
SendMessage(Form1.Listbox1.Handle, LB_ADDSTRING, 0, Integer(PChar(RecText)));
PostMessage(Form1.Handle, CM_INCCOUNT, 0, 0);
End;
Finally
SocketStream.Free;
End;
Except
HandleException;
End;
End;
Procedure TForm1.ServerSocketGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
Var SocketThread: TServerClientThread);
Begin
// Create a new thread for connection
SocketThread := TFileServerThread.Create(False, ClientSocket);
ClientSocket.SendText('Welcome to Server'+#13#10);
End;
写 Socket 写程序,一向用 NonBlockMode,这次需要把一个程序改为 BlockMode
没什么概念,有一个例子,看明白了,但对整个BlockMode还是没有什么概念,看
Delphi的帮助实在是@$@#!$。所以,哪位能帮忙把整个BlockMode的大概念解释一下
还有注意事项之类。
我拿到的例子如下:
{ TFileServerThread }
TFileServerThread = Class(TServerClientThread)
public
Procedure ClientExecute; override;
//有其它什么游泳的方法吗?
End;
{ TFileServerThread }
Procedure TFileServerThread.ClientExecute;
Var
Data: Array[0..1023] Of char;
RecText: String;
SocketStream: TWinSocketStream;
Begin
While Not Terminated And ClientSocket.Connected Do
Try
//超过30000指定时间读不到数据就退出?
SocketStream := TWinSocketStream.Create(ClientSocket, 30000);
Try
FillChar(Data, SizeOf(Data), 0);
//每次读取指定长度的数据,如果只有一半数据呢?继续等待?
If SocketStream.Read(Data, SizeOf(Data)) = 0 Then
Begin
// If we didn't get any data after xx seconds then close the connection
ClientSocket.SendText('Timeout on Server'+#13#10);
//Wait a little time to allow sending of text before disconnect
sleep(1);
ClientSocket.Close;
Terminate;
End;
RecText := Data;
If Length(RecText) > 2 Then
Delete(RecText, Pos(#13#10, RecText), 2); // Delete #13#10
If ClientSocket.Connected Then
Begin
ClientSocket.SendText(RecText);
SendMessage(Form1.Listbox1.Handle, LB_ADDSTRING, 0, Integer(PChar(RecText)));
PostMessage(Form1.Handle, CM_INCCOUNT, 0, 0);
End;
Finally
SocketStream.Free;
End;
Except
HandleException;
End;
End;
Procedure TForm1.ServerSocketGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
Var SocketThread: TServerClientThread);
Begin
// Create a new thread for connection
SocketThread := TFileServerThread.Create(False, ClientSocket);
ClientSocket.SendText('Welcome to Server'+#13#10);
End;