-1

I'm currently following a tutorial for initializing the winsock struct, but it uses "#include&lt" instead of a usual "#include" statement. I'm wondering what the difference is and if the distinction is necessary. I have a lot of experience coding in C and C++ for Unix systems, but am relatively new to windows development. The full code in the tutorial is as follows:

#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET s;
    struct sockaddr_in server;

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    printf("Initialised.\n");

    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }

    printf("Socket created.\n");


    server.sin_addr.s_addr = inet_addr("74.125.235.20");
    server.sin_family = AF_INET;
    server.sin_port = htons( 80 );

    //Connect to remote server
    if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }

    puts("Connected");

    return 0;
}```

UPDATE: This was apparently a result of an html typo in the original tutorial's website and should just be ```#include <stdio.h>``` 
sbeisner
  • 23
  • 5
  • 3
    `#include<winsock2.h>` should not compile with the html escape sequences. Are you talking about the difference between `#include ` and `#include "someheader.h"` there should be many duplicates for the latter. – drescherjm Jan 14 '20 at 16:18
  • 1
    Browser issue in rendering the escaped characters: https://stackoverflow.com/questions/5068951/what-do-lt-and-gt-stand-for – sshashank124 Jan 14 '20 at 16:18
  • It appears the site is displaying the unescaped HTML escape sequences of the <> characters (`<`, `>`). Just substitute them. – Govind Parmar Jan 14 '20 at 16:19
  • 4
    There are plenty of other indicators the HTML escaping when off the rails in this code. `printf("Initialised.\n");` for example. – WhozCraig Jan 14 '20 at 16:19
  • 2
    I think we need a response from the OP on this. – drescherjm Jan 14 '20 at 16:23
  • 2
    The edit assumes that the HTML-escaping issue is the OP's, as opposed to the original code's. The two answers so far assume the opposite. We really need to know which it is. – John Bollinger Jan 14 '20 at 16:24
  • Does this answer your question? [Difference between angle bracket < > and double quotes " " while including header files in C++?](https://stackoverflow.com/questions/3162030/difference-between-angle-bracket-and-double-quotes-while-including-heade) – smac89 Jan 14 '20 at 16:24
  • 2
    And closing as a typo (as opposed to as a dupe) also relies on an assumption about where the escaping issue was introduced. – John Bollinger Jan 14 '20 at 16:26
  • 4
    @smac89 Please don't completely change the meaning of the question in an edit, unless OP has told you that your interpretation is correct. – interjay Jan 14 '20 at 16:28
  • https://gcc.godbolt.org/z/R3Bt62 – Aykhan Hagverdili Jan 14 '20 at 16:34
  • 1
    [Here's](https://www.binarytides.com/winsock-socket-programming-tutorial/) the tutorial which has this issue with escape codes, so we can see that the question is really about the HTML escaping problem. – interjay Jan 14 '20 at 16:35
  • 1
    sbeisner, please double-check that the shown code looks exactly as the code you are wondering about. Do you see `` at home or actually `<winsock2.h>`? Your answer to this is fundamentally important for making this question answerable. Though it was closed for a questionable reason, without that answer by you the question stays deservedly closed. But if you answer, I will try to be the first to vote for reopening. – Yunnosch Jan 14 '20 at 16:35
  • 1
    I actually saw &lt as opposed to "<" or ">" I understand that this was most likely a typo from the poster of the tutorial. I just wanted to make sure I wasn't missing some syntax complications with windows. Thanks. – sbeisner Jan 14 '20 at 16:56

2 Answers2

5

That is just bad HTML formatting that slipped in by accident.

The proper syntax is:

#include <winsock2.h>

And because in HTML the < and > symbols are frequently encoded as &lt; and &gt;, the encoding accidentally got into your example code, rather than the proper symbol.

Same with further down in the code where you'll see &quot; and &amp;, and it should actually be " and &.

printf(&quot;Could not create socket : %d&quot; , WSAGetLastError());
printf("Could not create socket : %d", WSAGetLastError());

if (WSAStartup(MAKEWORD(2,2),&amp;wsa) != 0)
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)

Slightly Related Trivia:

C has Trigraphs, which makes this code perfectly legal:

??=include <stdio.h>
int main(void)
??<
    char text??(??) = "Hello World";
    printf(text);
    return 0;
??>
abelenky
  • 63,815
  • 23
  • 109
  • 159
1

It should be #include <winsock2.h>.

The problem is that <> is replaces eith &lt;&gt; and it isn't replaced back.

&lt; is the HTML escape of < and &gt; is the HTML escape of >.

In order to prevent XSS, it may got replaced one time too much.

E.g. the author wanted to use the HTML escape but the XSS protection escaped the escape and your browser undid the escape only once.

Another possibility is that the XSS protection replaced the submission of the author with HTML escapes and another XSS protection replaced it again when you tried to load the page.

As you see in your code, this is also the case with " (&quot;) and & (&amp;).

It is also possible that there was no XSS protection when the tutorial was created and the author correctly escaped the special characters. After that, an XSS protection was added . This XSS protection escaped the escapes and lead to your result.

dan1st
  • 12,568
  • 8
  • 34
  • 67