#Web Server

100 messages · Page 1 of 1 (latest)

karmic bison
#

I've been working on this for a while now, and I can't quite seem to figure out why it's not working.... I believe the networking stuff works, i get "404 not found" at the localhost:8000 when i run it. but i cant get it to give me ANYTHING else. Kinda stumbling hard on this one...

ANYone got any suggestions or hints as to what i'm doing wrong?

stark kelpBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

sharp hornet
#

seems like it's probably a path error in what you're requesting

#

are you sure the path you're requesting actually exists relative to the root_directory you're setting?

karmic bison
#

@sharp hornet As in when I call it in the command line, is the text file in the same “folder” (to dumb it down for me) as the .c file? Because, yes it is. Or are you saying that when I do call it in the command line I can’t just call the text file by name, I have to include the entire “desktop/college/csi/class7/test.text” path?

sharp hornet
#

Paths are relative to where you run the command to start the program from

mighty owl
#

as long as the process hasn't chdir itself

past sinew
karmic bison
#

so then, yes. the file is in the same folder as the .c file. So, if thats not the problem, why am i only seeing "404 not found"? when i check the localhost:8000? I'm sure it has something to do with how im parsing the client request.... I just cant find/see where I'm going wrong. and im reallly struggling with HOW you debug this type of project when you can't see any result from what you're changing... though at this point, the making it work is the debugging, so nevermind.... any more hints/suggestions.

PS- thank you guys so much for the suggestions so far.

stark kelpBOT
#

@karmic bison Has your question been resolved? If so, type !solved :)

karmic bison
#

@stark kelp no, it has not. please don't cut this life line! 🙏

serene ibex
#

There are a couple of problems. When you fetch the file you wish I presume you are fetching localhost:8000/filename? if so, then when you receive the request in your code and parse the GET request, you should get a path of /filename. You then remove the / from the front and just use filename. This requires that when you run the code you must be sure that your root directory ends with / so for example:

./WebServer /home/username/files/

Without the trailing / you will not correctly construct the full path.

Secondarily, once you read up the file content, you are sending a response header that looks like this:

"HTTP/1.0 200 OK\r\nContent-Length: %ld\r\n"

Where you of course provide the content length of the file requested. This is not quite correct as the HTTP response header must be terminated with a two carriage return line feed character sequence. So change the above response to look like this:

"HTTP/1.0 200 OK\r\nContent-Length: %ld\r\n\r\n"

With those constraints and changes it will work for you.

karmic bison
#

hey @serene ibex, I really appreciate the help. though i may still be a bit confused. I made the 200 OK change with the "\r\n" (can't believe i missed that one, i took that out as a test... my b)

now i run using this command line (which i think is what you were telling me) and this is what it shows:
mike_naz@TheBeast:/mnt/c/Users/Mikey/desktop/college/csi333$ ./Web_Server /mnt/c/Users/Mikey/desktop/college/csi333/c_tester.txt
Server is running on port 8000

but it doesn't change the web browser localhost:8000/Web_Server.c

which it should, right?

#

it still just displays 404 not found.

wraith orchid
#

get some tool for quick test of communication, e.g. curl

#

curl -v localhost:8000

#

well, you likely want to add the path

#

anyway it will show you in console the request and the response it got

karmic bison
#

/mnt/c/Users/Mikey/desktop/college/csi333/c_tester.txt

is that the path? that is the path... or am i being dumb???

#

ohhhhhh.... it's supposed to be .html isn't it?! durrrr. or is that wrong tooo?

serene ibex
#

The command line should specify a folder not the file name. The file name is specified in the browser or curl as appropriate. Is it clear?

wraith orchid
#

i.e. curl takes the same thing as browser

karmic bison
#

i think i'm grasping it, so you're saying that i shouldn't have the .txt file be the end of the path but the file that it lives in instead? and are you saying that my handle_client function should know what file it should look for? am i on the right track?

wraith orchid
#

so e.g. http://localhost:8000/c_tester.txt

#

you are asking weird questions for the code you say you wrote

serene ibex
#

Yep

karmic bison
#

Yeah... i felt that way for the past 4 weeks.... like when i ask questions he just gives me lecture notes to reference and code snippets.... not gonna lie, i feel like i should give up trying to grasp this. and asking the wrong questions seems like the core of my issue. sorry

serene ibex
#

I got you. Ask away until you get it.

karmic bison
#

yeah? You kinda made me realize this is futile, I'm too far from grasping this aren't I? thanks, but I'm not trying to waste your time. thanks though.

serene ibex
#

Your choice. Sorry to see you go. You are not wasting my time.

karmic bison
#

OK. regroup. pitty party over! it's not over yet!!!!! I'm locked in. Thanks man!

serene ibex
#

Sweet... I get it.

karmic bison
#

but please don't get tooo upset when i ask a dumb question. just say "dumb" and i'll move on to the next question, because i'm probably overthinking it most of the time anyway.

serene ibex
#

The only dumb Q is the one you don't ask.

karmic bison
#

haha. we'll see if i can prove that to be false.

stark kelpBOT
#

@karmic bison Has your question been resolved? If so, type !solved :)

karmic bison
#

@stark kelp NOOOOO! thank you!

#

ok, so. how exactly do you read curl. So i ran it and it gave me some stuff, mostly the 404 not found and the content-Length being 13 (which is the size of "404 not found") so not exactly useful but something?

#

OH!!!!!!

#

it worked!!!!

serene ibex
#

heh

karmic bison
#

ohhhhhh. drrrr. the path makes the server a directory! then the localhost:8000 becomes the like a live version of my folder. just the one that i ended the path on. so when i was ending the path on the actual file it was trying to access files that it (what points to?)?

#

(is any of that off the mark?)

serene ibex
#

Yeah, you specify on the server command line what directory (ending with /) you will use to serve up whatever file is requested in the browser or Curl. The localhost:8000 bit is just telling the browser or Curl the IP address and port from which to request the file name that follows. So localhost:8000/foo will request foo from the directory you specified when you ran the server.

karmic bison
#

that makes sooooo much sense!
yo! this blows my mind, but i still have some clarifying questions, if you dont mind. I'm still far from where I want to be from understanding this.

serene ibex
#

ask away

karmic bison
#

in the code my professor gave us, he used .ai_family, ai_addr, and ai_port, but google and the internets (haha) suggested i used sin_ insetad of ai_ when i was setting up the server socket. whats the difference, and also, what exactly is going on at that point? I found in his notes that it says, "TCP stream sockets" for the .ai_ part of that.

elder hare
#

i'm just learning too, otherwise i might have a more substantive answer

mighty owl
serene ibex
#

what he said

#

🙂

#

The server socket code is fine as you have it implemented, but you can read those links for details.

#

fine == functional 🙂

karmic bison
#

ha. truth, I can't wait to come back to this and really turn it into something nice. Especially can't wait to come back to this and grasp it enough to know what could be improved.

serene ibex
#

Eventually you should think about parsing the root directory path when evaluating the command line for the server because it is easy in the code as written to get it wrong and end up not having a valid path to whatever file they are asking for.

#

maybe something as simple as it verifying the directory exists

karmic bison
#

ok. that makes sense.

#

so the next big hurtle, that i have 6 hours to do... dear lord... (I honestly don't care if i dont get it done in time, I really just want to learn this stuff at this point) is taking a game that we made at the beginnig of the semester and adding it into our webserver code so that the game runs on the server... he describes it as "HTML-izing" one of the functions in our game that displays the current "board" essentially... do able in 6 hours or am i probably screwed?

#

I know ZERO html.

serene ibex
#

Has he not provided any html training?

karmic bison
#

no. BUT he seems to walk us through it in the assignment details. but no, HTML was never in the class slides nor in any lecture (and i never missed a lecture!) but again, I'm pretty sure he vaguely gives us what we need to know cause he's more focused on the server part

serene ibex
#

wow...

#

what does the screen look like in the game?

karmic bison
#

it's more a text based game... it was to teach us about pointers, linked lists, and understanding dereferencing. It's called words without friends.

serene ibex
#

sure, but what does it look like?

karmic bison
#

we read a file (the dictionary) and chose a random word (greater than 6 letters) and made the "game list" from every word in the library that could be made using the letters of the random word and then the user just had to find all the words that could be made

#

it ran in the shell so.... it didnt really "look" like anything

#

it displayed the list of words found and the list of words still not found (but with a dash to represent the hidden letter)

serene ibex
#

Well, I suppose in the simplest approach you could change all the text output to be prefixed with an HTML header specifying the content type as text/plain

elder hare
# karmic bison I know ZERO html.

you can learn enough HTML to do almost anything in 30 minutes. go to https://htmlboilerplates.com/ and grab the basic file that pops up, go to MDN and search for "div", "table", "img", "a", "h1", "ol", "ul", and "p", and youre good to go for almost anything. there are also tags for italics, underline, bold, etc, but i've given you a list of the most common logical containers. you might want to use "form" if youre sending data back to the server from a webpage, but beyond that i basically havent ever used anything else, and i took a webdev class. hell, i've given you quite a bit of fluff for your use-case

karmic bison
#

yeah, thats essentially what he says too. is it that easy? so instead of printing to the console, i just have it strcat() using a buffer specifically for.......... geeeeez. thanks @elder hare !

elder hare
# karmic bison yeah, thats essentially what he says too. is it that easy? so instead of printin...

yep, it really is that easy. only hard thing about HTML is that to make it look really pretty, you have to use CSS, and everyone hates CSS. i found it to be ok-ish when i was doing small websites for class, but i was using very new stuff in the standard (specifically flex-box for any time i wanted to change the default behavior of <div> to not be "create a new full-page-width logical box under the last one"), and even then it i could see how it could quickly become complexity hell, but i wouldn't think you'd need much (if any) CSS for this

serene ibex
#

So, just with plain text you could do something as simple as this:
<html>
<head><title>Whatever title you want</title></head>
<body><pre>
put the text from your game output here
</pre></body>
</html>

The <pre> tags will preserve the plain text formatting in the browser.

#

You could just reply with this HTML header:
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: nnn

put the text from your game output here

#

where nnn is the length of the output

#

My suggestion is you start small and simple and get some early success and then expand out as you desire.

elder hare
#

100%. the reason webdev can be so fun is the same reason building terminal apps can be really fun: its very easy to get very fast, very positive feedback

karmic bison
#

bet. reading his instructions, he says just copy paste most of the code and where i was printf()ing i should store it into a buffer that will get sent out on the network.... so i think im gonna just chip away at it.

#

I REALLLLLLLY appreciate your help. both of you. thank you so much. i felt very lost and alone and kinda freaking out, definitely feel better.

serene ibex
#

Reach out any time

karmic bison
#

I'll take you up on that. You're a frickin HERO!

karmic bison
#

ok. if you're available, would you mind taking a look at the frankenstein that is the fussion on my game code and webserver code? It runs, but then it hits a segmentaion fault... I can't find where i'm messing up.... but i changed a bunch...

mighty owl
#

i'd suggest compiling and running with asan at first

stark kelpBOT
#
Address Sanitizer

Memory errors in C and C++ are easy to make and they can be very hard to debug because they can manifest far from their source. Address sanitizer is a runtime checker that identifies memory errors at their source and makes debugging much simpler. Address sanitizer is available for gcc/clang on linux and msvc on windows. To use it simply pass -fsanitize=address to the compiler.

Note: Make sure to turn on debug symbols with -g for gcc/clang and -Zi for msvc.

ce Example

How to read sanitizer output

The first few lines tell you the problem, heap-use-after-free, due to performing a READ of size 4, at example.c line 7 (from the first line of the stack trace).

==1==ERROR: AddressSanitizer: heap-use-after-free on address ....
READ of size 4 at 0x602000000010 thread T0
    #0 0x40120f in main /app/example.c:7
    #1 0x7fda58629d8f  (...)
    #2 0x7fda58629e3f in __libc_start_main (...)
    #3 0x4010b4 in _start (...)

Additional information is also included such as where the allocation was performed and where the allocation was freed.

See Also
  • Other sanitizers exist and can be similarly helpful, including ubsan, threadsan, and memorysan.
serene ibex
#

I will be happy to look at it when I get home. I am currently in transit.

#

This gives you a pretty clear indication of where to look meanwhile:

$ gcc -g -fsanitize=address Web_Server_WWOF4.c
$ ./a.out
Server is running on port 8000
AddressSanitizer:DEADLYSIGNAL
=================================================================
==687==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x55ae038a6570 bp 0x7fffbe390230 sp 0x7fffbe390140 T0)
==687==The signal is caused by a WRITE memory access.
==687==Hint: address points to the zero page.
    #0 0x55ae038a6570 in initialization Web_Server_WWOF4.c:300
    #1 0x55ae038a58b8 in main Web_Server_WWOF4.c:94
    #2 0x7f20f419dd8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #3 0x7f20f419de3f in __libc_start_main_impl ../csu/libc-start.c:392
    #4 0x55ae038a5724 in _start (a.out+0x2724)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV Web_Server_WWOF4.c:300 in initialization
==687==ABORTING
#

Clearly a null pointer reference (0x000000000000) on line 300 of your program file.

#

Talk to you in a bit.

karmic bison
#

that actually helped.... still doesn't work, but it compiles. I need to be at work in 4 hours, so I'm hitting the potatoes... thanks for all your help today. HAPPY TURKEY DAY!

#

this what i have so far, if you have time to look at it any time. literally no rush. thanks again!

stark kelpBOT
#

@karmic bison Has your question been resolved? If so, type !solved :)

serene ibex
#

Well, there are plenty of issues, but you did a nice job with the HTML. There are plenty of things that will prevent this from displaying. I captured the HTML that you generated and just dropped it in a foo.html file and then loaded it in my browser.

#

You remember I suggested that you start very small and get an early success that you can build upon? Well, you need to actually write the HTML to the socket for the browser to render it. You did a good job building a form that will submit a new query with your guess. I would focus on actually outputting the generated HTML to the connected client.

idle sable
#

(didn't read all the replies). you probably are presuming the cwd

serene ibex