コード例 #1
0
ファイル: udp_sender.cpp プロジェクト: CaiGuaiNi/Socket
int main(void)
{
    try
    {
        Socket::UDP sock;
        Socket::Address to("127.0.0.1", 10000);
        
        sock.send<string>(to, "this is the string"); // ("127.0.0.1", 10000, "this is a string");
                                                     // as well as the others
        
        int iarr[5] = { 0, 1, 2, 3, 4 };
        sock.send<int>(to, iarr, 5);
        
        sock.send<float>(to, 5.0);
        
        double darr[5] = { 0.0, 1.0, 2.0, 3.0, 4.0 };
        sock.send<double>(to, darr, 5);
        
        vector<prova> vec;
        for (int i = 0; i < 5; i++) vec.push_back( { i, i + 1 });
        sock.send<prova>(to, vec);

        sock.close();
    }
    catch (Socket::SocketException &e)
    {
        cout << e << endl;
    }

    return 0;
}
コード例 #2
0
ファイル: myclient.cpp プロジェクト: Strider94/Socket
int main(void)
{
    try
    {
        Socket::UDP sock;
        
        sock.listen_on_port(3000);
        
        sock.send("127.0.0.1", 2000, "request");
        
        Socket::Datagram received = sock.receive();
        
        cout << received.data << endl;
        
        sock.close();
    }
    catch (Socket::Exception &e)
    {
        cout << e.what() << endl;
    }
    
    return 0;
}
コード例 #3
0
ファイル: udp_receiver.cpp プロジェクト: CaiGuaiNi/Socket
int main(void)
{
    try
    {
        Socket::UDP sock;
        double buffer[SOCKET_MAX_BUFFER_LEN];
        int i;

        sock.listen_on_port(10000);
        
        Socket::Datagram<string>            rec_str = sock.receive<string>();
        Socket::Datagram<int[5]>            rec_arr = sock.receive<int, 5>(); // ([, 5]);
        Socket::Datagram<float>             rec_var = sock.receive<float>();
        Socket::Datagram<double*>           rec_pnt = sock.receive<double>(buffer); // (buffer [, SOCKET_MAX_BUFFER_LEN]);
        Socket::Datagram<vector<prova> >    rec_vec = sock.receive<prova>(5); // conflict with the first one, must be specified

        cout << rec_str.data << endl;
        cout << endl;
        for (i = 0; i < 5; i++) cout << rec_arr.data[i] << endl;
        cout << endl;
        cout << rec_var.data << endl;
        cout << endl;
        for (i = 0; i < rec_pnt.received_elements; i++) cout << rec_pnt.data[i] << endl;
        cout << endl;
        for (i = 0; i < rec_vec.data.size(); i++) cout << rec_vec.data[i].something << " - " << rec_vec.data[i].somethingelse << endl;
        cout << endl;
        cout << "from: " << rec_vec.address << endl; // I know could be always different, just for the sake of simplicity

        sock.close();
    }
    catch (Socket::SocketException &e)
    {
        cout << e << endl;
    }
    
    return 0;
}
コード例 #4
0
ファイル: udp_receiver.cpp プロジェクト: zhenglinj/Common
int main(void)
{
    char* buffer = new char[SOCKET_MAX_BUFFER_BYTES];

    try
    {
        Socket::UDP sock;
        sock.open();

        unsigned int i;
        int buffer_size = SOCKET_MAX_BUFFER_BYTES / sizeof(double);

        // sock.set_option(SOL_SOCKET, SO_SNDBUF, (const char*)&buffer_size, sizeof(char)); // throw ERROR
        sock.set_option(SOL_SOCKET, SO_SNDBUF, (const char*)&buffer_size, sizeof(buffer_size));
        sock.set_dontfragment(true);
        sock.set_broadcast(true);
        sock.bind_on_port(10000);

        // Socket::Datagram<string>            rec_str = sock.receive<string>();
        Socket::Datagram<string>            rec_str;
        do
        {
            rec_str = sock.receive_timeout<string>(1000);
            if (rec_str.received_bytes > 0)
                break;
            else
                cout << "receive timeout" << endl;
        }
        while (1);
        cout << rec_str.data << endl;
        cout << endl;


        Socket::Datagram<int[5]>            rec_arr = sock.receive<int, 5>(); // ([, 5]);
        for (i = 0; i < 5; i++)
            cout << rec_arr.data[i] << " ";
        cout << endl << endl;


        Socket::Datagram<float>             rec_var = sock.receive<float>();
        cout << rec_var.data << endl;
        cout << endl;


        // (buffer [, SOCKET_MAX_BUFFER_BYTES]);
        Socket::Datagram<double*>           rec_pnt = sock.receive<double>((double*)buffer);
        for (i = 0; i < rec_pnt.received_elements; i++)
            cout << rec_pnt.data[i] << " ";
        cout << endl;


        Socket::Datagram<prova*>            rec_stct = sock.receive<prova>((prova*)buffer);
        cout << "struct prova: {" << rec_stct.data->something << ", " << rec_stct.data->somethingelse << "}" << endl;


        sock.close();
    }
    catch (Socket::SocketException &e)
    {
        int lasterror;
        string err_msg;
        if ((lasterror = e.get_error(err_msg)) > 0)
            cout  << "lasterror = " << lasterror << ", " << err_msg << endl;
        cout << e.what() << endl;
    }

    if (buffer)
    {
        delete[] buffer;
        buffer = nullptr;
    }

    return 0;
}