08-04-2004, 07:23 AM
This code works fine in the IDE, and when compiled it still runs, but displays "[ERROR] Memory Corrupt" at the very end.
This is basically just a copy of the gethttp.bas example, except I'm using a string for the receive buffer. If I make the buffer smaller (<=780 bytes), I do not get the error. But I want a big buffer.
Anybody know what's causing this or how to fix it?
This is basically just a copy of the gethttp.bas example, except I'm using a string for the receive buffer. If I make the buffer smaller (<=780 bytes), I do not get the error. But I want a big buffer.
Anybody know what's causing this or how to fix it?
Code:
DEFINT A-Z
'$DYNAMIC
'$INCLUDE: 'DSOCK.BI'
'$LIB: 'DSOCKQB.LIB'
CLS
DIM WSADat AS WSAData
DIM saServer AS sockaddrIn
ReqVer = MAKEWORD(1, 1)
rc = WSAStartup(ReqVer, WSADat)
IF rc <> 0 THEN
PRINT "Error:"; rc
rc = WSACleanup
END
END IF
IF WSADat.wVersion <> ReqVer THEN
PRINT "Winsock version not supported"
rc = WSACleanup
END
END IF
lpServerName$ = "www.qbasicnews.com"
lpHostEntry& = gethostbyname(lpServerName$)
IF lpHostEntry& = NULL THEN
PRINT "Can't find " + lpServerName$
rc = WSACleanup
END
END IF
SocketHandle& = socket(AF.INET, SOCK.STREAM, IPPROTO.TCP)
IF SocketHandle& = INVALID.SOCKET THEN
PRINT "Error:"; WSAGetLastError
rc = WSACleanup
END
END IF
lpServEnt& = getservbyname("http", "tcp")
IF lpServEnt& = NULL THEN
saServer.sinPort = htons(80)
ELSE
saServer.sinPort = servent.sPort(lpServEnt&)
END IF
saServer.sinFamily = AF.INET
saServer.sinAddr.sAddr = hostent.hAddrList(lpHostEntry&)
rc = connect(SocketHandle&, saServer, LEN(saServer))
IF (rc = SOCKET.ERROR) THEN
PRINT "Error:"; WSAGetLastError
rc = closesocket(SocketHandle&)
rc = WSACleanup
END
END IF
PRINT "Sending"
SendBuffer$ = "GET / HTTP/1.1" + CHR$(13) + CHR$(10)
SendBuffer$ = SendBuffer$ + "Host: " + lpServerName$ + CHR$(13) + CHR$(10)
SendBuffer$ = SendBuffer$ + "Connection: close" + CHR$(13) + CHR$(10) 'Keep-Alive?
SendBuffer$ = SendBuffer$ + "User-Agent: Your Mother" + CHR$(13) + CHR$(10)
SendBuffer$ = SendBuffer$ + CHR$(13) + CHR$(10)
lpSendBuffer& = MAKELONG(SADD(SendBuffer$), VARSEG(SendBuffer$))
rc = send(SocketHandle&, lpSendBuffer&, LEN(SendBuffer$), 0)
IF rc = SOCKET.ERROR THEN
PRINT "Error:"; WSAGetLastError
rc = closesocket(SocketHandle&)
rc = WSACleanup
END
END IF
PRINT "Receiving:"
BufferSize = 4096
ReceiveBuffer$ = SPACE$(BufferSize)
lpRecvBuffer& = MAKELONG(SADD(ReceiveBuffer$), VARSEG(ReceiveBuffer$))
OPEN "RECEIVE.TXT" FOR OUTPUT AS #1
DO
rc = recv(SocketHandle&, lpRecvBuffer&, BufferSize, 0)
IF rc = SOCKET.ERROR THEN
PRINT "Error:"; WSAGetLastError
rc = closesocket(SocketHandle&)
rc = WSACleanup
END
END IF
IF rc = 0 THEN EXIT DO
PRINT LEFT$(ReceiveBuffer$, rc);
PRINT #1, LEFT$(ReceiveBuffer$, rc);
LOOP
CLOSE #1
rc = shutdown(SocketHandle&, 2)
rc = closesocket(SocketHandle&)
rc = WSACleanup
END