C++ library which performs communication using TCP/IP and UDP/IP. It supports Windows (using Winsock2) and UNIX (using POSIX Socket).
socket_stream enables you to communicate with stream socket using C++ iostream.
// Example 1: Simple HTTP client
#include <iostream>
#include <gimite/socket.h>
int main()
{
// Call this before using any classes in this library.
gimite::startup_socket();
{
// Connects to host www.google.com port 80 (HTTP).
gimite::socket_stream sst("www.google.com", 80);
// Checks connection error.
if (!sst){
std::cerr << "Failed to connect." << std::endl;
}else{
// Sends string.
sst << "GET / HTTP/1.0\r\n\r\n";
std::string s;
// Receives each line and shows it.
while (std::getline(sst, s))
std::cout << s << std::endl;
}
}
// Call this after all use of this library has finished.
gimite::cleanup_socket();
return 0;
}
server_stream_socket is a wrapper of server side stream socket.
// Example 2: Echo server
#include <iostream>
#include <gimite/socket.h>
int main()
{
// Call this before using any classes in this library.
gimite::startup_socket();
{
// Starts listening to client connection at port 12345.
gimite::server_stream_socket server(12345);
// Checks binding error.
if (!server){
std::cerr << "Failed to bind socket." << std::endl;
}else{
while (true){
// Waits for client connection, and returns socket handle for connection with first client.
gimite::socket_t s= server.accept();
// Creates socket_stream for communication with the client.
gimite::socket_stream sst(s);
// Just echoes back string received.
char c;
while (sst.get(c))
sst << c;
}
}
}
// Call this after all use of this library has finished.
gimite::cleanup_socket();
return 0;
}
datagram_socket wraps datagram socket.
// Example 3: UDP communication (server side)
#include <iostream>
#include <gimite/socket.h>
int main()
{
// Call this before using any classes in this library.
gimite::startup_socket();
{
gimite::socket_address saddr;
// Creates socket at port 12345.
gimite::datagram_socket dsock(12345);
char cs[5];
while (true){
// Receives 5 bytes packet. saddr points to sender address.
dsock.recvfrom(cs, 5, &saddr);
// Shows received string.
std::cout << "Message from client: " << cs << std::endl;
// Sends reply packet "pong\0".
dsock.sendto("pong", 5, &saddr);
}
}
// Call this after all use of this library has finished.
gimite::cleanup_socket();
return 0;
}
// Example 4F UDP connection (client side)
#include <iostream>
#include <gimite/socket.h>
int main()
{
// Call this before using any classes in this library.
gimite::startup_socket();
{
// Target is host localhost port 12345.
gimite::socket_address saddr("localhost", 12345);
// Creates socket. Port number is automatically allocated.
gimite::datagram_socket dsock(0);
// Sends packet "ping\0".
dsock.sendto("ping", 5, &saddr);
char cs[5];
// Receives 5 bytes packet.
dsock.recvfrom(cs, 5);
// Shows received string.
@@std::cout << "Message from server: " << cs << std::endl;
@}
@// Call this after all use of this library has finished.
@gimite::cleanup_socket();
@return 0;
}
bool startup_socket();
void cleanup_socket();
Wrapper of stream (TCP) socket. It inherits std::iostream. Use normal input/output method of std::iostream to send/receive data.
socket_stream();
socket_stream(const ip_address& host, int port);
explicit socket_stream(socket_t sock);
virtual ~socket_stream();
void open(const ip_address& host, int port);
void close();
socket_t release();
int send(const void* buffer, int size);
int recv(void* buffer, int size);
socket_t socket()const;
operator void*()const;
bool operator!()const;
*1@I'm not sure this is safe way because socket_stream doesn't care about thread safety :)
Wrapper of server side stream (TCP) socket.
server_stream_socket();
explicit server_stream_socket(int port, int backlog= 5);
server_stream_socket(socket_t sock, bool is_bound);
~server_stream_socket();
bool bind(int port, int backlog= 5);
socket_t accept();
void close();
socket_t release();
void socket(socket_t sock, bool is_bound);
socket_t socket()const;
operator const void*()const;
bool operator!()const;
Wrapper of datagram (UDP) socket.
datagram_socket();
explicit datagram_socket(int port);
datagram_socket(socket_t sock, bool is_bound);
~datagram_socket();
bool bind(int port);
int recvfrom(void* buffer, int size, socket_address* addr= 0, int flags= 0);
int sendto(const void* buffer, int size, const socket_address* addr, int flags= 0);
void close();
socket_t release();
void socket(socket_t sock, bool is_bound);
socket_t socket()const;
operator const void*()const;
bool operator!()const;
Class which represents IP address. You can pass std::string or char* instead wherever ip_address is required (e.g. constructor of socket_stream).
explicit ip_address(sock_uint32_t addr= INADDR_NONE);
ip_address(const std::string& host);
ip_address(const char* host);
ip_address& operator=(const std::string& host);
ip_address& operator=(const char* host);
in_addr addr()const;
sock_uint32_t as_int()const;
Structure which represents a pair of IP address and port number.
explicit socket_address(const ip_address& i= ip_address(), int p= 0);
ip_address ip;
int port;
Type name
|
Description
|
Windows
|
UNIX
|
Ver.1.5 | 2007/4/5 | Renamed diagram_socket to datagram_socket. |
Added diagram_socket. Introduced ip_address class which replaced std::string in host name parameter. Tuned socket_stream. |
||
Gimite