Is there is Windows Socket lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- manjunath · 2017-01-15 15:45 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. ---------------------------------------------------------------------- rvk · 2017-01-15 16:07 Hi and Welcome, Next time try to put code between code-tags. It's much better readable that way. Also try to mention what errors you're getting and what you expect from the code. Just dumping code and saying it doesn't work isn't a really useful problem-description. Your problem lies in this line: Code: Pascal [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i count := fprecv(ClientSocket,@clientMessage,20000,0);Your clientmessage is a String. But a string-type is just a pointer to a memory location where you have characters. But that memory-location isn't yet defined. It normally get's defined when you do clientmessage := 'abc' or SetLength(clientMessage, 2000) or something. So either declare clientmessage as array[0..32000] of char or do a SetLength(ClientMessage, 32000) before calling this line. (With as much data as you expect to receive. In your case you're trying to read 20000 chars so this will work: Code: Pascal [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var clientmessage: array[0..20000] of char;...count := fprecv(ClientSocket,@clientMessage,20000,0); ---------------------------------------------------------------------- Thaddy · 2017-01-15 16:27 array[0..19999] of char? Strictly speaking it is always BYTE not CHAR. ---------------------------------------------------------------------- rvk · 2017-01-15 16:32 Quote from: Thaddy on January 15, 2017, 05:27:45 pmarray[0..19999] of char? Strictly speaking it is always BYTE not CHAR. You're right about the 19999. But if you make it array of byte you can't treat it like a string. With array of char you can do writeln(clientmessage); With array of byte you can't and need to convert it. ---------------------------------------------------------------------- manjunath · 2017-01-15 16:40 Thanks a lot. Will try this. My other doubt is whether I should use Windows socket or not. I am trying this on Windows 10 system. Regards ---------------------------------------------------------------------- Thaddy · 2017-01-15 17:16 Windows sockets are simply the Berkeley socket implementation for Windows. It is basically the same on most if not all other platforms as well. All what is otherwise provided are higher level wrappers and/or additions. @rvk: The protocol streams should be treated as bytes. because the protocol says stream then store (including store to a screen, file, memory, whatever). Conversions for display are costly and should be done where appropriate. Therefor only treat the buffers as string when it is clear it is a string. Which is not often the case..... Even html pages are byte streamed, not string streamed. ---------------------------------------------------------------------- manjunath · 2017-01-15 17:34 Thanks a lot for the inputs. Coming from the java stream, I am adjusting to the new paradigm. All your inputs would be of great help. Appreciate you guys. ---------------------------------------------------------------------- Remy Lebeau · 2017-01-15 19:02 In addition to what others said, recv() can return either -1 on error, 0 on graceful disconnect, and > 0 on success, so checking for >0 by itself is sufficient in your example. But more importantly, you are sending an HTTP response (are you trying to implement an HTTP server?), but your response is incomplete. HTTP uses CRLF line breaks, and an HTTP message separates its headers and body using 2 line breaks, but you are only sending one. And you should include a "Content-Length: 0" header so the client knows you are not sending any body data, and also include a "Connection: close" header so the client knows you are about to close the socket connection. If you really want to implement an HTTP server, consider using a pre- existing solution, such as the TIdHTTPServer component in Indy. ---------------------------------------------------------------------- manjunath · 2017-01-15 19:08 Hi, I tried the following code. But it shows me a blank page. Can you please let me know why ? Code: Pascal [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program trysocket;uses sockets,sysutils,winsock;var serverMessage : String; clientMessage : array[0..19999] of char; headerString : String; ServerAddr : TInetSockAddr; ClientAddr : TInetSockAddr; ClientSocket : LongInt; ServerSocket : LongInt; ClientAddrSize : LongInt; count : LongInt; msglen : integer;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.1 200 OK' +slinebreak; serverMessage := serverMessage + 'Date: Mon, 27 Jul 2009 12:38:53 GMT' + slinebreak; serverMessage := serverMessage + 'Server: MyServer (Win64)' + slinebreak; serverMessage := serverMessage + 'Last-Modified: Wed 22 Jul 2009 19:15:56 GMT'+slinebreak; serverMessage := serverMessage + 'Content-Length: 71'+slinebreak; serverMessage := serverMessage + 'Content-Type: text/html'+slinebreak; serverMessage := serverMessage + 'Connection: Closed'+slinebreak; serverMessage := serverMessage + '