Dlib库【2】——Socket相关
来源:互联网 发布:php分割 编辑:程序博客网 时间:2024/06/16 17:08
配置Dlib环境: 链接
1.增强本IO_Socket
从www.baidu.com 的端口80获取消息,打印返回消息
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt/*This is an example illustrating the use of the iosockstream object from thedlib C++ Library.This program simply connects to www.baidu.com at port 80 and requests themain baidu web page. It then prints what it gets back from baidu to thescreen.For those of you curious about HTTP check out the excellent introduction athttp://www.jmarshall.com/easy/http/*/#include <dlib/iosockstream.h>#include <iostream>using namespace std;using namespace dlib;int main(){try{// Connect to baidu's web server which listens on port 80. If this// fails it will throw a dlib::socket_error exception. iosockstream stream("www.baidu.com:80");// At this point, we can use stream the same way we would use any other// C++ iostream object. So to test it out, let's make a HTTP GET request// for the main baidu page.stream << "GET / HTTP/1.0\r\n\r\n";// Here we print each character we get back one at a time. while (stream.peek() != EOF){cout << (char)stream.get();}system("pause");}catch (exception& e){cout << e.what() << endl;}}
2.服务器部分:1234端口向客户端发送接受到的东西
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt/*This is an example illustrating the use of the sockets andserver components from the dlib C++ Library.This is a simple echo server. It listens on port 1234 for incomingconnections and just echos back any data it receives.*/#include <dlib/sockets.h>#include <dlib/server.h>#include <iostream>using namespace dlib;using namespace std;class serv : public server{void on_connect(connection& con){char ch;while (con.read(&ch, 1) > 0){// we are just reading one char at a time and writing it back// to the connection. If there is some problem writing the char// then we quit the loop. if (con.write(&ch, 1) != 1)break;}}};int main(){try{serv our_server;// set up the server object we have madeour_server.set_listening_port(1234);// Tell the server to begin accepting connections.our_server.start_async();cout << "Press enter to end this program" << endl;cin.get();}catch (exception& e){cout << e.what() << endl;}}
3.增强版服务器
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt/* This is an example illustrating the use of the server_iostream object from the dlib C++ Library. This is a simple echo server. It listens on port 1234 for incoming connections and just echos back any text it receives, but in upper case. So basically it is the same as the sockets_ex.cpp example program except it uses iostreams. To test it out you can just open a command prompt and type: telnet localhost 1234 Then you can type away.*/#include <dlib/server.h>#include <iostream>using namespace dlib;using namespace std;class serv : public server_iostream{ void on_connect ( std::istream& in, std::ostream& out, const std::string& foreign_ip, const std::string& local_ip, unsigned short foreign_port, unsigned short local_port, uint64 connection_id ) { // The details of the connection are contained in the last few arguments to // on_connect(). For more information, see the documentation for the // server_iostream. However, the main arguments of interest are the two streams. // Here we also print the IP address of the remote machine. cout << "Got a connection from " << foreign_ip << endl; // Loop until we hit the end of the stream. This happens when the connection // terminates. while (in.peek() != EOF) { // get the next character from the client char ch = in.get(); // now echo it back to them out << (char)toupper(ch); } }};int main(){ try { serv our_server; // set up the server object we have made our_server.set_listening_port(1234); // Tell the server to begin accepting connections. our_server.start_async(); cout << "Press enter to end this program" << endl; cin.get(); } catch (exception& e) { cout << e.what() << endl; }}
4.String buffer形式的IO_socket
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt/*This is an example illustrating the use of the sockets and sockstreambufcomponents from the dlib C++ Library. Note that there is also aniosockstream object in dlib that is often simpler to use, seeiosockstream_ex.cpp for an example of its use.This program simply connects to www.google.com at port 80 and requests themain Google web page. It then prints what it gets back from Google to thescreen.For those of you curious about HTTP check out the excellent introduction athttp://www.jmarshall.com/easy/http/*/#include <dlib/sockets.h>#include <dlib/sockstreambuf.h>#include <iostream>using namespace std;using namespace dlib;int main(){try{// 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.scoped_ptr<connection> con(connect("www.baidu.com", 80));{// Create a stream buffer for our connectionsockstreambuf buf(con);// Now stick that stream buffer into an iostream objectiostream stream(&buf);// This command causes the iostream to flush its output buffers// whenever someone makes a read request. buf.flush_output_on_read();// 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.}// 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. // // Also note that the timeout can be changed by suppling an optional argument // to this function.close_gracefully(con);system("pause");}catch (exception& e){cout << e.what() << endl;}}
阅读全文
0 0
- Dlib库【2】——Socket相关
- Dlib库【7】——物体追踪
- Dlib库【8】——多层神经网络
- Dlib相关问题
- dlib相关问题
- 【机器学习】Dlib——机器学习库学习系列
- Dlib库【3】——Thread线程,数据统计
- Dlib库【4】——定时器Timer,线程对象
- Dlib库【10】——计算积分,读取xml显示
- Dlib库【9】——Surf特征点
- 用anaconda安装dlib库(python—opencv)
- dlib——机器学习工具箱
- Dlib库【1】——3D点云显示(鼠标控制)
- Dlib库【5】——分配任务,遍历目录,队列queue的用法
- Dlib库【6】——内存管理,成员功能指针,Logger(记录)
- Dlib C++ 算法库
- dlib库使用
- 如何使用dlib库
- BZOJ 1061: [Noi2008]志愿者招募 费用流
- Cxf拦截器中获取当前请求的参数
- java 反射机制
- console.log()打印object却显示为字符串[object Object]
- java simpleMailMessage 邮件发送功能
- Dlib库【2】——Socket相关
- android各版本特性
- 小技巧:两种方式快速实现平滑涂鸦画板
- php数据库操作——新数据插入到MySQL
- Unity 学习笔记 2
- JAVA实现文件下载,浏览器端得到数据流没生产文件
- 通达OA 工作流执行出现的异常现象处理(图文)
- Java关于Properties用法的总结(一)
- 关于C++兼容C,C++在linux程序设计API的调用等问题