atomic web server threads

Share your advanced PureBasic knowledge/code with the community.
tatanas
Enthusiast
Enthusiast
Posts: 276
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: atomic web server threads

Post by tatanas »

I didn't put any mutex in this example but I'm using them in my program.
A mutex for the SendNetworkString() is this example wouldn't fix the problem (there is only one Send and no competitive access).
Windows 11 Pro x64
PureBasic 6.30 x64
User avatar
idle
Always Here
Always Here
Posts: 6243
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

I'll take a look in the morning if I get time.
User avatar
idle
Always Here
Always Here
Posts: 6243
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

I don't see any issue with your test example on windows PB6.30 x64. The send in the thread doesn't crash it
tatanas
Enthusiast
Enthusiast
Posts: 276
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: atomic web server threads

Post by tatanas »

At the first start of the server there is no crash, SendNetworkString() return -1. But if you retry (stop/start the server), you should have a crash.
I'm not using TLS.
Windows 11 Pro x64
PureBasic 6.30 x64
User avatar
idle
Always Here
Always Here
Posts: 6243
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

tatanas wrote: Thu Jan 29, 2026 7:59 am At the first start of the server there is no crash, SendNetworkString() return -1. But if you retry (stop/start the server), you should have a crash.
I'm not using TLS.
I can't get it to crash at all. :?
User avatar
matalog
Enthusiast
Enthusiast
Posts: 322
Joined: Tue Sep 05, 2017 10:07 am

Re: atomic web server threads

Post by matalog »

HI Idle, I am running your single_server_localhost example, adjusted my hosts file to have 127.0.0.1 and I can see the site on all of the browsers on this computer.

Should I be able to see the web site on my mobile phone which is also registered on the same wifi network?
User avatar
HeX0R
Addict
Addict
Posts: 1258
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: atomic web server threads

Post by HeX0R »

not via 127.0.0.1 of course.
You need to call the network ip
User avatar
matalog
Enthusiast
Enthusiast
Posts: 322
Joined: Tue Sep 05, 2017 10:07 am

Re: atomic web server threads

Post by matalog »

HeX0R wrote: Fri Feb 20, 2026 2:56 pm not via 127.0.0.1 of course.
You need to call the network ip
Yes, of course. Should I have anything special in 'hosts' for that?

I am now trying the ip address of the computer I am running the Atomic Web Server on in my mobile phone, and it still doesn't see it.

EDIT: I hadn't updated the IP address in the program. Sorted now thanks.
User avatar
matalog
Enthusiast
Enthusiast
Posts: 322
Joined: Tue Sep 05, 2017 10:07 am

Re: atomic web server threads

Post by matalog »

I'm now trying to use this 'single_server_localhost.pb' as a server for a folder on PC to allow me to use .wget to download a file onto ZX Spectrum (with wifi adaptor attached) wirelessly.

The file seems to link okay, but doesn't actually download anything.

The terminal in the program returns "connect 1726160353472
192.168.x.xx GET /drawing.bmp HTTP/1.0 client 1726160353472 time 20:15:57"

The spectrum status just doesn't update until I close the server on PC or until the event times out.

Any ideas what I might have to change to allow the files to upload correctly? I can do this fine on windows IIS.

EDIT: Maybe this 'single_server_localhost.pb" isn't the type of server that I can just upload a file to the www folder, and expect all computers accessing the server to see it? I tried on my mobile phone, and it wasn't able to download it either.
Last edited by matalog on Fri Feb 20, 2026 10:46 pm, edited 1 time in total.
User avatar
idle
Always Here
Always Here
Posts: 6243
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

matalog wrote: Fri Feb 20, 2026 9:24 pm I'm now trying to use this 'single_server_localhost.pb' as a server for a folder on PC to allow me to use .wget to download a file onto ZX Spectrum (with wifi adaptor attached) wirelessly.

The file seems to link okay, but doesn't actually download anything.

The terminal in the program returns "connect 1726160353472
192.168.x.xx GET /drawing.bmp HTTP/1.0 client 1726160353472 time 20:15:57"

The spectrum status just doesn't update until I close the server on PC or until the event times out.

Any ideas what I might have to change to allow the files to upload correctly? I can do this fine on windows IIS.

EDIT: Maybe this 'single_server_localhost.pb" isn't the type of server that I can just upload a file to the www folder, and expect all computers accessing the server to see it? I tried onmy mobile phone, and it wasn't able to download it either.
you've replicated the git hub folder on your pc under www folder and your using wget from the zx spectrum to copy the files from your PC as long as it's using http and there's an index with links it should work.
atomic won't index or list the files, it will only fetch requested files.

It mentions on the github page you cant use : on in the command http:// is http// is wget adding it back in ?
so the server sees http://

as a sanity check can you see the local files in your web browser on the pc?
User avatar
matalog
Enthusiast
Enthusiast
Posts: 322
Joined: Tue Sep 05, 2017 10:07 am

Re: atomic web server threads

Post by matalog »

idle wrote: Fri Feb 20, 2026 10:20 pm
matalog wrote: Fri Feb 20, 2026 9:24 pm I'm now trying to use this 'single_server_localhost.pb' as a server for a folder on PC to allow me to use .wget to download a file onto ZX Spectrum (with wifi adaptor attached) wirelessly.

The file seems to link okay, but doesn't actually download anything.

The terminal in the program returns "connect 1726160353472
192.168.x.xx GET /drawing.bmp HTTP/1.0 client 1726160353472 time 20:15:57"

The spectrum status just doesn't update until I close the server on PC or until the event times out.

Any ideas what I might have to change to allow the files to upload correctly? I can do this fine on windows IIS.

EDIT: Maybe this 'single_server_localhost.pb" isn't the type of server that I can just upload a file to the www folder, and expect all computers accessing the server to see it? I tried onmy mobile phone, and it wasn't able to download it either.
you've replicated the git hub folder on your pc under www folder and your using wget from the zx spectrum to copy the files from your PC as long as it's using http and there's an index with links it should work.
atomic won't index or list the files, it will only fetch requested files.

It mentions on the github page you cant use : on in the command http:// is http// is wget adding it back in ?
so the server sees http://

as a sanity check can you see the local files in your web browser on the pc?

I have replicated the github folder on my PC. I added a bmp file to the www folder in that.

How do I index a new file in there? It is telling me that the files I add, that I haven't indexed, are Not found.

I can indeed see other files you use, like index.html on my phone and the local PC.

I can see 192.168.x.xx/images/atomic_webserver6.jpg from my phone, and searching the text of the whole source folder in notepad++, it doesn't seem to be mentioned.

How can I determine what images in the source folders will be available to browsing computers?
User avatar
idle
Always Here
Always Here
Posts: 6243
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

you can use a handler to do indexof to generate the hrefs of the root

Code: Select all

XIncludeFile "atomic_web_server3.pbi"

CompilerIf #PB_Compiler_OS = #PB_OS_Windows 
   #Cdir = "\" 
CompilerElse 
   #Cdir = "/"
CompilerEndIf 

Structure folderContents
   type.b
   value.s
EndStructure

Procedure GetFileList(StartDir.s,List Lfiles.folderContents(),pattern.s="*.*",Recursive=1,bset=0)
   Protected mDir,Directory.s,Filename.s,FullFileName.s, tdir.s,ct,a,depth,bmatch
   
   Static NewList Lpattern.s()
   Static FileCount
      
   If Not bset
      StartDir = RTrim(StartDir, #Cdir) 
      pattern = RemoveString(pattern,"*.")
      ct = CountString(pattern,"|") + 1
      ClearList(lpattern())
      For a = 1 To ct 
           AddElement(Lpattern())
           Lpattern() = UCase(StringField(pattern,a,"|"))
        Next
        filecount=0
        bset=1
   EndIf 
 
  mDir = ExamineDirectory(#PB_Any, StartDir, "*.*") 
  If mDir 
    While NextDirectoryEntry(mDir)
      If DirectoryEntryType(mDir) = #PB_DirectoryEntry_File
          Directory = startdir
          FileName.s = DirectoryEntryName(mDir)
           ForEach Lpattern()
              If  Lpattern() = GetExtensionPart(UCase(Filename))
                  bmatch=1
              ElseIf  Lpattern() = "*"
                 bmatch =1 
              EndIf   
              If bmatch 
                  FullFileName.s = StartDir + #Cdir + FileName
                   AddElement(LFiles()) 
                   Lfiles()\value = FullFileName
                   Lfiles()\type = #PB_DirectoryEntry_File
                  FileCount+1
                  bmatch =0    
               EndIf
            Next  
        Else
         tdir = DirectoryEntryName(mDir)
         If tdir <> "." And tdir <> ".."
            If Recursive = 1
              depth + 1
            GetFileList(startDir + #Cdir + tdir,LFiles(),Pattern,Recursive,bset)
           EndIf
         EndIf
      EndIf
   Wend
   FinishDirectory(mDir)
  EndIf
    
  ProcedureReturn FileCount

EndProcedure

Procedure Indexof(*request.Atomic_Server_Request) 
  
  Protected *Atomic_Server.Atomic_Server = *request\Serverid  
  Protected *atomic_client.Atomic_Server_Client = *request\clientID 
  
  Protected NewList myfiles.folderContents() 
  GetFileList("./www/",myfiles()) 
  
  Protected file.s,content.s = "<!DOCTYPE html>" + #CRLF$
  content + "<html><head>" + #CRLF$
  content + "<title>" + *Atomic_Server\Title + "</title>" + #CRLF$
  content + "</head><body>" 
  ForEach myfiles() 
    file.s = RemoveString(myfiles()\value,"./www/")
    content + "<a href="+ #DQUOTE$ + file + #DQUOTE$ + ">" + file + "</a></br>" 
  Next   
  content + "</body>" 
  
  *request\status = 200 
  *request\ContentType = "text/html"
  
  Protected *Data = UTF8(content) 
  
  ProcedureReturn *data  
   
EndProcedure   

Global server1 = Atomic_Server_Init("atomic_webserver","./www/","127.0.0.1","",8080,#PB_Network_IPv4,5000) 

Atomic_Server_Add_Handler(server1,"indexof",@Indexof()) 
OpenConsole() 

If Atomic_Server_Start(server1,-1,1)  ;start server no window logs to console 
  
  Repeat 
  Until Input() = "quit"   ;type quit to exit 
  
  Atomic_Server_Exit(server1)
  CloseConsole()
  
EndIf 



User avatar
matalog
Enthusiast
Enthusiast
Posts: 322
Joined: Tue Sep 05, 2017 10:07 am

Re: atomic web server threads

Post by matalog »

Thanks for trying to help with this Idle. Sorry, I don't know a lot about working with TCP/IP or indeed HTML, can you tell me what I need to do with that indexer you made?

Is that indexer necessary to allow other computers to be able to download something from the contents of the www folder?

Is it correct that I should not be able to download a file that I add to your www folder when I run a fresh download of your single_server_localhost?
User avatar
idle
Always Here
Always Here
Posts: 6243
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

I just updated my post above removing the ./www/

if you use a program like wget it will just follow the href links
so if you add any files to the server directory a call to http://indexof will return a simple html page with all the files so wget should fetch them all

to do a subdirectory you can do it like

GetFileList("./www/assets",myfiles())


you can use
wget -r --follow-tags=a http://192.168.1.x

or alternatively you can change the indexof function to output the paths to a txt file
User avatar
matalog
Enthusiast
Enthusiast
Posts: 322
Joined: Tue Sep 05, 2017 10:07 am

Re: atomic web server threads

Post by matalog »

Hi Idle, sorry, I don't really understand what I should be doing. I lack the knowledge that you may be assuming I have.

Do I run your example 'single_server_localhost' before the example you shared, or after, or put them in the same program?

Your new example is working with port 8080, and that won't open, even if I have 80 open already as an instance of your 'single_server_localhost' program.

At the minute, i'm just trying to be able to download a photo that I place in your 'www' folder, from my phone, both regsitered on the same wifi network.

I don't really need to see an index of the files, i'm not sure if the phone does...

I'll be typing '192.168.x.xxx/1.jpg' into my phone and trying to download an image file called '1.jpg' that is located in the 'www' folder of your example.

What do I need to do to achieve that?

P.S. Why is the PB Forum server very slow these days?
Post Reply