WebServer dont work with 2.12

This forum is meant for questions and discussions about the X# language and tools
User avatar
Horst
Posts: 336
Joined: Tue Oct 13, 2015 3:27 pm

WebServer dont work with 2.12

Post by Horst »

Hello
With the help of this community i build a little WebServer. Instead to use IIS or Apache. It works very well.
Now i compiled it with 2.12 and the WebServer starts. When i open the browser and type localhost nothing happen any more.
This little WebServer is the core/heart of my App.
So i need help !! Because i dont know how this core realy works :-) and i think its a XSharp bug.

Horst
Attachments
WebServer.zip
(206.51 KiB) Downloaded 89 times
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

WebServer dont work with 2.12

Post by ic2 »

Hello Horst,

Your zip doesn't seem to contain X# code? What do you expect us to test or check?
I also noticed a server.log with messages like Die Datei "C:WebServererror.html" konnte nicht gefunden werden..

Did you notice that and does this explain some of your issues?

Dick
User avatar
Horst
Posts: 336
Joined: Tue Oct 13, 2015 3:27 pm

WebServer dont work with 2.12

Post by Horst »

Hi Dick
Its a Xide VIAEF file into the zip. i thought thats ok.
The logfile is not relevant for this error. the program will not call any thread when its compiled by x# 12.2

PRIVATE METHOD HandleRequests() AS VOID
LOCAL context AS HttpListenerContext
SELF:httpListener:Start()
TRY
WHILE SELF:httpListener:IsListening
Console.WriteLine("The Linstener Is Listening!")
context := SELF:httpListener:GetContext()
SELF:messages:Add(context)

Console.WriteLine("The Linstener has added a message!")
END WHILE

CATCH e AS Exception
Console.WriteLine(e:Message)
END TRY

i think , the bold text is not working anymore.
in my example compiled with x#10c the browser will show index.html and crash. the crash is ok because i made the example small as possible,
but compiled with x# 12 the browser is the whole time waiting for response and will not show the index.html.

WebServer.exe has to run as admin with the parameter -80
Sorry ist hard to explain. ;-)
Horst
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

WebServer dont work with 2.12

Post by wriedmann »

Hi Horst,
first of all: I don't think anyone can help you with incomplete code.
To have a help, first of all your sample has to be complete and you have to deliver instructions how to build and run in.
On my machine, the library does not build because QRCoder.dll is missing.
Do you have tried to debug your executable, maybe using System.Diagnostics.Debug.Write() methods?
But nevertheless, I have been able to compile and run your code.
A few points:
- it is bad code to don't test if any parameters are passed to your program
- why you are starting your listener outside the try-catch block? So if there is any exception in this code, your application will crash
- write errors to the console does not helps a lot, it may be a lot better to write them also to a file
But anyway, on my machine it works:
webserver.png
webserver.png (42.6 KiB) Viewed 739 times
I have to admit that I don't run the 2.12 compiler, but a newer prerelease version.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Horst
Posts: 336
Joined: Tue Oct 13, 2015 3:27 pm

WebServer dont work with 2.12

Post by Horst »

Hallo Wolfgang
This listener is build with some of your code and help/code of others.
As i said, i dont know realy what it does :-) My part starts with the response method - the simple clipper code ;)
i have no experience when it goes to close to the machine.
So for any advice to make it more stable i am realy happy.

in the attachement i tryed to make a better sample (less code) , when you can compile and run it , and the "hello world" comes so all is ok, i can wait till x# 2.13 :-)

Horst

ps:
1 - in the zip is a readme file - do you think its ok like that for others to let it run ?
2 - i dont know to explain how to compile , is it not in the viaef file included ?
Attachments
WebServer.zip
(19.61 KiB) Downloaded 96 times
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

WebServer dont work with 2.12

Post by ic2 »

Hello Horst,
Horst wrote: Its a Xide VIAEF file into the zip. i thought thats ok.
Probably, as Wolfgang got it working. I understand that this is a Xide variation on the VO AEF file(s) but I have no idea how to run this. It doesn't start to run with F5 or Shift F5, I can's set breakpoints, etc. You limit your audience to those familiair with Xide. If it contains a product file it should probably run in Xide also for those not using Xide, like me.

There is also no Readme in the zip (I checked the zip of your second upload)

Dick
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

WebServer dont work with 2.12

Post by wriedmann »

Hi Dick,
personally I think that everyone that uses X# should also have XIDE installed - even if not used in production it is the best environment for test code, for a small library or a console executable.
And it also gives the possibility to transfer code using export files like VO.
Wolfgang
P.S. there IS a readme.txt in the second zip
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

WebServer dont work with 2.12

Post by wriedmann »

Hi Horst,
personally I think you should try to understand what you are writing and using.
A few samples how code should not be:

Code: Select all

function Start (args as string [] ) as void
Console:WriteLine(args [1]) 
If someone tries to start your executable without parameters, it will terminate with a runtime error.
IMHO you should write code like this

Code: Select all

if args:Length > 0 .and. args[1]:Length >= 2
   cArg := args[1]
else
   cArg := "-80"
   Console.WriteLine( "Error: application has to be started with the port as parameter '-80' is the default" )
endif
Console:WriteLine(cArg)
Another sample, as I wrote before:

Code: Select all

private method HandleRequests() as void 
local context as HttpListenerContext
self:httpListener:Start()
try
while self:httpListener:IsListening
   Console.WriteLine("The Linstener Is Listening!")
   context := self:httpListener:GetContext()
   self:messages:Add(context)
   Console.WriteLine("The Linstener has added a message!")
end while
catch e as Exception
Console.WriteLine(e:Message) 
end try
You yhould move the "self:httpListener:Start()" inside the try - catch block because otherwise you will not be notified if the application gives a runtime error inside that statement:

Code: Select all

private method HandleRequests() as void 
local context as HttpListenerContext
try
self:httpListener:Start()
while self:httpListener:IsListening
   Console.WriteLine("The Linstener Is Listening!")
   context := self:httpListener:GetContext()
   self:messages:Add(context)
   Console.WriteLine("The Linstener has added a message!")
end while
catch e as Exception
Console.WriteLine(e:Message) 
end try
I'm far away from being an expert in the .NET Framework programming, so my samples are far from being perfect, but they should give you an idea what you can make better.
Wolfgang
P.S. and yes, when compiled with X# the output is shown in the browser, but there is also a runtime error:

Code: Select all

05:07:11 Processor 1 crashed. System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
   bei Service.HttpServer.Response(HttpListenerContext ocontext) in C:XSharpXIDEProjectsTestProjectApplicationsWebServerPrgHttpServer.prg:Zeile 245.
   bei Service.HttpServer.Processor(Int32 number, BlockingCollection`1 messages) in C:XSharpXIDEProjectsTestProjectApplicationsWebServerPrgHttpServer.prg:Zeile 91.
04-07-2022_05-07-25.png
04-07-2022_05-07-25.png (4.6 KiB) Viewed 739 times
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Horst
Posts: 336
Joined: Tue Oct 13, 2015 3:27 pm

WebServer dont work with 2.12

Post by Horst »

Hallo Wolfgang

I see, i tryed to catch der arguments on the wrong place and i will change that.

Code: Select all

PUBLIC METHOD Start(port AS STRING ) AS VOID
LOCAL i AS LONG
// Falls ohne Parameter gestartet-  Standartport 8080 nehmen                   
IF Empty (port) ; port := "80" ; ENDIF
SELF:listenerLoop := Thread{HandleRequests}
SELF:httpListener:Prefixes:Add( "http://*:"+AllTrim(port)+"/") //hk
And now i see also witch line of code i have to move into the try :-)

The runtime error is wired because its only happend when you type localhost at the first time , type it again (while the app is still running) no runtime error appears.

When i compile i have 1 warning

Code: Select all

warning XS0165: Use of unassigned local variable 'bBuffer'
LOCAL bBuffer  ?? 	AS BYTE[]	
and its a shame, i dont know how to assign a byte

About debug. is it right, this app i cant let it run with the debugger ? Thats why also Dick cant let it run with F5 in the IDE.

And i am happy it works with your newer version of X#, i was realy shocked when the core of my app was no more working.

Thanks for your help
Horst
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

WebServer dont work with 2.12

Post by wriedmann »

Hi Horst,
about assigning a value to a byte array ( byte[] ), you can find that information here:
https://docs.xsharp.it/doku.php?id=string_char_byte
And for debugging purposes you can also start your executable and attach the Visual Studio Debugger to your running executable.
But debugging is not only using a debugger, but can also be done writing to the debug port or to log files.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply