News for hackers. Stuff that matters.
Re:Let’s Build a Web Server. Part 1 by corysama on Friday April 03, 2015 @08:20PM (#9319349)
Pretty much the same minimal http server in C:

    #include <WinSock.h>
    #include <stdio.h>
    #pragma comment(lib, "wsock32.lib")
    int main(int argc, const char *argv[]) {
        WSADATA wsadata;
        WSAStartup(2, &wsadata);
        sockaddr_in address;
        memset(&address, 0, sizeof(address));
        address.sin_family      = AF_INET;
        address.sin_addr.s_addr = inet_addr("0.0.0.0");
        address.sin_port        = htons(80);
        int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
        bind(sock, (struct sockaddr *)&address, sizeof(address));
        for(;;) {
            listen(sock, 0);
            int connection = accept(sock, NULL, NULL);
            char recvBuffer[1024];
            int recvSize = recv(connection, recvBuffer, sizeof(recvBuffer)-1, 0);
            recvBuffer[recvSize]=0;
            printf(recvBuffer);
            char response[] = "HTTP/1.1 200 OK\nContent-Type: text/html\n\nlol";
            send(connection, response, sizeof(response), 0);
            closesocket(connection);
        }
        return 0;
    }
Re:Let’s Build a Web Server. Part 1 by mhogomchungu on Saturday April 04, 2015 @04:29AM (#9320360)
> char response[] = "HTTP/1.1 200 OK\nContent-Type: text/html\n\nlol";

isnt it better to write that as:

const char* response = "HTTP/1.1 200 OK\nContent-Type: text/html\n\nlol"; ?

Having the string in a writable buffer when you have no intention of modifying the string seems like not a very good idea.

Any decent compiler will evaluate "strlen(response)" at compile time and hence there will be no run time penalty here.

Re:Let’s Build a Web Server. Part 1 by exDM69 on Saturday April 04, 2015 @06:57AM (#9320525)
> Having the string in a writable buffer when you have no intention of modifying the string seems like not a very good idea.

Any decent compiler will also notice this and put the string literal in the .RODATA section. Using "const" doesn't really make a difference, it's more of a hint for the compiler.

It really doesn't make any difference in this case.

Re:Let’s Build a Web Server. Part 1 by smoyer on Saturday April 04, 2015 @09:34AM (#9320776)
It may be a hint to modern compilers, but it's also the documentation of intent for future developers[1].

[1] Where future developers includes me - I'll forget what I wrote in six months or less.

Re:Let’s Build a Web Server. Part 1 by mattstreet on Saturday April 04, 2015 @10:32AM (#9320915)
Not just as documentation, it would prevent another developer from accidentally modifying it.
Re:Let’s Build a Web Server. Part 1 by smoyer on Saturday April 04, 2015 @10:58AM (#9320990)
Excellent point ... I kind of assumed that (your IDE giving you a warning that you're trying to modify a constant) when I was thinking about it as documentation but your point more succinctly describes "making it hard to make mistakes".
Re:Let’s Build a Web Server. Part 1 by Matheus28 on Saturday April 04, 2015 @04:02PM (#9321996)
It does, the compiler doesn't know what the function "send" will do. The const isn't the focus of what he said, it's allocating in the stack vs in a read-only memory location.
Re:Let’s Build a Web Server. Part 1 by Elizer0x0309 on Friday April 03, 2015 @11:41PM (#9319813)
This is more inline with what the blog's author should've done with all the grandiose intro of trying to understand the underlying system. Good work!
Re:Let’s Build a Web Server. Part 1 by _asummers on Friday April 03, 2015 @08:25PM (#9319364)
Why is for(;;) used instead of while(true)? I've seen this idiom used many times but never understood the reasoning why. According to this[0] SO, there are no performance gains, so why would one prefer the for version?

[0] http://stackoverflow.com/questions/2288856/when-implementing...

Re:Let’s Build a Web Server. Part 1 by corysama on Friday April 03, 2015 @08:41PM (#9319409)
They are equivalent. It's just a matter of personal preference. I like it because it's shorter and it doesn't read as a loop that evaluates the value 'true' on each iteration. Even though you know the evaluation optimizes away trivially if you think about it, I don't even want to think that far when I'm scanning code.
Re:Let’s Build a Web Server. Part 1 by nchelluri on Friday April 03, 2015 @08:42PM (#9319413)
I had a professor who gave some reasoning about this. He's written a paper: http://plg.uwaterloo.ca/~pabuhr/papers/MELoop.ps

I haven't read the whole thing but I like this bit:

> There are many algorithms that are best written using an exit from the middle of a loop. If not written in this fashion, techniques such as the traditional “priming” of a loop must be used. For example, in reading a file:

  read(a, b, c)
  WHILE NOT eof DO
    { process a, b, & c }
    read(a, b, c)
  END WHILE


  LOOP
    read(a, b, c)
    WHEN eof EXIT
    { process a, b, & c }
  END LOOP
> The example on the left [top] shows the priming of a loop. This requires duplication of the READ statement or, in other cases, creation of a subprogram to eliminate the duplication, both of which are undesirable. As well, loop priming can be done an arbitrary distance before the beginning of the loop making the program difficult to read as the priming statement(s) is critical to the understanding of the loop.
Re:Let’s Build a Web Server. Part 1 by _asummers on Friday April 03, 2015 @09:02PM (#9319458)
This wasn't really my question. I was asking about the different infinite loop idioms in C family languages and the rationale for using one over the other.
Re:Let’s Build a Web Server. Part 1 by ptaipale on Saturday April 04, 2015 @02:02AM (#9320088)
Because the original K&R book introduced for(;;) structure.
Re:Let’s Build a Web Server. Part 1 by ternaryoperator on Friday April 03, 2015 @08:54PM (#9319438)
In the first versions of C, 'true' was not a keyword, so for(;;) was the way an endless loop was encoded. After that, it became the convention.
Re:Let’s Build a Web Server. Part 1 by machosx on Saturday April 04, 2015 @12:16AM (#9319879)
Why not just use while(1)?
Re:Let’s Build a Web Server. Part 1 by ufo on Saturday April 04, 2015 @01:05AM (#9319971)
Personal preference. Some people think "for(;;)" looks nicer.
Re:Let’s Build a Web Server. Part 1 by z92 on Saturday April 04, 2015 @04:52AM (#9320388)
the "for" version is also shorter by a single byte.
Re:Let’s Build a Web Server. Part 1 by pcthrowaway on Saturday April 04, 2015 @02:19AM (#9320126)
I just started learning Go last night, but I'll hazard a guess: since Go draws heavily from C, but has the for loop as its only looping construct, it seems likely that this habit has carried back into C code written by developers who've used Go, or who've been reading a lot C code written by developers who also use Go.
Re:Let’s Build a Web Server. Part 1 by jsprogrammer on Saturday April 04, 2015 @03:17AM (#9320249)
for(;;) existed way before Go came along.
Re:Let’s Build a Web Server. Part 1 by pcthrowaway on Saturday April 04, 2015 @04:48AM (#9320380)
I'm not saying it didn't, but 'while (true)' would be the more intuitive way to write that in any language besides Go.
Re:Let’s Build a Web Server. Part 1 by throwawayfor on Saturday April 04, 2015 @10:34AM (#9320924)
Another reason is that MSVC whines about loop conditionals evaluating to constants. Or at least it used to. So while(1) would cause a warning, but not for(;;)
Re:Let’s Build a Web Server. Part 1 by phodo on Friday April 03, 2015 @09:41PM (#9319549)
See this list of winsock alternatives if trying on non-windows http://tangentsoft.net/wskfaq/articles/othersys.html