C
cgfhz
Unregistered / Unconfirmed
GUEST, unregistred user!
我这里先把源码贴上:
Server端源码://阻塞式
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
Const
DATA_BUFFERSIZE = 256;
giWaitTime = 15000;
type
TServerThread = class(TServerClientThread)
public
procedure ClientExecute; override;
end;
TBUff = array[0.. DATA_BUFFERSIZE - 1] of Char;
TForm1 = class(TForm)
Memo1: TMemo;
ServerSocket1: TServerSocket;
procedure ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormActivate(Sender: TObject);
procedure ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
gMulReadExcWriteA,
gMulReadExcWriteB,
gMulReadExcWriteC,
gMulReadExcWriteD : TMultiReadExclusiveWriteSynchronizer;
function WriteMemo(s: string): integer;
implementation
{$R *.dfm}
{ TServerThread }
procedure TServerThread.ClientExecute;
var
Stream: TWinSocketStream;
sRevText, sSendText: string;
iActualRevLen: integer;
Fbuffer : TBuff;
procedure FailProcess;
begin
if Assigned(ClientSocket) then
begin
if ClientSocket.Connected then
ClientSocket.Close;
end;
Terminate;
end;
begin
inherited FreeOnTerminate := True;
while (not Terminated) and ClientSocket.Connected do
begin
Stream := TWinSocketStream.Create(ClientSocket,giWaitTime);
try
try
if Stream.WaitForData(giWaitTime) then
begin
FillChar(FBuffer, DATA_BUFFERSIZE, ' ');
gMulReadExcWriteA.BeginRead;
iActualRevLen := Stream.Read(Fbuffer, DATA_BUFFERSIZE);
gMulReadExcWriteA.EndRead;
sRevText := Trim(FBuffer);
WriteMemo(sRevText);
sSendText := '收到消息';
ClientSocket.SendText(sSendText);
FailProcess;
end
else
FailProcess;
except
on E: exception do
begin
WriteMemo('监听过程中出现异常:' + E.Message);
Exit;
end;
end;
finally
if not Terminated then
FailProcess;
Stream.Free;
end;
end;
end;
procedure TForm1.ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread := TServerThread.Create(FALSE, ClientSocket);
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('['+ Socket.RemoteAddress + ']' + '----请求连接成功!');
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('['+ Socket.RemoteAddress + ']' + '----断开连接成功!');
end;
function WriteMemo(s: string): integer;
begin
gMulReadExcWriteB.BeginWrite;
Form1.Memo1.Lines.Add('['+s+']');
gMulReadExcWriteB.EndWrite;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
gMulReadExcWriteA := TMultiReadExclusiveWriteSynchronizer.Create;
gMulReadExcWriteB := TMultiReadExclusiveWriteSynchronizer.Create;
//gMulReadExcWriteC := TMultiReadExclusiveWriteSynchronizer.Create;
//gMulReadExcWriteD := TMultiReadExclusiveWriteSynchronizer.Create;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
gMulReadExcWriteA.Free;
gMulReadExcWriteB.Free;
//gMulReadExcWriteC.Free;
//gMulReadExcWriteD.Free;
if ServerSocket1.Active then
ServerSocket1.Active := False;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
ServerSocket1.Port := 7000;
ServerSocket1.Active := True;
end;
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
WriteMemo('['+ Socket.RemoteAddress + ']' + '----请求连接成功!');
end;
end.
Client端源码://非阻塞式
unit ClientUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, Buttons;
type
TForm1 = class(TForm)
Memo1: TMemo;
ClientSocket1: TClientSocket;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
bitSend: TBitBtn;
Edit3: TEdit;
procedure bitSendClick(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.bitSendClick(Sender: TObject);
begin
ClientSocket1.Address := Trim(Edit1.Text);
ClientSocket1.Port := StrtoInt(Edit2.Text);
if not ClientSocket1.Active then
ClientSocket1.Active := True;
ClientSocket1.Socket.SendText(Trim(Edit3.Text));
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(FormatDatetime('YYYYMMDD',date)+':'+Socket.ReceiveText);
ClientSocket1.Active := False;
end;
end.
Server端源码://阻塞式
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
Const
DATA_BUFFERSIZE = 256;
giWaitTime = 15000;
type
TServerThread = class(TServerClientThread)
public
procedure ClientExecute; override;
end;
TBUff = array[0.. DATA_BUFFERSIZE - 1] of Char;
TForm1 = class(TForm)
Memo1: TMemo;
ServerSocket1: TServerSocket;
procedure ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormActivate(Sender: TObject);
procedure ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
gMulReadExcWriteA,
gMulReadExcWriteB,
gMulReadExcWriteC,
gMulReadExcWriteD : TMultiReadExclusiveWriteSynchronizer;
function WriteMemo(s: string): integer;
implementation
{$R *.dfm}
{ TServerThread }
procedure TServerThread.ClientExecute;
var
Stream: TWinSocketStream;
sRevText, sSendText: string;
iActualRevLen: integer;
Fbuffer : TBuff;
procedure FailProcess;
begin
if Assigned(ClientSocket) then
begin
if ClientSocket.Connected then
ClientSocket.Close;
end;
Terminate;
end;
begin
inherited FreeOnTerminate := True;
while (not Terminated) and ClientSocket.Connected do
begin
Stream := TWinSocketStream.Create(ClientSocket,giWaitTime);
try
try
if Stream.WaitForData(giWaitTime) then
begin
FillChar(FBuffer, DATA_BUFFERSIZE, ' ');
gMulReadExcWriteA.BeginRead;
iActualRevLen := Stream.Read(Fbuffer, DATA_BUFFERSIZE);
gMulReadExcWriteA.EndRead;
sRevText := Trim(FBuffer);
WriteMemo(sRevText);
sSendText := '收到消息';
ClientSocket.SendText(sSendText);
FailProcess;
end
else
FailProcess;
except
on E: exception do
begin
WriteMemo('监听过程中出现异常:' + E.Message);
Exit;
end;
end;
finally
if not Terminated then
FailProcess;
Stream.Free;
end;
end;
end;
procedure TForm1.ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread := TServerThread.Create(FALSE, ClientSocket);
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('['+ Socket.RemoteAddress + ']' + '----请求连接成功!');
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('['+ Socket.RemoteAddress + ']' + '----断开连接成功!');
end;
function WriteMemo(s: string): integer;
begin
gMulReadExcWriteB.BeginWrite;
Form1.Memo1.Lines.Add('['+s+']');
gMulReadExcWriteB.EndWrite;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
gMulReadExcWriteA := TMultiReadExclusiveWriteSynchronizer.Create;
gMulReadExcWriteB := TMultiReadExclusiveWriteSynchronizer.Create;
//gMulReadExcWriteC := TMultiReadExclusiveWriteSynchronizer.Create;
//gMulReadExcWriteD := TMultiReadExclusiveWriteSynchronizer.Create;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
gMulReadExcWriteA.Free;
gMulReadExcWriteB.Free;
//gMulReadExcWriteC.Free;
//gMulReadExcWriteD.Free;
if ServerSocket1.Active then
ServerSocket1.Active := False;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
ServerSocket1.Port := 7000;
ServerSocket1.Active := True;
end;
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
WriteMemo('['+ Socket.RemoteAddress + ']' + '----请求连接成功!');
end;
end.
Client端源码://非阻塞式
unit ClientUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, Buttons;
type
TForm1 = class(TForm)
Memo1: TMemo;
ClientSocket1: TClientSocket;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
bitSend: TBitBtn;
Edit3: TEdit;
procedure bitSendClick(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.bitSendClick(Sender: TObject);
begin
ClientSocket1.Address := Trim(Edit1.Text);
ClientSocket1.Port := StrtoInt(Edit2.Text);
if not ClientSocket1.Active then
ClientSocket1.Active := True;
ClientSocket1.Socket.SendText(Trim(Edit3.Text));
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(FormatDatetime('YYYYMMDD',date)+':'+Socket.ReceiveText);
ClientSocket1.Active := False;
end;
end.