Hello friends,

Since I'm not too familiar with win programming may I ask a dumb question. I made a small app on D7 using TclientSocket which connects to some embedded server and request some data.

Now I use string based functions like

sock.Socket.SendText('text to send');

but I send pure binary commands so I defined some constants like

const
MOD_HEADER = #$45#$2A#$3F#$92#$3D#$29#$01#$7F#$2C#$3A#$42#$20#$1C#$7B#$D2#$F2;

and send

sock.Socket.SendText(MOD_HEADER);

Now the question is this is ok to do that way? I used this method to avoid filling by hand byte-by-byte some array since there are couple.

But I saw also

sock.Socket.SendStream(astream)

What are your experts oppinions ?

Thanks in advance,


sock.Socket.SendText('text to send');

but I send pure binary commands so I defined some constants like

const
MOD_HEADER = #$45#$2A#$3F#$92#$3D#$29#$01#$7F#$2C#$3A#$42#$20#$1C#$7B#$D2#$F2;

and send

sock.Socket.SendText(MOD_HEADER);

Now the question is this is ok to do that way?

I tryed in turbo pascal 7 and worked.

Program solution;
Const mode = #$45#$2A;
Begin
    Write(mode);
End.
{
and the result is:E*
so the results are characters  to pass into a parameter and that what you want or not?
}

I think you don't understand well, mine works OK too

sock.Socket.SendText(MY_CONST_TEXT)

but I wonder there is a difference if I use strings for storing non ASCII chars and use them as buffers since I saw Delphi 7 allows that and works very well.

I mean Socket has several options to send data :

sock.socket.sendtext()
sock.socket.sendbuf()
sock.socket.sendstream()

Is there any difrence by socket point of view wich method I use?

Thanks very much for any clarify,
Waiting for answers

SendBuf method (TCustomWinSocket)
Writes Count bytes to the socket connection from the Buf parameter.

Delphi syntax:

function SendBuf(var Buf; Count: Integer): Integer;

C++ syntax:

int __fastcall SendBuf(void *Buf, int Count);

Description
Use SendBuf to write to the socket connection. Call this method from the OnSocketEvent event handler of a Windows socket object or in the OnWrite or OnClientWrite event handler of a socket component. Alternately, Use SendBuf to write when a connection is first formed when the socket does not expect notification that the socket on the other end of the connection is expecting to read.For non-blocking sockets, the data is sent to the WinSock DLL which has it's own internal buffers. If the WinSock can accept additional data, SendBuf returns immediately with the number of bytes queued. If the WinSock internal buffer space is not able to accept the buffer being sent, SendBuf returns -1 and no data is queued at all. In this case, wait a bit for the WinSock to have a chance to send out already-queued data; then try again.For blocking sockets, SendBuf returns the number of bytes actually written.If an error occurs while writing to the connection, SendBuf terminates the connection and raises an ESocketError exception.

-------

SendText method (TCustomWinSocket)
Writes the string S to the socket connection.

Delphi syntax:

function SendText(const S: string): Integer;

C++ syntax:

int __fastcall SendText(const AnsiString S);

Description
Use SendText to write a string to the socket connection. The writing may occur in the OnSocketEvent event handler of a Windows socket object or in the OnWrite or OnClientWrite event handler of a socket component. Alternately, SendText may write from a socket that is expected to write to the connection without a notification to signal the connection’s readiness to read. If an error occurs while writing to the connection, SendText terminates the connection and raises an ESocketError exception.
SendText returns the number of bytes sent. Note that this may be less than the length of the string S if the socket is nonblocking.
---------
SendStream method (TCustomWinSocket)
Writes all the information that can be read from the AStream parameter to the socket connection.

Delphi syntax:

function SendStream(AStream: TStream): Boolean;

C++ syntax:

bool __fastcall SendStream(Classes::TStream* AStream);

Description
Use SendStream to write to the socket connection. The writing may occur in the OnSocketEvent event handler of a Windows socket object or in the OnWrite or OnClientWrite event handler of a socket component. Alternately, SendStream may write from a socket that is expected to write to the connection without a notification to signal the connection’s readiness to read. SendStream reads information from the stream indicated by AStream and writes it to the socket connection. The value returned by SendStream indicates whether any information was successfully written to the connection.
Note:	The Stream passed as a parameter to SendStream becomes “owned” by the windows socket object.  The Windows socket object frees the stream when it is finished with it.  Do not attempt to free the stream after it has been passed as a parameter.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.