Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

TCP/IP-component problem

I'm trying to use the GetData method in the TCP-component on the Internet tab in Delphi 3, but it doesn't work. ´Here is a sample of my code:

procedure TForm1.Button1Click(Sender: TObject);
var
data:string;
begin
tcp2.GetData(data) <--- that doesn't work!
end;

tcp2 is a new instance of the tcp component, the firt instance of it I used for the listen procedure. It said in the Delphi help that I should create a new instance of it.

I would appreciate if someone could answer this question, but I nearly don't have any points, so im setting it to very low.
0
sageryd
Asked:
sageryd
  • 11
  • 6
  • 4
  • +1
1 Solution
 
bruintjeCommented:
H sageryd,

Well maybe it would do better if you declared your 'data' variable of the type Variant.

Can't test it right now, my internet comps are not working at all :O)........

Hope this helps
Bruintje.
0
 
williams2Commented:
brUINTje is right

But I REALLY would recommend you NOT TO USE THIS COMPONENT because it
1) Sucks because of it's limitations
2) Has errors which you won't be able to solve properly !!!

You should use the TClientSocket and TServerSocket instead for a beginning. When you learn how they work you should use the components composed by Francois Piette, they are more flexible.

But anyway, if you want a simple example on how to use them, I think I could provide you an easy one.

Regards,
Williams
0
 
dejohn99Commented:
AMEN to williams2.

I have never heard of anyone actually getting the components supplied with D3 to work, and they have no support.  ICS in contrast is supported by the author and other users on a continuing basis on a free mailing list.  It is distributed in Delphi source for D1 through 4 and BCB 1 and 2.  It's cost is one post card for Francois, and a nice stamp for his kids.  Check out http://www.rtfm.be/fpiette/icsuk.htm
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sagerydAuthor Commented:
bruintje:
I have tried that too, but it still doesn't work.

williams2:
Yes! Please give me your example on how to use those, I have tried to use them, but I thought that the TCP component might be easier, so I started using that one instead, but I'll give it a try with those others again then.

dejohn99:
Thanx!
0
 
sagerydAuthor Commented:
bruintje:
I have tried that too, but it still doesn't work.

williams2:
Yes! Please give me your example on how to use those, I have tried to use them, but I thought that the TCP component might be easier, so I started using that one instead, but I'll give it a try with those others again then.

dejohn99:
Thanx!
0
 
sagerydAuthor Commented:
Edited text of question
0
 
bruintjeCommented:
Sageryd,

Think the socket components will be easier to use, and for the components Williams is talking about you can find them at:

http://www.rtfm.be/fpiette/indexuk.htm (the author)

or at

http://www.delphideli.com/

The whole package is full of examples for all sorts of internet communications so I think this will help you on your way.

:O)
Bruintje.

0
 
sagerydAuthor Commented:
My real problem is that I want to connect to another computer (which I have already figured out how to do), and then send a string or a file to the other computer. I already downloaded the WSocket-component, and tried it, but I still don't know how to manage it.....

But thanx anyway bruintje!, I'm checking the site out.....
0
 
sagerydAuthor Commented:
My real problem is that I want to connect to another computer (which I have already figured out how to do), and then send a string or a file to the other computer. I already downloaded the WSocket-component, and tried it, but I still don't know how to manage it.....

But thanx anyway bruintje!, I'm checking the site out.....
0
 
sagerydAuthor Commented:
Williams2:

Why haven't you answered yet? Any problems? Please post your example A.S.A.P.   ;)

//Sageryd
0
 
williams2Commented:
I'm very sorry, but I have been away all week, because I just got myself a new job ..600 KM away ..but I'll post an example tonight, I promise !

Regards,
Williams
0
 
williams2Commented:
The following example needs you to have Francois Piettes TWSocket
component installed. I also made some efforts in showing how the differences are from the original Delphi C/S components.

The example shows how to copy the screen area to a buffer (Stream) and then send it to a client on request. To make this work, you will have to open to new delphi projects and add a FormCreate event procedure to them both. Then go save the projects in to different folders, and replace the code as server and client parts into unit1.

You can change the connection IP in the client part to make it work on a remote machine.

------------------------- Server part -------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ScktComp, ExtCtrls, StdCtrls, ExtDlgs, WSocket, Winsock;

Const
  //You may set this to False if you would like the whole image!
  STRETCH = True;
  STRETCH_WIDTH = 200;
  STRETCH_HEIGHT = 150;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
//    ServerSocket1: TServerSocket;
    SrvSocket,
    CliSocket: TWSocket;
//    procedure ServerSocket1ClientRead(Sender: TObject;
//      Socket: TCustomWinSocket);
    procedure SrvSocketSessionAvailable(Sender: TObject; Error: Word);
    procedure SocketDataAvailable(Sender: TObject; Error: Word);
  private
    { Private declarations }
    Stream: TMemoryStream;
//    procedure SendNextPart(Socket: TCustomWinSocket);
    procedure SendNextPart(Socket: TWSocket);
  public
    { Public declarations }
    Timer: TTimer;
    procedure OnTime(Sender: TObject);
//    procedure SendScreen(Socket: TCustomWinSocket);
    procedure SendScreen(Socket: TWSocket);
  protected
    Bitmap: TBitmap;
  end;

var
  Form1: TForm1;


implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Bitmap:= TBitmap.Create;
  if STRETCH then
  begin
    Bitmap.Width:= STRETCH_WIDTH;
    Bitmap.height:= STRETCH_HEIGHT;
  End else
  begin
    Bitmap.Width:= Screen.Width;
    Bitmap.Height:= Screen.Height;
  End;
  Timer:= TTimer.Create(Self);
  Timer.Interval:= 1000;
  Timer.OnTimer:= OnTime;
  Timer.Enabled:= True;
{  ServerSocket1:= TServerSocket.Create(Self);
  With ServerSocket1 do
  begin
    Port:= 2500;
    Active:= True;
    ServerType:= stNonBlocking;
    OnClientRead:= ServerSocket1ClientRead;
  End;}
  CliSocket:= TWSocket.Create(self);
  CliSocket.OnDataAvailable:= SocketDataAvailable;
  SrvSocket:= TWSocket.Create(Self);
  With SrvSocket do
  begin
    Addr              := '127.0.0.1';      { Accept local clients }
    Port              := '2500';
    OnSessionAvailable:= SrvSocketSessionAvailable;
    Listen;                                { Start listening for client }
  End;
  Stream:= TMemoryStream.Create;
end;

procedure TForm1.SrvSocketSessionAvailable(Sender: TObject; Error: Word);
var
    NewHSocket : TSocket;
    PeerName   : TSockAddrIn;
    Peer       : String;
begin
    { We need to accept the client connection }
    NewHSocket := SrvSocket.Accept;

    { And then associate this connection with our client socket }
    CliSocket.Dup(NewHSocket);
end;

procedure TForm1.SocketDataAvailable(Sender: TObject; Error: Word);
//procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
//  Socket: TCustomWinSocket);
var
  S: String;
  Socket: TWSocket;
Begin
  Socket:= TWSocket(Sender);
//  S:= Socket.ReceiveText;
  S:= Socket.ReceiveStr;
  If S='Get me the screen' then SendScreen(Socket) else
  If S='Send next' then SendNextPart(Socket) else
  If S='Done' then Socket.Close;
End;

//procedure TForm1.SendScreen(Socket: TCustomWinSocket);
procedure TForm1.SendScreen(Socket: TWSocket);
var
  Size: Integer;
Begin
  Bitmap.SaveToStream(Stream);
  Size:= Stream.Size;
  Stream.Position:= 0;
//  Socket.SendBuf(Size,SizeOf(Size));
  Socket.Send(@Size,SizeOf(Size));
End;

//procedure TForm1.SendNextPart(Socket: TCustomWinSocket);
procedure TForm1.SendNextPart(Socket: TWSocket);
const
  MaxChunkSize = 8192; { copy in 8K chunks }
var
  ChunkSize: Integer;
  CopyBuffer: Array[0..MaxChunkSize] of Byte;
Begin
  Chunksize:= Stream.Size-Stream.Position;
  If ChunkSize>0 then
  begin
    If ChunkSize > MaxChunkSize then ChunkSize:= MaxChunkSize;
    Stream.Read(CopyBuffer,ChunkSize);
    Socket.Send(@CopyBuffer,ChunkSize);
//    Socket.SendBuf(CopyBuffer,ChunkSize);
  End else
    Stream.Clear; //The final
End;

procedure TForm1.OnTime(Sender: TObject);
var
  dwRop: DWord;
  DC: hDC;
begin
  dwRop:= SRCCOPY;
  DC:= GetDC(0);
  If STRETCH then
    StretchBlt(
      Bitmap.Canvas.Handle, // handle to destination device context
      0, // x-coordinate of destination rectangle's upper-left corner
      0, // y-coordinate of destination rectangle's upper-left corner
      Bitmap.Width, // width of destination rectangle
      Bitmap.Height, // height of destination rectangle
      DC, // handle to source device context
      0, // x-coordinate of source rectangle's upper-left corner
      0, // y-coordinate of source rectangle's upper-left corner
      Screen.Width,      // width of source rectangle
      Screen.Height,      // height of source rectangle
      dwRop        // raster operation code (See below)
    )
  else
    BitBlt(
      Bitmap.Canvas.Handle, // handle to destination device context
      0, // x-coordinate of destination rectangle's upper-left corner
      0, // y-coordinate of destination rectangle's upper-left corner
      Bitmap.Width, // width of destination rectangle
      Bitmap.Height, // height of destination rectangle
      DC, // handle to source device context
      0, // x-coordinate of source rectangle's upper-left corner
      0, // y-coordinate of source rectangle's upper-left corner
      dwRop        // raster operation code (See below)
    );
  ReleaseDC(0,DC);
End;

end.

------------------------- Client part -------------------------

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    ProgressBar: TProgressBar;
    Button1: TButton;
    Button2: TButton;
//    ClientSocket1: TClientSocket;
    Image1: TImage;
    Label1: TLabel;
    CliSocket: TWSocket;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
{    procedure ClientSocket1Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure ClientSocket1Disconnect(Sender: TObject;
      Socket: TCustomWinSocket);}
    procedure CliSocketSessionConnected(Sender: TObject; Error: Word);
    procedure CliSocketSessionClosed(Sender: TObject; Error: Word);
    procedure CliSocketDataAvailable(Sender: TObject; Error: Word);
    procedure CliSocketError(Sender: TObject);
  private
    Stream: TMemoryStream;
  public
    Procedure UpdateProgressBar;
  protected
    Bitmap: TBitmap;
    Receiving: Boolean;
    FSize: Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption:= 'Connecting..';
//  ClientSocket1.Open;
  CliSocket.Addr   := 'localhost';        { Server host name              }
  CliSocket.Proto  := 'tcp';              { Protocol we wants to use      }
  CliSocket.Port   := '2500';             { The port we wants to connect  }
  CliSocket.Connect;                      { Let's connect !               }
  { Connect is just a request, it returns immediately. We eventually gets }
  { gets connected later. At that time we will receive the event          }
  { SessionConnected. If you need a timeout, you have to start a TTimer.  }
end;

//procedure TForm1.ClientSocket1Connect(Sender: TObject;
//  Socket: TCustomWinSocket);
procedure TForm1.CliSocketSessionConnected(Sender: TObject; Error: Word);
begin
  Label1.Caption:= 'Connected';
  Receiving:= False;
  FSize:= 0;
end;

//procedure TForm1.ClientSocket1Error(Sender: TObject;
//  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
//  var ErrorCode: Integer);
procedure TForm1.CliSocketError(Sender: TObject);
begin
  ShowMessage('Did you startup the server? I cannot find it!');
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
//  if ClientSocket1.Active then
//    ClientSocket1.Socket.SendText('Get me the screen');
  CliSocket.SendStr('Get me the screen');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Label1:= TLabel.Create(Self);
  Label1.SetBounds(32,72,32,13);
  Label1.Parent:= Self;
  Label1.Caption:= 'Idle..';
  Button1:= TButton.Create(Self);
  Button1.SetBounds(8,8,75,25);
  Button1.Parent:= Self;
  Button1.Caption:= 'Connect';
  Button1.OnClick:= Button1Click;
  Button2:= TButton.Create(Self);
  Button2.SetBounds(8,40,75,25);
  Button2.Parent:= Self;
  Button2.Caption:= 'Get screen';
  Button2.OnClick:= Button2Click;
{  ClientSocket1:= TClientSocket.Create(Self);
  With ClientSocket1 do
  begin
    Active := False;
    ClientType := ctNonBlocking;
    Port := 2500;
    Address := '127.0.0.1';
    OnConnect := ClientSocket1Connect;
    OnDisconnect := ClientSocket1Disconnect;
    OnRead := ClientSocket1Read;
    OnError := ClientSocket1Error;
  end;}
  CliSocket:= TWSocket.Create(Self);
  With CliSocket do
  begin
    OnSessionConnected:= CliSocketSessionConnected;
    OnSessionClosed:= CliSocketSessionClosed;
    OnDataAvailable:= CliSocketDataAvailable;
    OnError:= CliSocketError;
  //  CliSocket.
  End;
  Image1:= TImage.Create(Self);
  Image1.SetBounds(100,0,0,0);
  Image1.AutoSize:= true;
  Image1.Parent:= Self;

  Stream:= TMemoryStream.Create;
  ProgressBar:= TProgressBar.Create(Self);
  ProgressBar.Min:= 0;
  ProgressBar.Align:= alBottom;
  ProgressBar.Parent:= Self;
end;

Procedure TForm1.UpdateprogressBar;
Begin
  ProgressBar.Position:= ProgressBar.Max-FSize;
  progressBar.Update;
End;

//procedure TForm1.ClientSocket1Read(Sender: TObject;
//  Socket: TCustomWinSocket);
procedure TForm1.CliSocketDataAvailable(Sender: TObject; Error: Word);
const
  MaxChunkSize = 8192; { copy in 8K chunks }
var
  BytesReceived: Longint;
  CopyBuffer: Array[0..MaxChunkSize] of Byte; { buffer for copying }
  ChunkSize: Integer;
  TempSize: Integer;
begin
  If FSize=0 then
  begin
//    If Socket.ReceiveLength>=SizeOf(TempSize) then
    If CliSocket.RcvdCount>=SizeOf(TempSize) then
    begin
//      Socket.ReceiveBuf(TempSize,SizeOf(TempSize)); //get the size
      CliSocket.Receive(@TempSize,SizeOf(TempSize));
//      Socket.ReceiveBuf(TempSize,SizeOf(TempSize)); //get the size
      Stream.SetSize(TempSize);
      Stream.Position:= 0;
      ProgressBar.Max:= TempSize;
      FSize:= TempSize; //Threadsafe code!
    End;
  End;
  If (FSize>0) and not(Receiving) then
  begin
    Receiving:= True;
//    While Socket.ReceiveLength>0 do
    While CliSocket.RcvdCount>0 do
    Begin
//      ChunkSize:= Socket.ReceiveLength;
      ChunkSize:= CliSocket.RcvdCount;
      If ChunkSize > MaxChunkSize then ChunkSize:= MaxChunkSize;
//      BytesReceived:= Socket.ReceiveBuf(CopyBuffer,ChunkSize);
      BytesReceived:= CliSocket.Receive(@CopyBuffer,ChunkSize);
      Stream.Write(CopyBuffer, BytesReceived); { ...write chunk }
      Dec(FSize,BytesReceived);
      UpdateProgressBar;
    End;
    //This is called "to Syncronize trasnsmissions"
//    Socket.SendText('Send next');
    CliSocket.SendStr('Send next');
    If FSize=0 then
    begin
//      Socket.SendText('Done');
      CliSocket.SendStr('Done');
      Stream.Position:= 0;
      Image1.Picture.Bitmap.LoadFromStream(Stream);
      FSize:= 0;
      ProgressBar.Position:= 0;
      Stream.Clear;
    End;
    Receiving:= False;
  End;
end;


//procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
//  Socket: TCustomWinSocket);
procedure TForm1.CliSocketSessionClosed(Sender: TObject; Error: Word);
begin
  Label1.Caption:= 'Disconnected';
end;

end.


Regards,
Williams
0
 
sagerydAuthor Commented:
The answer worked excellent, and I probably will be going to use the code in the future when I know more Delphi. I would like an example without so complicated code, I want to be able to place my own components meaning that I don't want the program itself to create for example a new Socket-component here and there. Instead I would like an instruction for which components I should place out and what properties I should set.

If you would like (and it would be easier for me too!), you can mail me a zip-file or something containing the project-files and the units.

Here's my mail-address: jesus84@hotmail.com

Sorry for the trouble!


Regards
Johan
0
 
williams2Commented:
Tell me Johan, what do you expect ? I will help people, but I don't want to bother about laziness. You can just drag some components down to your form and name it like the ones which are created in the creation part and add then their repective properties.

Then ofcourse you can optionally remove all the remarked blocks.

Sorry, the code won't be any easier if that is what you expected. Sending more than 8 kbytes is not that trivial

Regards,
Williams
0
 
sagerydAuthor Commented:
OK, sorry...
0
 
bruintjeCommented:
Well make it a happy end and give Williams the points, because I think he really deserved them.

:O)
0
 
sagerydAuthor Commented:
Yepp, think so too! Williams, post something as an answer so that you can get those points.
0
 
williams2Commented:
Thank you, I'm sorry that I might have sounded a bit rud, but I suppose most of you will deal with the problems as they come, I just don't remember how I made it work the first time, but I sure remember that dealing with the sockets is like making a baby spell your name.

:0)

Cheers,
Williams
0
 
sagerydAuthor Commented:
:)
0
 
williams2Commented:
:-)
0
 
bruintjeCommented:
:O)
0
 
sagerydAuthor Commented:
;)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 11
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now