English / Japanese

gimite/socket


Overview

C++ library which performs communication using TCP/IP and UDP/IP. It supports Windows (using Winsock2) and UNIX (using POSIX Socket).


Download

gimite/socket.h Ver.1.5


Notes

  1. Some Windows headers includes Winsock.h internally, which causes compiler error. In this case, including Winsock2.h before the header will do the trick.
  2. GCC 2.9x or before is not supported because interface of streambuf is incompatible.


Examples

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;
}


Global functions

bool startup_socket();

void cleanup_socket();


socket_stream

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 :)


server_stream_socket

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;


datagram_socket

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;


ip_address

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;


socket_address

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;


Typedefs

Type name
Description
Windows
UNIX
socket_t Type of socket handle. SOCKET int
sock_uint32_t 32-bit unsigned integer which represents IP address. unsigned long u_int32_t


History

Ver.1.5 2007/4/5 Renamed diagram_socket to datagram_socket.
Ver.1.4 2004/11/3 Added comparision, equality and output to ostream to ip_address and socket_address.
Ver.1.3 2004/9/21 Fixed a bug that receiving '\xff' is interpreted as EOF in some cases.
Ver.1.2 2004/2/15 Added socket_stream::send() and socket_stream::recv().
Ver.1.1 2003/11/28 Renamed server_socket to server_stream_socket and modified API.
Added diagram_socket.
Introduced ip_address class which replaced std::string in host name parameter.
Tuned socket_stream.
Ver.1.0 2003/10/12 Published.


Contacts

Please send bug reports, comments, etc. to ***
I confirmed that this library works with BCB6 Pro, VC.net Std, VC.net 2003 Pro, GCC 3.3 (Cygwin, FreeBSD).

Gimite