WinSocket问题!CSDN上等了半天没人回答,郁闷中... ( 积分: 100 )

  • 主题发起人 主题发起人 cgfhz
  • 开始时间 开始时间
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.
 
你想问什么啊,我想没有人喜欢这样看代码找问题的!
 
问题是每次Client发给Server时,第一次只能连上Server端,但Server端执行到“if Stream.WaitForData(giWaitTime) then”是跳出了,执行失败!第二次发送就正常的接收信息,然后返回给客户端了!哪位高手知道问题出在哪里?
 
刚才忘记写上问题了,不好意思!
 
没人能回答?
 
问题解决了,把客户端中发送信息的写到Clientsocket的事件中就OK了;
procedure TFrm_Main.ClientSocket1Write(Sender: TObject;
Socket: TCustomWinSocket);
var
st : string;
begin
if trim(MemoSend.Text) = '' then
begin
ShowMessage('没有生成交易参数!');
Exit;
end;
st := MemoSend.Text;
ClientSocket1.Socket.SendText(st);
end;
procedure TFrm_Main.bitSendClick(Sender: TObject);
var st: string;
begin
ClientSocket1.Active := False;
ClientSocket1.Active := True;
end;
 
过来曾分,我分数不多,解决问题了,就多少给点吧老兄!
 
先Connect,然后再Write这个不容易出现问题;
只有通的情况下才能发送信息;
 
正好在写类似的东西,不过是纯socket,用API搞的。
 
后退
顶部