Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Odd Error with TcpSocket and HTTP  (Read 3180 times)

0 Members and 1 Guest are viewing this topic.

chessguy

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Odd Error with TcpSocket and HTTP
« on: January 13, 2013, 11:12:14 pm »
Greetings. After I discovered port forwarding, I decided to try to make my own server, for testing web things. This would be able to, unlike free hosts, offer more freedom, for obvious reasons, such as server-side scripting. Not to mention, I can learn a bunch - say, POP or HTTP and networking things - side by side with networking through the server for games and experimenting with HTML5 and such things.

Anyway, As I was testing the port forwarding, I was able to receive the HTTP requests fine, and regardless of what it said, I sent a test response back, with a rather basic page.

I am guessing my HTTP is pretty bad, but I DID manage to get something. IT appeared as though the browser decided to display everything I said it, as simple as possible. It did display the <p> element correctly. It read:

Quote
Some paragraph here...

Failed to set socket option "TCP_NODELAY" ; all your TCP packets will be bufferedFailed to enable broadcast on UDP socketCannot send data over the network (no data to send)Cannot receive data from the network (the destination buffer is invalid)vector::_M_fill_insertFailed to bind listener socket to port Failed to listen to port Failed to accept a new connection, the socket is not listening255.255.255.255www.sfml-dev.org/ip-provider.php˜6@Â6@Â6@Â6@Â6@Â6@Â6@Â6@Â6@Â6@Â6@Â6@Â6@Â6@Â6@Â6@Â6@´6@Ÿ6@¦6@Â6@Â6@»6@Â6@Â6@­6@vector::_M_fill_insert/GETPOSTHEAD HTTP/. : http/http://https://Fromuser@sfml-dev.orgUser-Agentlibsfml-network/2.xHostContent-LengthContent-Typeapplication/x-www-form-urlencodedConnectionclose$tI

Some paragraph here was my example <p> text, and is not actually another paragraph of relative information. AnywayI don't know where this content comes from, but I assume, somehow, SFML added this on, although I thought the error log normally redirected to the console, (like with a 0x0 texture). So I am pretty lost, but I don't think this is my doing. Anyway, my guess is, this message gets tacked in there with any HTTP/HTML, and so the browser is rather confused, and so making a proper server is a bit impossible till I fix this giberish. All I can assume is that it is an SFML sockets issue.

Not quite sure how to correctly format code in here, sorry.



int main()
{
    sf::TcpSocket socket;

    sf::TcpListener listener;

    listener.listen(11111);

    while (true)
    {
        if (listener.accept(socket) == sf::Socket::Done)
        {
            std::cout << "Connection!\n";

            socket.setBlocking(true);

            char buffer[1000];

            sf::IpAddress other;

            std::size_t t;

            socket.receive(buffer, sizeof(buffer), t);

            std::cout << buffer;

            socket.send("HTTP 1.1 200 OK\n\rConnection: close"
                              "\n\r\n\r"
                              "<!DOCTYPE html><html><head><title>TestPage</title>"
                              "</head><body style = \"background-color: lightblue;\" >"
                              "<p>Some paragraph here...</p></body></html>\r\n"
                              , 1000);
        }

        sf::sleep(sf::seconds(0.01));

    }

    std::cin.get();
}


This with including SFML/Network and iostream, as well as linking with the SFML libs. I used Firefox and typed in the URL bar my external ip followed by :11111 (this rather than 80). I don't think it is a browser issue, though, because chrome displays about the same thing, although is gets the style = "background-color: lightblue" part correct as well.


Thanks if you can help.

EDIT: SFML2-rc from... a few months back. Might be useful information.
« Last Edit: January 14, 2013, 07:51:18 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Odd Error with TcpSocket and HTTP
« Reply #1 on: January 14, 2013, 07:53:57 am »
You must pass the exact size of the text to the send function, not 1000. With this code, the server actually sends 1000 characters, which includes your HTTP string plus whatever is in memory next to it.
Laurent Gomila - SFML developer

chessguy

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Odd Error with TcpSocket and HTTP
« Reply #2 on: January 14, 2013, 06:52:52 pm »
I thought about that once, but for some reason I assumed that it was a safe operation.

Although... if that is true, what do you think the contents next to it are from? I have never seen the TCP_NODELAY message before now. Perhaps this same program always gets that memory space? Not sure.

It seems to be fixed now. I decided it would be easier to use a string (previously I did 1000 because I would need a variable for the string to get the size somehow...). However, browsers load - or don't load - this for a very long time. I am not sure whether I am sending information inaccurately still, or it is someHTTP thing. I have:

std::string string = "HTTP/1.1 200 OK\n\rConnection: close\n\r\n\r<html><head><title>Title</title></head><body style = \"background-color: lightblue;\"><p style = \"background-color:black; color: yellow;\">Test</p></body></html>";

            for (int i = 0; i < string.length(); ++i)
                std::cout << string;

            std::cout << "\n\n" << string.length() + 1 << "\n\n";

            socket.send(string.c_str(), string.length() + 1);

I think length + 1 is correct here (c_string adds \0)?

Anyway, it "loads" for long time - least a minute. Chrome and Firefox. Sometimes it displays as it should, and other times it times out. Any ideas?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Odd Error with TcpSocket and HTTP
« Reply #3 on: January 14, 2013, 09:28:57 pm »
Quote
Although... if that is true, what do you think the contents next to it are from?
All literal strings are stored in the same area of the executable, so what you see are hard-coded strings that appear in SFML source code.

Quote
            for (int i = 0; i < string.length(); ++i)
                std::cout << string;
Why do you print the string as many times as there are characters in it?
Laurent Gomila - SFML developer

chessguy

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Odd Error with TcpSocket and HTTP
« Reply #4 on: January 15, 2013, 06:32:53 pm »
Oh. That makes sense.

I am not sure. I believe I meant to have string there - I was trying to see what was contained in the string, and... only send what was expected.

At what point would SFML output an error or description of TCP_NODELAY, and what does it mean?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Odd Error with TcpSocket and HTTP
« Reply #5 on: January 15, 2013, 06:43:42 pm »
Quote
At what point would SFML output an error or description of TCP_NODELAY, and what does it mean?
Google TCP_NODELAY if you're interested. This is really out of topic, and I'm not going to explain here every possible error that SFML can output anyway ;)
Laurent Gomila - SFML developer