I'm working on a fairly simple Client/Server application and have some trouble receiving a TStringStream from a client using recv provided by winsock API.
I keep getting this error: 'access violation at 0x00000000: read of address 0x00000000'.
The client only copies text into a TStringStream, gets it's length and sends it to the server. The server then receives the Stream and outputs it's text.
Below some abstract code extracts.
{ the server's part }
inBuf := TStringStream.Create;
{ MAKE THIS SOCKET A PASSIVE ONE }
listen(serversock, LISTENQ);
{ ACCEPT CONNECTION ON serversock FROM cliaddr -> CONNECTED SOCKET = connfd }
connfd := accept(serversock, @cliaddr, @len);
recv(connfd, inLen, sizeof(inLen), 0);
//up to here everything is fine with the strem:
//Size = InLen, Position = 0, all bytes are '0'
rec := recv(connfd, inBuf, inLen, 0);
//rec = inLen, which is fine
//now this: inBuf: FMemory $1, FSize 9 (no matter how long the msg is)
// FPosition 7077987 and FBytes: many many random
DebugOutput(inBuf.DataString); //the error is thrown here
where connfd is the connected socket, servsock is the listening socket, inLen is a cardinal containing the length of inBuf, inBuf is a global TStringStream. rec is a cardinal containing the # of bytes received by recv.
{ the client's send function }
function SSend(sock :TSocket; addr :sockaddr_in; msg :TStringStream) :Integer;
var
len: Cardinal;
begin
len := msg.Size;
send(sock, len, sizeof(len), 0);
msg.Seek(0,0);
send(sock, msg, sizeof(msg), 0);
Result := 0;
end;
and the client's call to SSend:
{ CREATE (OUTPUT)STREAM }
s := TStringStream.Create;
s.WriteString(_input.Text);
//_input is a TMemo with text, let's say, ´hello´
SSend(client, servaddr, s);
//client is a TSocket
Thanks for any help in advance!
p1.e