Newsgroups : Borland : borland.public.delphi.internet.winsock : 2005 Apr : New to Sockets.. Need alot of basic help

www.cryer.info
Managed Newsgroup Archive

New to Sockets.. Need alot of basic help

Subject:New to Sockets.. Need alot of basic help
Posted by:"Tikii" (kara.rus..@go.pdsnet)
Date:Tue, 19 Apr 2005 15:15:38

Hi Everyone.
I have no experience with sockets and I am have to write a program that
takes offline data and  upload them to our server for one of our clients. I
would like to do this using sockets. I have tried to do it wih TSocketSever
and TSocketClient and was able to sends messages and files. The only the
problem with sending the files is that I had to have the filename hardcoded
on the server side to save it.
I since have created a packed record so that I can store the filesize and
the file name and send the filestream to the client
type
   TMyPacket = packed record
       FileSize: Integer;
       FileName: string[50];
       fsSource       : TFileStream ;
   end;

My problem is that I don't know what to do once I have the filestream on the
server side. I have created a second filestream to read into and I don't
know how read the fssource into the new file on the serverside. I have seen
the examples using pointers but I get an access violation as soon as I try
to read the MySendingRecord.fsSource


I also had a couple of real basics questions about sockets.
If I use TSocketsClient/Server can more than one user upload their files at
the same time? If so, Is there any additional programming that I would need
to do?

Also, Should I be using the IdTCP components instead? Do they allow more
than one user to upload their files at the same time? If they do, Is there
any additonal programming/Precautions that I need to consider


I am sorry for such basic questions but I have no experience in socket
programming and I am on a very tight deadline.
Any help or insight would be GREATLY appreciated

My code on the client is

rocedure TfrmServer.Button2Click(Sender: TObject);
var
  FStream,sClientStream: TFileStream;
  Buffer: Array of Byte;
begin
   if ClientSocket1.Active = True then
   begin
      OpenDialog1.Filter := 'All Files (*.*)';
if OpenDialog1.Execute then
      begin
         Edit1.Text := ExtractFileName(OpenDialog1.FileName); // To send as
filename after
         MySendingRecord.FileSize:=GetFileSize(OpenDialog1.Filename);
         MySendingRecord.FileName:=OpenDialog1.Filename;

         try
         MySendingRecord.fsSource:= TFileStream.Create(OpenDialog1.FileName,
fmOpenRead);
         MySendingRecord.fsSource.Seek(0,soFromBeginning);

ClientSocket1.Socket.SendBuf(MySendingRecord,sizeof(MySendingRecord));
         sleep(3000);
         finally
       MySendingRecord.fsSource.Free;
          end;
      end;
   end
     else
     MessageDlg('Error: You are not connected', mtError, [MbOK],0);  //
Error Check above code won't work until the socket is connected
end;


My code on the server is

procedure TfrmServer.ServerSocketClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
     sSourceFilename :String;
    ReceivedLen,IncommingLen: integer;
    fsDestination    : TFileStream;

begin
    Socket.ReceiveBuf(MyReceivingRecord,SizeOf(MyReceivingRecord));
    Socket.ReceiveLength;
    IncommingLen := ;
    sSourceFileName := MyReceivingRecord.FileName;
    Edit1.Text:=sSourceFilename;
    Edit2.Text:=InttoStr(IncommingLen);


fsDestination:=TFileStream.Create(ExtractFileName(ssourceFileName),fmCREATE
or fmOPENWRITE and fmsharedenywrite);

end;

Thanks for the help in advance

Replies:

www.cryer.info
Managed Newsgroup Archive