关于在Delphi中使用VB的WinSock控件的一些问题(200分)

  • 主题发起人 主题发起人 青松
  • 开始时间 开始时间
我现在需要在Delphi中使用VB的WinSock控件,我发现似乎服务器端的控件和客户端的控件
必须成对出现,如果服务器端用的是Delphi的ServerSocket则客户端必须用ClientSocket,
同样,如果客户端用的是VB的WinSock,则服务器端用ServerSocket也会出错.不知道是不是
这样,希望各位大侠指教.
另外,在Winsock的OnDataArrival中,如何调用Winsock控件的GetData方法?我用什么变量
都会报错.谢谢!

 
1、服务器端用SERVERSOCKET,客户端用WINSOCK控件没问题,我已顺利实现通讯;
2、WINSOCKE的GETDATA需要OLEVARIANT类型的参数,它是一个变长BYTE类型的数组,你要自己
转换,也可以把它先保存到BUFFER中,逐一分析执行;
3、在这例子中,还动态调用ActiveX:MSWINSOCK.WINSOCK.1并设置参数,也可实现收发功能;

以下是我以前的一个例子,照原样贴出来,只要能看清是怎么来着就行,若有问题请E:
E_mail:wyh919@21cn.com
Client:Winsockt控件:remotehost:127.0.0.1,remoteport:1024,protocol:sckTCPProtocol
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
OleCtrls, MSWinsockLib_TLB, StdCtrls, Comobj;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Winsock1: TWinsock;
Memo1: TMemo;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Winsock1DataArrival(Sender: TObject
bytesTotal: Integer);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var
ActiveX1 : olevariant;

S : olevariant;
i : Integer;
procedure TForm1.Button1Click(Sender: TObject);
begin
WinSock1.Connect;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
i := i + 1;
WinSock1.SendData(IntToStr(i));
end;

procedure TForm1.Winsock1DataArrival(Sender: TObject
bytesTotal: Integer);
var
j : Integer;
buf : array[0..255] of char;
begin
WinSock1.GetData(S);

For j:= varArrayLowBound(S,1) to varArrayHighBound(S,1) do
begin
//ShowMessage(IntToStr(j));
buf[j] := chr(Byte(S[j]));
end;
ShowMessage(StrPas(buf));
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
try
ActiveX1 := CreateOleObject('MSWINSOCK.WINSOCK.1');
except
exit;
end;
ActiveX1.Protocol := 0
//sckTCPProtocol;
ActiveX1.RemotePort := 1024;
ActiveX1.RemoteHost := '127.0.0.1';
ActiveX1.Connect;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
ActiveX1.SendData('abcde');
end;

end.
==============================================
Server:ServerSocket:port 1024

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
ServerSocket1: TServerSocket;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var
count : Integer;

procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Active := True;
end;

procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
ShowMessage('Client Connect ok');
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
Str : String;
begin
SetLength(Str,Socket.ReceiveLength);
Socket.ReceiveBuf(Str[1],Socket.ReceiveLength);
Memo1.Lines.Add(Str);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Inc(count);
ServerSocket1.Socket.Connections[0].SendText('mes:'+InttoStr(Count));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Count := 0;
end;

end.

 
接受答案了.
 
后退
顶部