一个c++ socket 客户端库
http://www.rudeserver.com/socket/index.html
The RudeSocket™ Open Source C++ Socket Library provides a simple to use interface for creating and using client sockets. You can connect to the destination server through an unlimited number of chainable proxies, SOCKS4 and SOCKS5 servers if anonymity or security is a priority. Supports SSL [1] as well as normal connections. Supports timeouts. Full version requires that openSSL libraries are installed. However, a lite version is available if SSL is not required or available.
The library is currently available for linux development environments.
Features:
[1] - This product includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit. (http://www.openssl.org/)
General Usage
Socket *socket = new Socket();
socket->connect("google.com", 80);
socket->sends("GET / HTTP/1.0\n\n");
const char *response = socket->reads();
cout << response;
socket->close();
SSL Usage
Socket *socket = new Socket();
socket->connectSSL("google.com", 443);
socket->sends("GET / HTTP/1.0\n\n");
const char *response = socket->reads();
cout << response;
socket->close();
Chaining Connections
Socket *socket = new Socket();
socket->insertSocks4("12.34.56.78", 8000, "username");
socket->insertSocks5("12.34.56.78", 8000, "username", "password");
socket->insertProxy("12.34.56.78", 8080);
socket->connectSSL("google.com", 443);
socket->sends("GET / HTTP/1.0\n\n");
const char *response = socket->reads();
cout << response;
socket->close();
Adding Error checking
Socket *socket = new Socket();
if(socket->connectSSL("google.com", 443))
{
if(socket->sends("GET / HTTP/1.0\n\n"))
{
const char *response = socket->reads();
if(response)
{
cout << response;
}
else
{
cout << socket->getError() << "\n";
}
}
else
{
cout << socket->getError() << "\n";
}
socket->close();
}
else
{
cout << socket->getError() << "\n";
}
Constructor Summary | |
Socket() Constructor |
|
~Socket() Destructor |
Method Summary | |
bool |
close() Closes the connection |
bool |
connect( const char* server, int port ) Connects to the specified server and port |
bool |
connectSSL( const char* server, int port ) Connects to the specified server and port over a secure connection |
const char* |
getError() Returns a description of the last known error |
bool |
insertProxy( const char* server, int port ) Inserts a CONNECT-Enabled HTTP proxy into the connect chain |
bool |
insertSocks4( const char* server, int port, const char* username ) Inserts a Socks4 server into the connect chain |
bool |
insertSocks5( const char* server, int port, const char* username, const char* password ) Inserts a Socks5 server into the connect chain |
bool |
insertTunnel( const char* server, int port ) Inserts a transparent tunnel into the connect chain |
int |
read( char* buffer, int length ) Reads a buffer of data from the connection |
const char* |
readline() Reads a line from the connection |
const char* |
reads() Reads everything available from the connection |
int |
send( const char* data, int length ) Sends a buffer of data over the connection |
bool |
sends( const char* buffer ) Sends a null terminated string over the connection |
void |
setMessageStream( std::ostream& o ) Sets an output stream to receive realtime messages about the socket |
void |
setTimeout( int seconds, int microseconds ) Sets the timeout value for Connect, Read and Send operations. |
Constructor Detail |
public Socket();
public ~Socket();
Method Detail |
public bool close();
public bool connect( const char* server, int port );
public bool connectSSL( const char* server, int port );
public const char* getError();
public bool insertProxy( const char* server, int port );
public bool insertSocks4( const char* server, int port, const char* username );
public bool insertSocks5( const char* server, int port, const char* username, const char* password );
public bool insertTunnel( const char* server, int port );
public int read( char* buffer, int length );
public const char* readline();
public const char* reads();
public int send( const char* data, int length );
public bool sends( const char* buffer );
public void setMessageStream( std::ostream& o );
public void setTimeout( int seconds, int microseconds );
官方原版源码下载:点击下载
删除socket_platform.h文件包含 #include <winsock2.h> 的代码,以防止重写义的问题