Mbed OS 文档翻译 之 参考(API(网络套接字(TCPSocket)))

TCPSocket

                                                                                     

                                                                             TCPSocket 类层次结构

TCPSocket 类提供通过 TCP 发送数据流的能力。TCPSockets 维护一个以 connect 成员函数开始的有状态连接。成功连接到服务器后,您可以使用 send 和 recv 成员函数来发送和接收数据(类似于从文件写入或读取)。

构造函数接受 NetworkStack 指针以打开指定 NetworkInterface 上的套接字。如果你没有传入构造函数,那么你必须调用 open 来初始化套接字。

有关 TCP 服务器功能,请参阅 TCPServer 类。

TCPSocket 类参考

TCPSocket 类参考

公共成员函数
 TCPSocket ()
template<typename S >
 TCPSocket (S *stack)
virtual ~TCPSocket ()
virtual int join_multicast_group (const SocketAddress &address)
nsapi_error_t connect (const char *host, uint16_t port)
virtual nsapi_error_t connect (const SocketAddress &address)
virtual nsapi_size_or_error_t send (const void *data, nsapi_size_t size)
virtual nsapi_size_or_error_t recv (void *data, nsapi_size_t size)
virtual nsapi_size_or_error_t sendto (const SocketAddress &address, const void *data, nsapi_size_t size)
virtual nsapi_size_or_error_t recvfrom (SocketAddress *address, void *data, nsapi_size_t size)
virtual TCPSocketaccept (nsapi_error_t *error=NULL)
virtual nsapi_error_t listen (int backlog=1)
 公共成员函数继承自 InternetSocket
virtual ~InternetSocket ()
nsapi_error_t open (NetworkStack *stack)
template<typename S >
nsapi_error_t open (S *stack)
virtual nsapi_error_t close ()
int join_multicast_group (const SocketAddress &address)
int leave_multicast_group (const SocketAddress &address)
nsapi_error_t bind (uint16_t port)
nsapi_error_t bind (const char *address, uint16_t port)
virtual nsapi_error_t bind (const SocketAddress &address)
virtual void set_blocking (bool blocking)
virtual void set_timeout (int timeout)
virtual nsapi_error_t setsockopt (int level, int optname, const void *optval, unsigned optlen)
virtual nsapi_error_t getsockopt (int level, int optname, void *optval, unsigned *optlen)
virtual void sigio (mbed::Callback< void()> func)
void attach (mbed::Callback< void()> func)
template<typename T , typename M >
void attach (T *obj, M method)
 公共成员函数继承自 Socket
virtual ~Socket ()
受保护的成员函数
virtual nsapi_protocol_t get_proto ()
 受保护的成员函数继承自 InternetSocket
virtual void event ()
int modify_multicast_group (const SocketAddress &address, nsapi_socket_option_t socketopt)
友元
class TCPServer
其他继承成员
 受保护的属性继承自 InternetSocket
NetworkStack_stack
nsapi_socket_t _socket
uint32_t _timeout
mbed::Callback< void()> _event
mbed::Callback< void()> _callback
rtos::EventFlags _event_flag
rtos::Mutex _lock
SocketAddress _remote_peer
uint8_t _readers
uint8_t _writers
volatile unsigned _pending
bool _factory_allocated
 从中继承的静态保护属性 InternetSocket
static const int READ_FLAG = 0x1u
static const int WRITE_FLAG = 0x2u
static const int FINISHED_FLAG = 0x3u

TCPSocket 示例

这是使用 ESP8266 模块的 HTTP 事务的 TCP 客户端示例。

main.cpp                                                                                                                                                 导入到 Mbed IDE

/* WiFi Example
 * Copyright (c) 2016 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "TCPSocket.h"

#if TARGET_UBLOX_EVK_ODIN_W2
#include "OdinWiFiInterface.h"
OdinWiFiInterface wifi;
#else
#if !TARGET_FF_ARDUINO
#error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time
#endif
#include "ESP8266Interface.h"
ESP8266Interface wifi(D1, D0);
#endif

const char *sec2str(nsapi_security_t sec)
{
    switch (sec) {
        case NSAPI_SECURITY_NONE:
            return "None";
        case NSAPI_SECURITY_WEP:
            return "WEP";
        case NSAPI_SECURITY_WPA:
            return "WPA";
        case NSAPI_SECURITY_WPA2:
            return "WPA2";
        case NSAPI_SECURITY_WPA_WPA2:
            return "WPA/WPA2";
        case NSAPI_SECURITY_UNKNOWN:
        default:
            return "Unknown";
    }
}

void scan_demo(WiFiInterface *wifi)
{
    WiFiAccessPoint *ap;

    printf("Scan:\r\n");

    int count = wifi->scan(NULL,0);

    /* Limit number of network arbitrary to 15 */
    count = count < 15 ? count : 15;

    ap = new WiFiAccessPoint[count];
    count = wifi->scan(ap, count);
    for (int i = 0; i < count; i++)
    {
        printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
               sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
               ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
    }
    printf("%d networks available.\r\n", count);

    delete[] ap;
}

void http_demo(NetworkInterface *net)
{
    TCPSocket socket;

    printf("Sending HTTP request to www.arm.com...\r\n");

    // Open a socket on the network interface, and create a TCP connection to www.arm.com
    socket.open(net);
    socket.connect("www.arm.com", 80);

    // 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);
    printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);

    // Recieve a simple http response and print out the response line
    char rbuffer[64];
    int rcount = socket.recv(rbuffer, sizeof rbuffer);
    printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);

    // Close the socket to return its memory and bring down the network interface
    socket.close();
}

int main()
{
    printf("WiFi example\r\n\r\n");

    scan_demo(&wifi);

    printf("\r\nConnecting...\r\n");
    int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
    if (ret != 0) {
        printf("\r\nConnection error\r\n");
        return -1;
    }

    printf("Success\r\n\r\n");
    printf("MAC: %s\r\n", wifi.get_mac_address());
    printf("IP: %s\r\n", wifi.get_ip_address());
    printf("Netmask: %s\r\n", wifi.get_netmask());
    printf("Gateway: %s\r\n", wifi.get_gateway());
    printf("RSSI: %d\r\n\r\n", wifi.get_rssi());

    http_demo(&wifi);

    wifi.disconnect();

    printf("\r\nDone\r\n");
}

以下是通过以太网接口进行 HTTP 事务的 TCP 客户端示例。

main.cpp                                                                                                                                              导入到 Mbed IDE

#include "mbed.h"
#include "EthernetInterface.h"

// Network interface
EthernetInterface net;

// Socket demo
int main() {
    // Bring up the ethernet interface
    printf("Ethernet socket example\n");
    net.connect();

    // Show the network address
    const char *ip = net.get_ip_address();
    printf("IP address is: %s\n", ip ? ip : "No IP");

    // Open a socket on the network interface, and create a TCP connection to mbed.org
    TCPSocket socket;
    socket.open(&net);
    socket.connect("developer.mbed.org", 80);

    // Send a simple http request
    char sbuffer[] = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n";
    int scount = socket.send(sbuffer, sizeof sbuffer);
    printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);

    // Recieve a simple http response and print out the response line
    char rbuffer[64];
    int rcount = socket.recv(rbuffer, sizeof rbuffer);
    printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);

    // Close the socket to return its memory and bring down the network interface
    socket.close();

    // Bring down the ethernet interface
    net.disconnect();
    printf("Done\n");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值