当前位置: 首页>>代码示例>>C++>>正文


C++ TCPSocket::open方法代码示例

本文整理汇总了C++中TCPSocket::open方法的典型用法代码示例。如果您正苦于以下问题:C++ TCPSocket::open方法的具体用法?C++ TCPSocket::open怎么用?C++ TCPSocket::open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TCPSocket的用法示例。


在下文中一共展示了TCPSocket::open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: TCPSOCKET_BIND_ADDRESS_INVALID

void TCPSOCKET_BIND_ADDRESS_INVALID()
{
    SKIP_IF_TCP_UNSUPPORTED();
    TCPSocket *sock = new TCPSocket;
    if (!sock) {
        TEST_FAIL();
        return;
    }
    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
    nsapi_error_t bind_result = NSAPI_ERROR_OK;
    if (get_ip_version() == NSAPI_IPv4) {
        bind_result = sock->bind("190.2.3.4", 1024);
    } else if (get_ip_version() == NSAPI_IPv6) {
        bind_result = sock->bind("fe80::ff01", 1024);
    } else {
        TEST_FAIL_MESSAGE("This stack is neither IPv4 nor IPv6");
    }
    if (bind_result == NSAPI_ERROR_UNSUPPORTED) {
        TEST_IGNORE_MESSAGE("bind() not supported");
    } else {
        TEST_ASSERT_EQUAL(NSAPI_ERROR_PARAMETER, bind_result);
    }

    delete sock;
}
开发者ID:0xc0170,项目名称:mbed,代码行数:25,代码来源:tcpsocket_bind_address_invalid.cpp

示例2: wifi_http

void wifi_http()
{
    TCPSocket socket;
    int ret;

    ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
    TEST_ASSERT_MESSAGE(ret == 0, "Connect failed");

    // Open a socket on the network interface, and create a TCP connection to www.arm.com
    ret = socket.open(get_wifi());
    TEST_ASSERT_MESSAGE(ret == 0, "Socket open failed");
    ret = socket.connect("www.arm.com", 80);
    TEST_ASSERT_MESSAGE(ret == 0, "Socket connect failed");

    // Send a simple http request
    char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
    int scount = socket.send(sbuffer, sizeof sbuffer);
    TEST_ASSERT_MESSAGE(scount >= 0, "Socket send failed");

    // Recieve a simple http response and check if it's not empty
    char rbuffer[64];
    int rcount = socket.recv(rbuffer, sizeof rbuffer);
    TEST_ASSERT_MESSAGE(rcount >= 0, "Socket recv error");
    TEST_ASSERT_MESSAGE(rcount > 0, "No data received");

    ret = socket.close();
    TEST_ASSERT_MESSAGE(ret == 0, "Socket close failed");

    ret = get_wifi()->disconnect();
    TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed");
}
开发者ID:NXPmicro,项目名称:mbed,代码行数:31,代码来源:main.cpp

示例3: TCPSOCKET_BIND_ADDRESS

void TCPSOCKET_BIND_ADDRESS()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
    int count = fetch_stats();
    for (int j = 0; j < count; j++) {
        TEST_ASSERT_EQUAL(SOCK_CLOSED,  tcp_stats[j].state);
    }
#endif

    TCPSocket *sock = new TCPSocket;
    if (!sock) {
        TEST_FAIL();
    }
    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(get_interface()));
    SocketAddress sockAddr = SocketAddress(get_interface()->get_ip_address(), 80);
    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->bind(sockAddr));

    delete sock;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
    count = fetch_stats();
    for (int j = 0; j < count; j++) {
        TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
    }
#endif
}
开发者ID:MarceloSalazar,项目名称:mbed-os,代码行数:26,代码来源:tcpsocket_bind_address.cpp

示例4: runListener

void runListener(const char * host, int port, const char *)
{
  printf("Setting up a listener socket to %s:%d\n", host, port);

  TCPSocket socket;
  int err;
  if((err = socket.open(host, port)) != 0) {
    printf("%s cannot open client socket to %s:%d -> %s\n", 
	   appname, host, port, strerror(err));
    return;
  }

  while(__began.since().secondsD() < __duration) {
    char buf[2048];
    bzero(buf, sizeof(buf));

    socket.read(buf, sizeof(buf) - 1, false);
    
    // buf[n] = 0;

    printf("%s", buf);
    fflush(0);
  }

  Radiant::info("%s Closing socket", appname);

  socket.close();
}
开发者ID:enuuros,项目名称:multitude,代码行数:28,代码来源:Main.cpp

示例5: test_bring_up_down

void test_bring_up_down() {
    NetworkInterface* net = MBED_CONF_APP_OBJECT_CONSTRUCTION;

    for (int i = 0; i < COUNT; i++) {
        int err = MBED_CONF_APP_CONNECT_STATEMENT;
        TEST_ASSERT_EQUAL(0, err);

        printf("MBED: IP Address %s\r\n", net->get_ip_address());
        TEST_ASSERT(net->get_ip_address());

        UDPSocket udp;
        err = udp.open(net);
        TEST_ASSERT_EQUAL(0, err);
        err = udp.close();
        TEST_ASSERT_EQUAL(0, err);

        TCPSocket tcp;
        err = tcp.open(net);
        TEST_ASSERT_EQUAL(0, err);
        err = tcp.close();
        TEST_ASSERT_EQUAL(0, err);

        err = net->disconnect();
        TEST_ASSERT_EQUAL(0, err);
    }
}
开发者ID:Archcady,项目名称:mbed-os,代码行数:26,代码来源:main.cpp

示例6: _tcpsocket_connect_to_chargen_srv

static void _tcpsocket_connect_to_chargen_srv(TCPSocket& sock) {
    SocketAddress tcp_addr;

    get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
    tcp_addr.set_port(19);

    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(tcp_addr));
}
开发者ID:sg-,项目名称:mbed-os,代码行数:9,代码来源:tcpsocket_recv_100k.cpp

示例7: is_tcp_supported

bool is_tcp_supported()
{
    static bool supported;
    static bool tested = false;
    if (!tested) {
        TCPSocket socket;
        supported = socket.open(NetworkInterface::get_default_instance()) == NSAPI_ERROR_OK;
    }
    return supported;
}
开发者ID:0xc0170,项目名称:mbed,代码行数:10,代码来源:main.cpp

示例8: TCPSOCKET_OPEN_CLOSE_REPEAT

void TCPSOCKET_OPEN_CLOSE_REPEAT()
{
    SKIP_IF_TCP_UNSUPPORTED();
    TCPSocket *sock = new TCPSocket;
    if (!sock) {
        TEST_FAIL();
    }

    for (int i = 0; i < 2; i++) {
        TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
        TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
    }
    delete sock;
}
开发者ID:0xc0170,项目名称:mbed,代码行数:14,代码来源:tcpsocket_open_close_repeat.cpp

示例9: _tcpsocket_connect_to_chargen_srv

static nsapi_error_t _tcpsocket_connect_to_chargen_srv(TCPSocket &sock)
{
    SocketAddress tcp_addr;

    get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
    tcp_addr.set_port(19);

    nsapi_error_t err = sock.open(get_interface());
    if (err != NSAPI_ERROR_OK) {
        return err;
    }

    return sock.connect(tcp_addr);
}
开发者ID:betzw,项目名称:mbed,代码行数:14,代码来源:tcpsocket_recv_100k.cpp

示例10: runClient

void runClient(const char * host, int port, const char * message)
{
  printf("Setting up a client socket to %s:%d\n", host, port);

  TCPSocket socket;
  int err = socket.open(host, port);

  if(err) {
    const int  msgSize = 128;
    char  msgBuf[msgSize] = "";
    printf("%s cannot open client socket to %s:%d -> %s\n", 
	   appname, host, port, strerror_s(msgBuf, msgSize, err));
    return;
  }

  socket.setNoDelay(true);

  char buf[1024];

  for(int i = 0; i < iterations; i++) {

    puts("Sending message");

    sprintf(buf, "%s %d", message, i + 1);
    
    int32_t len = strlen(buf) + 1;
    socket.write( & len, 4);
    socket.write(buf, len);

    if(withreplies) {
      len = 0;
      socket.read( & len, 4);
      bzero(buf, sizeof(buf));
      socket.read(buf, len);
      printf("Received reply \"%s\"\n", buf);
    }
  }

  puts("Closing");

  socket.close();
}
开发者ID:enuuros,项目名称:multitude,代码行数:42,代码来源:Main.cpp

示例11: TCPSOCKET_BIND_WRONG_TYPE

void TCPSOCKET_BIND_WRONG_TYPE()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
    int count = fetch_stats();
    for (int j = 0; j < count; j++) {
        TEST_ASSERT_EQUAL(SOCK_CLOSED,  tcp_stats[j].state);
    }
#endif

    TCPSocket *sock = new TCPSocket;
    if (!sock) {
        TEST_FAIL();
        return;
    }
    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
    char addr_bytes[16] = {0xfe, 0x80, 0xff, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    SocketAddress sockAddr;
    if (get_ip_version() == NSAPI_IPv4) {
        sockAddr = SocketAddress(addr_bytes, NSAPI_IPv4, 80);
    } else if (get_ip_version() == NSAPI_IPv6) {
        sockAddr = SocketAddress(addr_bytes, NSAPI_IPv6, 80);
    } else {
        TEST_FAIL_MESSAGE("This stack is neither IPv4 nor IPv6");
    }
    nsapi_error_t bind_result = sock->bind(sockAddr);
    if (bind_result == NSAPI_ERROR_UNSUPPORTED) {
        TEST_IGNORE_MESSAGE("bind() not supported");
    } else {
        TEST_ASSERT_EQUAL(NSAPI_ERROR_PARAMETER, bind_result);
    }

    delete sock;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
    count = fetch_stats();
    for (int j = 0; j < count; j++) {
        TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
    }
#endif
}
开发者ID:donatieng,项目名称:mbed,代码行数:40,代码来源:tcpsocket_bind_wrong_type.cpp

示例12: test_tcp_echo_async

/**
 * Test TCP data exchange via the asynchronous sigio() mechanism
 */
void test_tcp_echo_async()
{
    TCPSocket sock;
    SocketAddress host_address;
    bool callback_triggered = false;
    int x;
    int size;

    driver.disconnect();
    TEST_ASSERT(do_connect(&driver) == 0);

    TEST_ASSERT(
        driver.gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0);
    host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT);

    tr_debug("TCP: Server %s address: %s on port %d.",
             MBED_CONF_APP_ECHO_SERVER, host_address.get_ip_address(),
             host_address.get_port());

    TEST_ASSERT(sock.open(&driver) == 0)

    // Set up the async callback and set the timeout to zero
    sock.sigio(callback(async_cb, &callback_triggered));
    sock.set_timeout(0);

    TEST_ASSERT(sock.connect(host_address) == 0);
    // Test min, max, and some random sizes in-between
    do_tcp_echo_async(&sock, 1, &callback_triggered);
    do_tcp_echo_async(&sock, MBED_CONF_APP_TCP_MAX_PACKET_SIZE,
                      &callback_triggered);

    sock.close();

    drop_connection(&driver);

    tr_debug("TCP packets of size up to %d byte(s) echoed asynchronously and successfully.",
             MBED_CONF_APP_TCP_MAX_PACKET_SIZE);
}
开发者ID:MarceloSalazar,项目名称:mbed-os,代码行数:41,代码来源:main.cpp

示例13: TCPSOCKET_OPEN_DESTRUCT

void TCPSOCKET_OPEN_DESTRUCT()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
    int count = fetch_stats();
    for (int j = 0; j < count; j++) {
        TEST_ASSERT_EQUAL(SOCK_CLOSED,  tcp_stats[j].state);
    }
#endif

    for (int i = 0; i < 100; i++) {
        TCPSocket *sock = new TCPSocket;
        if (!sock) {
            TEST_FAIL();
        }
        TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
        delete sock;
    }
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
    count = fetch_stats();
    for (int j = 0; j < count; j++) {
        TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
    }
#endif
}
开发者ID:pan-,项目名称:mbed,代码行数:24,代码来源:tcpsocket_open_destruct.cpp

示例14: TCPSOCKET_OPEN_LIMIT

void TCPSOCKET_OPEN_LIMIT()
{
    int open_sockets[2] = {0};

    for (int i = 0; i < 2; i++) {
        TCPSocketItem *socket_list_head = NULL;
        TCPSocketItem *it;

        TCPSocket *sock;
        int ret;
        while (true) {
            sock = new TCPSocket;
            if (!sock) {
                break;
            }
            ret = sock->open(NetworkInterface::get_default_instance());
            if (ret == NSAPI_ERROR_NO_MEMORY || ret == NSAPI_ERROR_NO_SOCKET) {
                printf("[round#%02d] unable to open new socket, error: %d\n", i, ret);
                delete sock;
                break;
            }

            // Hopefully this doesn't interfere when trying to allocate more sockets
            it = new TCPSocketItem;
            if (!it) {
                delete sock;
                break;
            }

            it->sock = sock;
            // Order of items in the list doesn't matter
            it->next = socket_list_head;
            socket_list_head = it;
        }

        if (!socket_list_head) {
            break;
        }

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
        int count = fetch_stats();
        int open_count = 0;
        for (int j = 0; j < count; j++) {
            if ((tcp_stats[j].state == SOCK_OPEN) && (tcp_stats[j].proto == NSAPI_TCP)) {
                open_count++;
            }
        }
        TEST_ASSERT(open_count >= 4);
#endif

        TCPSocketItem *tmp;
        for (TCPSocketItem *it = socket_list_head; it;) {
            ++open_sockets[i];
            tmp = it;
            it = it->next;
            socket_list_head = it;
            delete tmp->sock;
            delete tmp;
        }
        printf("[round#%02d] %d sockets opened\n", i, open_sockets[i]);
    }
    TEST_ASSERT_EQUAL(open_sockets[0], open_sockets[1]);
    TEST_ASSERT(open_sockets[0] >= 4);
}
开发者ID:donatieng,项目名称:mbed,代码行数:64,代码来源:tcpsocket_open_limit.cpp

示例15: CVDRONE_ERROR

// --------------------------------------------------------------------------
// ARDrone::getConfig()
// Description  : Get current configurations of AR.Drone.
// Return value : SUCCESS: 1  FAILURE: 0
// --------------------------------------------------------------------------
int ARDrone::getConfig(void)
{
    // Open the IP address and port
    TCPSocket sockConfig;
    if (!sockConfig.open(ip, ARDRONE_CONFIG_PORT)) {
        CVDRONE_ERROR("TCPSocket::open(port=%d) failed. (%s, %d)\n", ARDRONE_CONFIG_PORT, __FILE__, __LINE__);
        return 0;
    }

    // Send requests
    UDPSocket tmpCommand;
    tmpCommand.open(ip, ARDRONE_COMMAND_PORT);
    tmpCommand.sendf("AT*CTRL=%d,5,0\r", seq++);
    tmpCommand.sendf("AT*CTRL=%d,4,0\r", seq++);
    msleep(500);
    tmpCommand.close();

    // Receive data
    char buf[4096] = {'\0'};
    int size = sockConfig.receive((void*)&buf, sizeof(buf));

    // Received something
    if (size > 0) {
        // Clear config struct
        memset(&config, 0, sizeof(ARDRONE_CONFIG));

        // Parsing configurations
        char *token = strtok(buf, "\n");
        if (token != NULL) parse(token, &config);
        while (token != NULL) {
            token = strtok(NULL, "\n");
            if (token != NULL) parse(token, &config);
        }
    }

    #if 0
    // For debug
    printf("general.num_version_config = %d\n", config.general.num_version_config);
    printf("general.num_version_mb = %d\n", config.general.num_version_mb);
    printf("general.num_version_soft = %s\n", config.general.num_version_soft);
    printf("general.drone_serial = %s\n", config.general.drone_serial);
    printf("general:soft_build_date = %s\n", config.general.soft_build_date);
    printf("general:motor1_soft = %f\n", config.general.motor1_soft);
    printf("general:motor1_hard = %f\n", config.general.motor1_hard);
    printf("general:motor1_supplier = %f\n", config.general.motor1_supplier);
    printf("general:motor2_soft = %f\n", config.general.motor2_soft);
    printf("general:motor2_hard = %f\n", config.general.motor2_hard);
    printf("general:motor2_supplier = %f\n", config.general.motor2_supplier);
    printf("general:motor3_soft = %f\n", config.general.motor3_soft);
    printf("general:motor3_hard = %f\n", config.general.motor3_hard);
    printf("general:motor3_supplier = %f\n", config.general.motor3_supplier);
    printf("general:motor4_soft = %f\n", config.general.motor4_soft);
    printf("general:motor4_hard = %f\n", config.general.motor4_hard);
    printf("general:motor4_supplier = %f\n", config.general.motor4_supplier);
    printf("general.ardrone_name = %s\n", config.general.ardrone_name);
    printf("general.flying_time = %d\n", config.general.flying_time);
    printf("general.navdata_demo = %s\n", config.general.navdata_demo ? "true" : "false");
    printf("general.com_watchdog = %d\n", config.general.com_watchdog);
    printf("general.video_enable = %s\n", config.general.video_enable ? "true" : "false");
    printf("general.vision_enable = %s\n", config.general.vision_enable ? "true" : "false");
    printf("general.vbat_min = %d\n", config.general.vbat_min);
    printf("general.localtime = %d\n", config.general.localtime);
    printf("general.navdata_options = %d\n", config.general.navdata_options);
    printf("control:accs_offset = {%f, %f, %f}\n", config.control.accs_offset[0], config.control.accs_offset[1], config.control.accs_offset[2]);
    printf("control:accs_gains = { %f %f %f %f %f %f %f %f %f }\n", config.control.accs_gains[0], config.control.accs_gains[1], config.control.accs_gains[2], config.control.accs_gains[3], config.control.accs_gains[4], config.control.accs_gains[5], config.control.accs_gains[6], config.control.accs_gains[7], config.control.accs_gains[8]);
    printf("control:gyros_offset = { %f %f %f }\n", config.control.gyros_offset[0], config.control.gyros_offset[1], config.control.gyros_offset[2]);
    printf("control:gyros_gains = { %f %f %f }\n", config.control.gyros_gains[0], config.control.gyros_gains[1], config.control.gyros_gains[2]);
    printf("control:gyros110_offset = { %f %f }\n", config.control.gyros110_offset[0], config.control.gyros110_offset[1]);
    printf("control:gyros110_gains = { %f %f }\n", config.control.gyros110_gains[0], config.control.gyros110_gains[1]);
    printf("control:magneto_offset = { %f %f %f }\n", config.control.magneto_offset[0], config.control.magneto_offset[1], config.control.magneto_offset[2]);
    printf("control:magneto_radius = %f\n", config.control.magneto_radius);
    printf("control:gyro_offset_thr_x = %f\n", config.control.gyro_offset_thr_x);
    printf("control:gyro_offset_thr_y = %f\n", config.control.gyro_offset_thr_y);
    printf("control:gyro_offset_thr_z = %f\n", config.control.gyro_offset_thr_z);
    printf("control:pwm_ref_gyros = %d\n", config.control.pwm_ref_gyros);
    printf("control:osctun_value = %d\n", config.control.osctun_value);
    printf("control:osctun_test = %s\n", config.control.osctun_test ? "true" : "false");
    printf("control:altitude_max = %d\n", config.control.altitude_max);
    printf("control:altitude_min = %d\n", config.control.altitude_min);
    printf("control:outdoor = %s\n", config.control.outdoor ? "true" : "false");
    printf("control:flight_without_shell = %s\n", config.control.flight_without_shell ? "true" : "false");
    printf("control:autonomous_flight = %s\n", config.control.autonomous_flight ? "true" : "false");
    printf("control:flight_anim = %d,%d\n", config.control.flight_anim[0], config.control.flight_anim[1]);
    printf("control:control_level = %d\n", config.control.control_level);
    printf("control:euler_angle_max = %f\n", config.control.euler_angle_max);
    printf("control:control_iphone_tilt = %f\n", config.control.control_iphone_tilt);
    printf("control:control_vz_max = %f\n", config.control.control_vz_max);
    printf("control:control_yaw = %f\n", config.control.control_yaw);
    printf("control:manual_trim = %s\n", config.control.manual_trim ? "true" : "false");
    printf("control:indoor_euler_angle_max = %f\n", config.control.indoor_euler_angle_max);
    printf("control:indoor_control_vz_max = %f\n", config.control.indoor_control_vz_max);
    printf("control:indoor_control_yaw = %f\n", config.control.indoor_control_yaw);
    printf("control:outdoor_euler_angle_max = %f\n", config.control.outdoor_euler_angle_max);
    printf("control:outdoor_control_vz_max = %f\n", config.control.outdoor_control_vz_max);
    printf("control:outdoor_control_yaw = %f\n", config.control.outdoor_control_yaw);
//.........这里部分代码省略.........
开发者ID:xxxlong,项目名称:cvdrone,代码行数:101,代码来源:config.cpp


注:本文中的TCPSocket::open方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。