We help IT Professionals succeed at work.

Sockets

franksymons
franksymons asked
on
Medium Priority
1,009 Views
Last Modified: 2012-08-14
I need more information on how to use the sockets provided with Delphi 3 C/S. Does anybody know a good website with info?
Thanks
Comment
Watch Question

Commented:
What info do you need?

I prefer the use of F.Piette's components. They are free (cardware), great support and don't need any dll, activeX etc.

http://www.rtfm.be/fpiette/indexuk.htm

Zif.

Author

Commented:
Adjusted points to 75
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi again,

I've had some time to look into this now and I want to thank you for the nice example, it's certainly worth an A grade. The ultimate goal of the program would be to create some kind of 'live update' program like Norton Antivirus has. I do have some additional questions on your example.

First, the program doesn't seem to work with very small files (170b). Which is weird, when running the program without using the debugger it shows this behaviour. When debugging line by line it doesn't.

Second, you talk about improving error handling, what do you suggest?

I'd like, and tried to add some way of transferring the file name right before the stream itself using sendtext, this works but I'm not sure whether this is the best way of doing this. In addition to the filename I'd like to be able to tell the server what he should do with the file... So I will probably have to send a second file with instructions or send each instruction as text. Do you have any suggestions on that?

Do you know of any good delphi books on this subject (or chapters in a book) or some webpages, programming examples. I've been looking on the Net for some time now, but could't find anything...

Thanks again for your example and hope to hear from you again soon.

Best regards from Belgium,
Frank.

Commented:
Zoals ik reeds vertelde Frank, kijk naar F.Piette's webpaginas (url : comment above). Daar zul je verschillende links en voorbeelden vinden. Zif.
Hi Frank.

You do observe things very well, and I must say, that this will maybe bring you a bright future. These exact problems is beeing discussed.

The very best book for understanding these matters are this:
"Distributed Systems - Concepts and Design"
2nd edition
by George Coulouris, Jean Dollimore and Tim Kindberg
Publisher: Addison-Wesley

1: Asynchonous tranfers can result in errors as the problems you have encountered. This can be solved by replacing the procedures followed by these answers:

2. The Norton Antivirus Update works apparently this way:
The client connects to a specific site
The client compares versioninfo (probably a version date+time)
The client requests the update setup system and might execute this when received successfully.

3. Typically you build a header-structure with information regarding requests, filenames, attributes, sizes etc. You will ofcourse have to expect that header on the server.
If you are sending complex structures, you should read more about Marshalling (Sending/receiving an object as a string or binary)

4. Error handling - You should provide errorhandling by using checksum errorchecking and f.ex. password checking/encryption to ensure dataintegrity. You will also have to check the time between the states to provide a timeout feature. All this is important, because of the unreliability of network systems.

I have been tought all this at Aarhus Business College in Denmark during my education as Computer Engineer. We primaly used the book I described above and can be ordered by most bookstores.

From question #1:

In the server project:

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
const
  MaxChunkSize = 8192; { copy in 8K chunks }
var
  BytesReceived: Longint;
  CopyBuffer: Array[0..MaxChunkSize] of byte; { buffer for copying }
  ChunkSize: Integer;
begin
  If FSize=0 then
  begin
    If Socket.ReceiveLength>=SizeOf(FSize) then
    begin
      Socket.ReceiveBuf(FSize,SizeOf(FSize));
      Stream.SetSize(FSize);
      Socket.SendText('Send next packet');
    End;
  End else
  begin
    While Socket.ReceiveLength>0 do
    Begin
      ChunkSize:= Socket.ReceiveLength;
      If ChunkSize > MaxChunkSize then ChunkSize:= MaxChunkSize;
      BytesReceived:= Socket.ReceiveBuf(CopyBuffer,ChunkSize);
      Stream.Write(CopyBuffer, BytesReceived); { ...write chunk }
      Dec(FSize,BytesReceived);
    End;
    If FSize=0 then
    Begin
      Socket.SendText('File received!');
      If SaveDialog1.Execute then
      begin
        If FileExists(SaveDialog1.Filename) then
          DeleteFile(SaveDialog1.Filename);
        Stream.SaveToFile(SaveDialog1.Filename);
        Stream.SetSize(0);
        FSize:= 0;
      End;
    End else
      Socket.SendText('Send next packet');
  End;
end;


In the client project:

procedure TForm1.Button2Click(Sender: TObject);
var
  Size: Integer;
begin
  if OpenDialog1.Execute Then
  begin
    Stream.LoadFromFile(OpenDialog1.Filename);
    Size:= Stream.Size;
    ClientSocket1.Socket.SendBuf(Size,SizeOf(Size));
    Stream.Position:= 0;
//    ClientSocket1.Socket.SendStream(Stream);
  End;
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);

  procedure SendNext;
  const
    MaxChunkSize = 8192;
  var
    CopyBuffer: Array[0..MaxChunkSize] of Byte;
    CopySize: Integer;
  begin
    CopySize:= Stream.Size-Stream.Position;
    If CopySize>MaxChunkSize then CopySize:= MaxChunkSize;
    Stream.WriteBuffer(CopyBuffer,CopySize);
    Socket.SendBuf(CopyBuffer,CopySize);
  End;

var
  S: String;
begin
  S:= Socket.ReceiveText;
  If S='Send next packet' then SendNext else
  begin
    Socket.Close;
    ShowMessage('Client: '+S);
  End;
end;

regards,
Williams

Author

Commented:
Thanks for your answers and comments, you've been very helpfull. And thanks to ziff for the comments I've downloaded the suite and I'm checking it out.
You are welcome, we could use some more network dudes here at EE.

Regards,
Williams
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.