?
Solved

View picture

Posted on 2011-05-08
4
Medium Priority
?
210 Views
Last Modified: 2012-05-11
Hi, little bit if a complex question and hope the answer is not :)
Im using basic TCP sockets,not indy.

i want to send a command from my laptopto send hello.jpg on my main pc. and send it back to my laptop. dont include sockets. or ips.. i already have them programed in.

inside my pictures


EXAMPLE CODE  BELOW THAT SHOWS MY FORMAT. PLEASE NO NEED TO FIX OE CHANGE.


This is a sample of code that sends a message that i am working with, please use the same fomat.

the serverside that accepts and responds to the request:

 GETIT_MYMSG:
        begin
        showmessage(fdata) ;
       
        end;


THE clientside code that sends the text to the server is

begin
SEND(GETIT_MYMSG, flatedit22.text);
end;




The serverside code that the server sends back after the message is clicked is:

SEND(GETIT_MYMSG,'Message Successfully Displayed NEW',Socket);


the clientside code that reads the above message and formats it

GETIT_MYMSG:                     logmemo.text:=(fdata);


So, i want to send a pic from pc- pc . cheers





//Clientside// that sends the text that i type into the editbox is
begin
SEND(GETIT_MYMSG, flatedit22.text);
end;

Open in new window

//Clientside //client that accepts the response from the server after the other pc clicked the messages
//

GETIT_MYMSG:                     logmemo.text:=(fdata);

Open in new window

//The server that shows the messages and sends response back to the client after the user has clicked the messages
GETIT_MYMSG:
        begin
        showmessage(fdata) ;
        SEND(GETIT_MYMSG,'Message Successfully Displayed NEW',Socket);

        end;

Open in new window

0
Comment
Question by:boycoder
  • 2
4 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 35716830
please send more of your code : the part that reads/writes TCP/IP socket, as we don't know which types are your data : GETIT_MYMSG (integer constant ?) and fdata (probably string, but how do you write/read it from TCP/IP stream?)

better yet, even if you specifically asked that we don't change your working code, remember that TCP/IP is not a real TRANSPORT-level protocol, in the ISO definition : it does not have a frame definition per-se, which is real handy for anything that is not pure stream. When dialog occurs, as in your application, then it is generally a good idea to add one frame layer on top of it. Once it is in place, you don't have to trouble yourself with bytes in a stream, just send and handle MESSAGES

here is how I would do that, protocol-wise, to allow mixing of anything in your stream :
every frame is composed of a TYPE : integer , LENGTH : integer, VALUE : string or byte array, depending on TYPE constant (typical TLV protocol)
Note that this is the simplest way of doing it, there is no error recovery mechanism, nor encryption or frame consistency checking. They are not necessary usually, so start with that and know that it's easy to add advanced functionalities on top of all this



const 
 TYPE_TEXT = $00000000; // 'class' of types, for all string messages
 TYPE_BINARY = $8000000; // 'class' of types, for all binary messages
// here are all real messages sent/received
 TYPE_MESSAGE = TYPE_TEXT;
 TYPE_RESPONSE = TYPE_TEXT+1; 
 TYPE_FILE_NAME= TYPE_TEXT+2; 
 TYPE_JPG_FILE = TYPE_BINARY;
 TYPE_BMP_FILE = TYPE_BINARY+1;
... // define more messages as needed

and you would read/write messages that way :
procedure SendTextMsg(MsgType:Cardinal;Msg:String;socket:TCustomWinSocket );
Var L:integer;
begin
 if MsgType>=TYPE_BINARY Then Raise Exception.Create('Cannot use SendTextMsg with that message TYPE'); // simple protocol control, this function is for sending text only
 Socket.SendBuf(MsgType,sizeof(Cardinal));
 L:=Length(Msg);
 Socket.SendBuf(L,sizeof(Integer));
 if L>0 Then Socket.SendBuf(Msg[1],L);
end; 


procedure SendBinMsg(MsgType:Cardinal;Msg:Pointer;MsgSize:Integer;socket:TCustomWinSocket );
begin
 if MsgType<TYPE_BINARY Then Raise Exception.Create('Cannot use SendBinMsg with that message TYPE'); // simple protocol control, this function is for sending text only
 Socket.SendBuf(MsgType,sizeof(Cardinal));
 Socket.SendBuf(MsgSize,sizeof(Integer));
 if MsgSize>0 Then Socket.SendBuf(Msg^,MsgSize);
end; 

and in OnRead events of TClient/ServerSocket (if those are the ones you use), or adapt to your own socket components :

procedure SocketRead(Sender: TObject; Socket: TCustomWinSocket);
Var
 MsgType:Cardinal;
 L:Integer;
 S:String;
 Data:Pointer;
begin
 Socket.ReceiveBuf(MsgType,sizeof(Cardinal));
 Socket.ReceiveBuf(L,sizeof(Integer));
 if MsgType<TYPE_BINARY Then
  begin
   SetLength(S,L);
   if L>0 Then Socket.ReceiveBuf(S[1],L);
   TreatTxtMsg(MsgType,L); // call a custom application protocol procedure to treat Text-based messages
  end else
  begin
   if L>0 Then
    begin
     GetMem(Data,L);
     Socket.ReceiveBuf(Data^,L);
    end else Data:=nil;
   TreatBinMsg(MsgType,L,Data); // call a custom application protocol procedure to treat Binary-based messages
   if L>0 Then FreeMem(Data); // FREE the binary message after treatment.. If the treatment needs to hold the data for later use, then it HAS to copy it.
  end;
end;

Open in new window

0
 
LVL 25

Accepted Solution

by:
epasquier earned 2000 total points
ID: 35716892
error , L47 :

TreatTxtMsg(MsgType,S);  // The stringfis used as parameter, not its size (L)

example of Treatment procedures :

procedure TreatTxtMsg( MsgType:Cardinal ; Msg : String );
begin
 Case MsgType of
  TYPE_MESSAGE : SendTxtMsg ( TYPE_RESPONSE , aResponseText ) ;
  TYPE_FILE_NAME : TempFileName := Msg ; // Save File Name for the incoming BIN message with file data
 end;
end;

procedure TreatBinMsg( MsgType:Cardinal ; MsgSize:Integer; Data : Pointer );
Var
 FileHandle:Integer;
begin
 Case MsgType of
  TYPE_JPG_FILE , TYPE_BMP_FILE: 
   if Data <> nil Then
    begin
// here, probably you would like to change the TempFileName to match some TEMP local directory
     FileHandle:=FileCreate( TempFileName );
     FileWrite(FileHandle, Data^, MsgSize );
     FileClose(FileHandle);
     Image.Picture.LoadFromFile(TempFileName);
     DeleteFile(TempFileName);
    end;
 end;
end;

So, your other PC would send an image with 2 frames, after reading it :
FileHandle:=FileOpen('C:\Image.Jpg', fmOpenRead );
JpgSize:=FileSeek(FileHandle,0,2); // seek position from end to know the size
GetMem(JpgData,JpgSize);
FileSeek(FileHandle,0,0); // return to Pos=0
FileRead(FileHandle,JpgData^,JpgSize);
FileClose(FileHandle);
SendTextMsg( TYPE_FILE_NAME , 'C:\Image.Jpg'); // In fact that is not strictly necessary, but useful for the other part to identify what picture is sent.
SendBinMsg( TYPE_FILE_JPG , JpgData , JpgSize );
FreeMem(JpgData);

Open in new window

0
 
LVL 8

Expert Comment

by:BdLm
ID: 35719396
what version of INDY , what version of DELPHI ???
0
 

Author Closing Comment

by:boycoder
ID: 35766112
A
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question