CLX C++ Libraries
Home >> udp::socket

Declaration

namespace udp {
    template <int Family>
    class basic_socket : basic_rawsocket<SOCK_DGRAM, Family>;
    
    typedef basic_socket<AF_INET> socket;
    typedef basic_sockaddress<AF_INET, IPPROTO_UDP> sockaddress;
    typedef basic_sockstream<socket> sockstream;
};

Overview

udp::socket は,UDP 用ソケットのラッパクラスです.UDP によるデータ通信においても, sockstream を利用することができます.UDP ソケットを用いたときは,以下のような動作を行います.

Example

Sender

example_udp_send.cpp

#include <iostream>
#include <string>
#include "clx/udp.h"

int main(int argc, char* argv[]) {
    if (argc < 3) return -1;
    clx::udp::sockstream s(argv[1], argv[2]);
    
    std::string buf;
    while (std::getline(std::cin, buf)) {
        s << buf << std::endl;
    }
    
    return 0;
}

Receiver

example_udp_recv.cpp

#include <iostream>
#include <string>
#include "clx/udp.h"

int main(int argc, char* argv[]) {
    if (argc < 2) return -1;
    clx::udp::sockstream ss(argv[1]);
    
    std::string buf;
    while (std::getline(ss, buf)) {
        std::cout << buf << " (from " << ss.from().ipaddr()
            << ':' << ss.from().port() << ')' << std::endl;
    }
    
    return 0;
}

Template Parameters

Family
プロトコルファミリーを指定します.

Related Types

typedef basic_rawsocket<SOCK_DGRAM, Family> rawsocket;
typedef basic_sockaddress<Family, IPPROTO_UDP> sockaddress;
typedef char char_type;
typedef std::basic_string<char_type> string_type;

Construction and Member Functions

basic_socket();
basic_socket(const basic_socket& cp);
explicit basic_socket(socket_int s, const sockaddress& addr);
explicit basic_socket(const char_type* host, int port);
explicit basic_socket(const string_type& host, int port);
explicit basic_socket(const char_type* host, const char_type* service);
explicit basic_socket(const string_type& host, const string_type& service);
explicit basic_socket(int port);
explicit basic_socket(const char_type* service);
explicit basic_socket(const string_type& service);
virtual ~basic_socket();

int bind(int port);
int bind(const char_type* service);
int bind(const string_type& service);
bool is_bind() const;

int send_to(const char_type* src, const int n, sockaddress& addr);
int send_to(const string_type& src, const sockaddress& addr);
int send_to(const char_type* src, int n, const char_type* host, int port);
int send_to(const string_type& src, const string_type& host, int port);
int send(const char_type* src, int n);
int send(const string_type& src);
int recv(char_type* dest, int n);

const sockaddress& from() const;
const sockaddress& to() const;