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

Declaration

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

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) std::exit(-1);
    clx::udp::sockstream s(argv[1], clx::udp::port(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) std::exit(-1);
    clx::udp::sockstream ss(clx::udp::port(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_sockaddress<Family, IPPROTO_UDP> address_type;
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 address_type& addr);
explicit basic_socket(const char_type* host, int port);
explicit basic_socket(const string_type& host, int port);
explicit basic_socket(int port);
virtual ~basic_socket();

int bind(int port);
bool is_bind() const;

int send_to(const char_type* src, int n, const address_type& addr);
int send_to(const string_type& src, const address_type& 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 address_type& from() const;
const address_type& to() const;