请TServerSocket高手进来 (100分)

  • 主题发起人 主题发起人 zlj555
  • 开始时间 开始时间
Z

zlj555

Unregistered / Unconfirmed
GUEST, unregistred user!
请问关于TClientSocket和TServerSocket中用到的TSocketThread以及TServerClientWinSocket是作什么用的?什么时候会用到?怎么用?
阻塞模式与非阻塞模式,各自的利弊在哪?
一般都用在什么场合。
 
线程阻塞模式的时候用
 
//给个例子

Unit main;

Interface

Uses
Windows, SysUtils, Messages, Classes, Forms, ScktComp, Controls, StdCtrls,
Menus, Mask, Spin, ComCtrls, ExtCtrls;

Const
CM_IncCount = WM_USER + 1;

Type
TForm1 = Class(TForm)
ServerSocket: TServerSocket;
MainMenu: TMainMenu;
File1: TMenuItem;
ActiveItem: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
Panel1: TPanel;
Label1: TLabel;
CacheEdit: TSpinEdit;
Label2: TLabel;
PortEdit: TSpinEdit;
Label3: TLabel;
ThreadCount: TEdit;
Panel2: TPanel;
ListBox1: TListBox;
Panel3: TPanel;
StatusBar1: TStatusBar;
CharCount: TLabel;
Procedure ServerSocketGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
Var SocketThread: TServerClientThread);
Procedure FormCreate(Sender: TObject);
Procedure FormClose(Sender: TObject; Var Action: TCloseAction);
Procedure Exit1Click(Sender: TObject);
Procedure PortEditChange(Sender: TObject);
Procedure ActiveItemClick(Sender: TObject);
Procedure ServerSocketThreadEnd(Sender: TObject;
Thread: TServerClientThread);
Procedure ServerSocketThreadStart(Sender: TObject;
Thread: TServerClientThread);
Procedure CacheEditChange(Sender: TObject);
protected
Procedure CMIncCount(Var Msg: TMessage); message CM_IncCount;
public
End;

{ TFileServerThread }

TFileServerThread = Class(TServerClientThread)
public
Procedure ClientExecute; override;
End;

Var
Form1: TForm1;

Implementation

{$R *.DFM}

{ TFileServerThread }

Procedure TFileServerThread.ClientExecute;
Var
Data: Array[0..1023] Of char;
RecText: String;
SocketStream: TWinSocketStream;
Begin
While Not Terminated And ClientSocket.Connected Do
Try
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;

Procedure TForm1.FormCreate(Sender: TObject);
Begin
CacheEdit.Value := ServerSocket.ThreadCacheSize;
PortEdit.Value := ServerSocket.Port;
CharCount.Caption := '0';
ActiveItemClick(Nil);
End;

Procedure TForm1.FormClose(Sender: TObject; Var Action: TCloseAction);
Begin
ServerSocket.Close;
End;

Procedure TForm1.CMIncCount(Var Msg: TMessage);
Begin
CharCount.Caption := IntToStr(StrToInt(CharCount.Caption) + 1);
End;

Procedure TForm1.Exit1Click(Sender: TObject);
Begin
Close;
End;

Procedure TForm1.PortEditChange(Sender: TObject);
Begin
ServerSocket.Port := StrToInt(PortEdit.Text);
End;

Procedure TForm1.ActiveItemClick(Sender: TObject);
Begin
ServerSocket.Active := Not ServerSocket.Active;
ActiveItem.Checked := ServerSocket.Active;
If ServerSocket.Active Then
StatusBar1.SimpleText := 'Active'
Else
StatusBar1.SimpleText := 'InActive';
End;

Procedure TForm1.ServerSocketThreadEnd(Sender: TObject;
Thread: TServerClientThread);
Begin
ThreadCount.Text := IntToStr(StrToInt(ThreadCount.Text) - 1);
End;

Procedure TForm1.ServerSocketThreadStart(Sender: TObject;
Thread: TServerClientThread);
Begin
ThreadCount.Text := IntToStr(StrToInt(ThreadCount.Text) + 1);
End;

Procedure TForm1.CacheEditChange(Sender: TObject);
Begin
ServerSocket.ThreadCacheSize := CacheEdit.Value;
End;

End.

 
不好意思,这几天没有上来,那我再问一下:阻塞模式与非阻塞模式,各自的利弊在哪?
一般都用在什么场合。
 
请问以下那里有TClientSocket和TServerSocket控件下载
 
在DELPHI的Internet页中都带了。
 
谁能告诉我利弊在哪儿呀?各用在什么场合????
 
搜索一下 离线dfwbbs就知道了。
 
阻塞模式就是要等结果,也就是同步
比如recv函数,要等要收到数据才能往下面运行

而非阻塞模式通过消息来告诉你数据到了,不用一直在那里死等
 
我想你已经了解了非阻塞方式和阻塞方式的基本含义。刚好我正在做一个程序。使用的是socket与unix通讯。我想解释一下我们的要求和选择,希望对你有所帮助。使用socket流方式,通过一个前置查询机将客户的请求以报文的格式发送,然后等待回复。采用的就是就是阻塞的方式,这样发送完毕后需要等待回复程序才能国等该系统的进一步处理。如果需要在等待回复的空闲时间内作一个其他的事情,那么一般选择非阻塞方式。其实,真正的理解这两种方式,需要了解通讯的一些原理与应用。最好,察看一些典型的应用案例。
 
使用这两个组件,弊:在非组塞方式下容易丢包,不稳定,利:处理与多个客户端的效率高
在组塞方式下要比非组塞要稳定。至于用在什么场合关键看需求。如果一台服务器对一台客户端的文件传输,使用组塞方式要好一些。如果登陆验证时间需要较长,也最好使用组塞方式。如果是简单的文本信息传输最好使用非组塞方式。
 
为什么发了一文章(青年报上的),账号封了就不开放?妈的!有这么严重吗?
 
后退
顶部