2009-02-17 09:45:57 +08:00
|
|
|
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
|
2008-05-02 22:19:38 +08:00
|
|
|
/*
|
|
|
|
|
2012-11-18 03:48:42 +08:00
|
|
|
This is an example illustrating the use of the sockets and sockstreambuf
|
|
|
|
components from the dlib C++ Library. Note that there is also an
|
|
|
|
iosockstream object in dlib that is often simpler to use, see
|
|
|
|
iosockstream_ex.cpp for an example of its use.
|
2008-05-02 22:19:38 +08:00
|
|
|
|
2012-11-18 03:48:42 +08:00
|
|
|
This program simply connects to www.google.com at port 80 and requests the
|
|
|
|
main Google web page. It then prints what it gets back from Google to the
|
|
|
|
screen.
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
|
2012-11-18 03:48:42 +08:00
|
|
|
For those of you curious about HTTP check out the excellent introduction at
|
|
|
|
http://www.jmarshall.com/easy/http/
|
2008-05-02 22:19:38 +08:00
|
|
|
*/
|
|
|
|
|
2017-05-15 07:52:34 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
|
2012-12-08 22:32:13 +08:00
|
|
|
#include <dlib/sockets.h>
|
|
|
|
#include <dlib/sockstreambuf.h>
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
using namespace std;
|
2012-11-18 03:48:42 +08:00
|
|
|
using namespace dlib;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
2012-11-18 03:48:42 +08:00
|
|
|
int main()
|
2008-05-02 22:19:38 +08:00
|
|
|
{
|
2012-11-18 03:48:42 +08:00
|
|
|
try
|
2008-05-02 22:19:38 +08:00
|
|
|
{
|
2012-11-18 03:48:42 +08:00
|
|
|
// Connect to Google's web server which listens on port 80. If this
|
|
|
|
// fails it will throw a dlib::socket_error exception. Note that we
|
|
|
|
// are using a smart pointer here to contain the connection pointer
|
|
|
|
// returned from connect. Doing this ensures that the connection
|
|
|
|
// is deleted even if someone throws an exception somewhere in your code.
|
2017-05-15 07:52:34 +08:00
|
|
|
std::unique_ptr<connection> con(connect("www.google.com",80));
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
{
|
2012-11-18 03:48:42 +08:00
|
|
|
// Create a stream buffer for our connection
|
|
|
|
sockstreambuf buf(con);
|
|
|
|
// Now stick that stream buffer into an iostream object
|
|
|
|
iostream stream(&buf);
|
|
|
|
// This command causes the iostream to flush its output buffers
|
|
|
|
// whenever someone makes a read request.
|
2014-10-01 07:21:38 +08:00
|
|
|
buf.flush_output_on_read();
|
2012-11-18 03:48:42 +08:00
|
|
|
|
|
|
|
// Now we make the HTTP GET request for the main Google page.
|
|
|
|
stream << "GET / HTTP/1.0\r\n\r\n";
|
|
|
|
|
|
|
|
// Here we print each character we get back one at a time.
|
|
|
|
int ch = stream.get();
|
|
|
|
while (ch != EOF)
|
|
|
|
{
|
|
|
|
cout << (char)ch;
|
|
|
|
ch = stream.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// At the end of this scope buf will be destructed and flush
|
|
|
|
// anything it still contains to the connection. Thus putting
|
|
|
|
// this } here makes it safe to destroy the connection later on.
|
|
|
|
// If we just destroyed the connection before buf was destructed
|
|
|
|
// then buf might try to flush its data to a closed connection
|
|
|
|
// which would be an error.
|
2008-05-02 22:19:38 +08:00
|
|
|
}
|
|
|
|
|
2012-11-18 03:48:42 +08:00
|
|
|
// Here we call close_gracefully(). It takes a connection and performs
|
|
|
|
// a proper TCP shutdown by sending a FIN packet to the other end of the
|
|
|
|
// connection and waiting half a second for the other end to close the
|
|
|
|
// connection as well. If half a second goes by without the other end
|
|
|
|
// responding then the connection is forcefully shutdown and deleted.
|
|
|
|
//
|
|
|
|
// You usually want to perform a graceful shutdown of your TCP connections
|
|
|
|
// because there might be some data you tried to send that is still buffered
|
|
|
|
// in the operating system's output buffers. If you just killed the
|
|
|
|
// connection it might not be sent to the other side (although maybe
|
|
|
|
// you don't care, and in the case of this example it doesn't really matter.
|
|
|
|
// But I'm only putting this here for the purpose of illustration :-).
|
|
|
|
// In any case, this function is provided to allow you to perform a graceful
|
|
|
|
// close if you so choose.
|
|
|
|
//
|
2023-06-12 08:21:05 +08:00
|
|
|
// Also note that the timeout can be changed by supplying an optional argument
|
2012-11-18 03:48:42 +08:00
|
|
|
// to this function.
|
|
|
|
close_gracefully(con);
|
2008-05-02 22:19:38 +08:00
|
|
|
}
|
|
|
|
catch (exception& e)
|
|
|
|
{
|
|
|
|
cout << e.what() << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|