Hi,
Can someone tell me as to what is the problem with the following code ?
This one does not work with the browser.
program trysocket;
uses
sockets,sysutils,winsock;
var
serverMessage : String;
clientMessage : String;
headerString : String;
ServerAddr : TInetSockAddr;
ClientAddr : TInetSockAddr;
ClientSocket : LongInt;
ServerSocket : LongInt;
ClientAddrSize : LongInt;
count : LongInt;
procedure PrintError(Const Msg :String);
begin
Writeln(Msg,SocketError);
Halt(100);
end;
begin
ServerSocket := fpSocket(AF_INET,SOCK_STREAM,0);
if ServerSocket = SOCKET_ERROR then
PrintError('Server Socket Error : ');
ServerAddr.sin_family := AF_INET;
ServerAddr.sin_port := htons(9090);
if fpBind(ServerSocket,@ServerAddr,SizeOf(ServerAddr)) = SOCKET_ERROR then
PrintError('Server Bind Error : ');
if fpListen(ServerSocket,1) = SOCKET_ERROR then
PrintError('Server Listen Error : ');
Writeln('Waiting for connection from client');
ClientAddrSize := SizeOf(ClientAddr);
ClientSocket := fpAccept(ServerSocket,@ClientAddr,@ClientAddrSize);
if ClientSocket = SOCKET_ERROR then
PrintError('Client Socket Accept Error : ');
Writeln('Client Socket Created ',ClientSocket);
count := fprecv(ClientSocket,@clientMessage,20000,0);
Writeln(clientMessage);
if (count <> SOCKET_ERROR) And (count > 0) then
begin
serverMessage := 'HTTP/1.0 404 Not Found\n';
Writeln(ClientSocket,serverMessage,23);
fpSend(ClientSocket,@serverMessage,23,4);
end;
CloseSocket(ClientSocket);
end.
Thanks in advance.