Hey Wilfried,
Thanks for your reply...
At 10:01 7/19/2005, Wilfried Mestdagh wrote:
>Hello W.,
>
>WD> Now having problems with Winsock functions and
>WD> associated pointers. For some reason, I can't
>WD> seem to "recv" data uncorrupted. I've
>WD> tried it using a string buffer and as a PChar.
>
>A Delphi string as whell as a pointer (like PChar) may contains as many
>NULL as you want, however you have to take in account that many of PChar
>functions will not work correcly if NULL's are in it. In case of a
>pointer you need a 'length' variable.
I'll research that.
>In case of communication programs
>I mostly work with a kind of TBuffer class thta does the deal.
OK, I'll look into that too.
>To give an example, if you use Pos() on a PChar, the function will stop
>at the first #0. In a string you can use it without problem even if
>NULL's are there.
>
>WD> Please tell me what I am doing wrong!!??
>
>Difficult to say, it is a lot of code. Can you shrink it to a few
>lines to show where the problem is ?
Sure. Most of these lines are debugging lines to show the
output of variables. There are only a few lines
that "do" anything.
It appears that the problem occurs somewhere between the
"Cnt := Winsock.recv..." line and the assignment of
the captured data to the "data" variable. In the string
version: "data := uint8Ptr(PChar(BigBuf));" In the PChar
version: I think it has to do with the incrementing
of the pointer "Inc(pcData, Cnt);"
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Function BlkgTCP.GetBlock(size: uint16;
data: uint8Ptr):
uint32;
// Override; // Virtual;
Var
// Number of bytes to read:
BytesRead: uint32;
Cnt: Integer;
Err: Integer;
BigBuf: String;
LilBuf: String;
// C++: {
Begin
LilBuf := '';
BigBuf := '';
// Number of bytes read:
// C++: uint32 BytesRead = (uint32)size;
BytesRead := uint32(size);
// Read data from the socket
// C++: while (BytesRead > 0)
While (BytesRead > 0) Do
// C++: {
Begin
LilBuf := '';
SetLength(LilBuf, 4096);
Cnt := Winsock.recv(MySocket, LilBuf[1], BytesRead, 0);
If (Cnt <> SOCKET_ERROR)
Then SetLength(LilBuf, Cnt)
Else SetLength(LilBuf, 0);
BigBuf := BigBuf + LeftStr(LilBuf, Cnt);
// C++: if (!Cnt)
If (Cnt = 0) Then
Begin
// C++: return SOCK_CLOSED;
Result := SOCK_CLOSED;
Exit;
End; // If (Cnt = 0)...
// C++: if (Cnt == SOCKET_ERROR)
// SOCKET_ERROR is -1
If (Cnt = SOCKET_ERROR) Then
// C++: {
Begin
// C++: int err = WSAGetLastError();
err := WSAGetLastError();
// C++: WSASetLastError(0);
WSASetLastError(0);
// C++: return err;
Result := err;
Exit;
// C++: }
End; // If (Cnt = SOCKET_ERROR)
// C++: StatxBytes += Cnt;
StatxBytes := StatxBytes + Cardinal(Cnt);
// C++: BytesRead -= Cnt;
BytesRead := BytesRead - Cardinal(Cnt);
BigBuf := BigBuf + #0;
data := uint8Ptr(PChar(BigBuf));
// C++: }
End; // While (BytesRead > 0)
// C++: return COMM_SUCCESS;
Result := COMM_SUCCESS;
Exit;
// C++: }
End; // Function BlkgTCP.GetBlock(
// End String version
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PChar Version:
Function BlkgTCP.GetBlock(size: uint16;
data: uint8Ptr):
uint32;
// Override; // Virtual;
Var
//number of bytes read
BytesRead: uint32;
pcData: PChar;
Cnt: Integer;
Err: Integer;
pcDataStr: String;
SomeInt: Integer;
// C++: {
Begin
// Number of bytes read:
// C++: uint32 BytesRead = (uint32)size;
BytesRead := uint32(size);
// C++: char* pcData = (char * )data;
pcData := PChar(data);
// Read data from the socket
// C++: while (BytesRead > 0)
While (BytesRead > 0) Do
// C++: {
Begin
If pcData <> NIL
Then SomeInt := Ord(pcData^)
Else SomeInt := 999;
// function recv(s: TSocket; var Buf; len, flags: Integer):
Cnt := Winsock.recv(MySocket, pcData, BytesRead, 0);
pcDataStr := PChar(pcData);
If pcData <> NIL
Then SomeInt := Ord(pcData^)
Else SomeInt := 999;
// C++: if (!Cnt)
If (Cnt = 0) Then
Begin
// C++: return SOCK_CLOSED;
Result := SOCK_CLOSED;
Exit;
End; // If (Cnt = 0)...
// C++: if (Cnt == SOCKET_ERROR)
// SOCKET_ERROR is -1
If (Cnt = SOCKET_ERROR) Then
// C++: {
Begin
// C++: int err = WSAGetLastError();
err := WSAGetLastError();
// C++: WSASetLastError(0);
WSASetLastError(0);
// C++: return err;
Result := err;
Exit;
// C++: }
End; // If (Cnt = SOCKET_ERROR)
// C++: StatxBytes += Cnt;
StatxBytes := StatxBytes + Cardinal(Cnt);
// C++: BytesRead -= Cnt;
BytesRead := BytesRead - Cardinal(Cnt);
// C++: pcData += Cnt;
Inc(pcData, Cnt);
pcDataStr := PChar(pcData);
If pcData <> NIL
Then SomeInt := Ord(pcData^)
Else SomeInt := 999;
// C++: }
End; // While (BytesRead > 0)
// C++: return COMM_SUCCESS;
Result := COMM_SUCCESS;
Exit;
// C++: }
End; // Function BlkgTCP.GetBlock(
// End PChar version
Start Here to Find It Fast! -> http://www.US-Webmasters.com/best-start-page/
$8.77 Domain Names -> http://domains.us-webmasters.com/
__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk