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

Author Topic: HTML etc crashes program when no internet connection present  (Read 14506 times)

0 Members and 1 Guest are viewing this topic.

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: HTML etc crashes program when no internet connection present
« Reply #15 on: May 02, 2012, 11:59:17 pm »
Problem is, I need a website I know won't be down, so it's a bit of a catch there.

Try http://www.google.com/  ;D

Google gives 302 (redirect) and on following said redirect, returns 404 (page not found).

I suppose I mean more of a way the computer can connect to itself via the internet (but not locally) so it can check - rather than relying on a website to check - as I know the computer will be running.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: HTML etc crashes program when no internet connection present
« Reply #16 on: May 03, 2012, 12:06:35 am »
There's not such thing in SFML and it's pretty OS specific (if I've understood you right).

As for google, I didn't meant you should connect with the HTTP class but just try to establish a connection and if the server responses you know the internet works or better you ping a fixed IP since the DNS server could be non functional either.  ;D

Btw: Can you please edit the title from 'HTML' to 'HTTP' or better 'sf:Http'?
« Last Edit: May 03, 2012, 12:10:59 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: HTML etc crashes program when no internet connection present
« Reply #17 on: May 03, 2012, 12:08:06 am »
Whilst on the issue of networking, Laurent, I'm getting some strange behaviour when trying to connect to the yahoo mailing server when using the TCP socket.

When the socket is set to blocking mode, the connection is established, and it works fine.

However, when I set the socket to non-blocking mode (I need to be able to do this as otherwise the socket would stop the program), and run it in a loop, it never connects.

You can pretty much put the code as this (just comment out the one you want to test):
Code: [Select]
sf::SocketTCP Test;

//Test.SetBlocking(false); while( !(Test.Connect(25,"smtp.mail.yahoo.com") == sf::Socket::Done) );
//Test.Connect(25,"smtp.mail.yahoo.com");
printf("Connected!\n");

And the non-blocking one won't ever connect. Why is that?

Don't be fooled by the equality operator - it's bracketed and negated.
« Last Edit: May 03, 2012, 12:14:02 am by Joshua Flynn »

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: HTML etc crashes program when no internet connection present
« Reply #18 on: May 03, 2012, 12:13:12 am »
There's not such thing in SFML and it's pretty OS specific (if I've understood you right).

As for google, I didn't meant you should connect with the HTTP class but just try to establish a connection and if the server responses you know the internet works or better you ping a fixed IP since the DNS server could be non functional either.  ;D

I'll have to get the TCP non-blocking non-connection issue solved before I can do that (otherwise, if I try to use blocking and connect when the internet is down, the program will freeze). Ping is a system command and I don't think it'd give me much in the way of feedback in C++.

It should be possible to 'connect to yourself'. Assuming you set up a socket to connect to your external IP and an open port, you probably could do that?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: HTML etc crashes program when no internet connection present
« Reply #19 on: May 03, 2012, 08:12:33 am »
The non-working non-blocking connect is a known issue ;)
Laurent Gomila - SFML developer

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: HTML etc crashes program when no internet connection present
« Reply #20 on: May 03, 2012, 11:48:43 pm »
The non-working non-blocking connect is a known issue ;)

Argh! Okay, I've got the source code with the build projects for code::blocks, does it build the .DLL for the network in the lib folder? If so, I'll see if I can write a fixed version of the TCPSocket and run that.

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: HTML etc crashes program when no internet connection present
« Reply #21 on: May 04, 2012, 01:05:57 am »
Actually, I am confused.

In the Connect function, regardless of whether or not the socket is in blocking mode (block until something happens), or non-blocking mode (return as soon as possible), you have a timeout value.

Surely, if a timeout value is specified, the socket, by default, is non-blocking (it doesn't wait for something to happen)? Because it will quit out after a given time? If select() is passed a NULL pointer for it's time-value, then it's treated as blocking. So surely, you'd want to re-write the Connect function, so that the time-out value only applies to non-blocking sockets?

This would solve both issues.



I took a further look at the code and after trying it, I found the key reason that the non-blocking socket won't connect is because when the socket is already connected, it returns an error. You could easily solve this by including:

Code: [Select]
if(Status == Socket::Error)
    {
        if(WSAGetLastError() == WSAEISCONN)
        {
            return (Status = Socket::Done);
        }
    }

Because if the purpose of the function is to connect, then it should return true if there is already a connection. I found on the first pass, select didn't catch it (default timeout value), on the second pass a 10037 (operation already in progress), and finally an 10056 (connection already established), which SFML should return not as an error, but a success (as the functions role is to connect - if it's already connected, it's succeeded).

The above function should be placed between:
Code: [Select]
Socket::Status Status = SocketHelper::GetErrorStatus();

//Here

// If we were in non-blocking mode, return immediatly
if (!IsBlocking)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: HTML etc crashes program when no internet connection present
« Reply #22 on: May 04, 2012, 08:06:51 am »
Thanks for helping :)

Feel free to submit a patch, or at least to add a link to your message in the corresponding tracker issue.
Laurent Gomila - SFML developer

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: HTML etc crashes program when no internet connection present
« Reply #23 on: May 04, 2012, 04:41:47 pm »
Thanks for helping :)

Feel free to submit a patch, or at least to add a link to your message in the corresponding tracker issue.

I will do that, although I don't know where/how to submit a patch.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: HTML etc crashes program when no internet connection present
« Reply #24 on: May 04, 2012, 04:52:56 pm »
You can fork on github and submit a pull request, you can clone the repository and convert your modifications to a patch, or even just rewrite the function and sending the code to me.
Laurent Gomila - SFML developer

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: HTML etc crashes program when no internet connection present
« Reply #25 on: May 14, 2012, 10:54:12 pm »
Is there a way to inquire with a server to check to see if there is data waiting to be received?h

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: HTML etc crashes program when no internet connection present
« Reply #26 on: May 14, 2012, 11:05:57 pm »
Could you elaborate? What is the problem that you're trying to solve?
Laurent Gomila - SFML developer

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: HTML etc crashes program when no internet connection present
« Reply #27 on: May 14, 2012, 11:13:40 pm »
Well, you know when you do fgetc, it returns EOF when there's no additional data to be read from the file?

Is there anything similar for recv? The problem is, if the socket is blocking (and I can't use non-blocking), if recv goes 'one byte over', it blocks. So I want to know if there is another byte in the data stream to receive, before I attempt to get it. Is there a function to poll the data on the stream/buffer?

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: HTML etc crashes program when no internet connection present
« Reply #28 on: May 14, 2012, 11:46:02 pm »
If you want to know what I'm intending, I need to read all the information given by an IMAP server from an email of an unknown length (which, with attachments, could easily run into many megabytes and thus make static buffers a bad idea), but I read it in such a way receive won't block (I have no idea what indicates the end of the received data).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: HTML etc crashes program when no internet connection present
« Reply #29 on: May 15, 2012, 08:05:02 am »
There are different approaches (depending on how your program works):
- non-blocking sockets (why can't you use them?)
- stop to wait and consider the data complete after a given timeout
- finding how the protocol encodes the end of data -- I'm sure it does

Explicitely encoding the end of data (or the total size to receive) is the only reliable way to know when to stop receiving with TCP (if the server doesn't close the connection when finished, like with HTTP), so almost all protocols do it.
Laurent Gomila - SFML developer

 

anything