Quantcast
Channel: Forum - Recent Threads
Viewing all articles
Browse latest Browse all 5781

TCP Socket NetX: HTTP Implementation (POST/GET)

$
0
0

Hi guys,

I'm developing an application with Renesas Synergy (SK-S7G2 board) and I'm using Ethernet (NetX) functions.
I needed to use http client functions to send and receive data (POST and GET). But, after failing repeatedly (with netx http client functions and static 80 port), I started to use tcp/socket functions.

I've seen a lot of similar problems in the forum, but I have problems yet.
LINK1  LINK2  LINK3  LINK4  LINK5

Using Wireshark, I see the right packets, but I also see a lot of junk and spurious packets (It seems that something is missing).

I'll put some of my code below.If anyone can check I'm right.

The netx_http_post_data just send data to server specified. And the netx_http_get_data send data to other server and wait reply (like a validation).

Am I forgetting something? That does not seem to be totally right. And sorry, I am noob on Renesas Synergy and NetX.

#includes ...

#define _SERVER         IP_ADDRESS(x,x,x,x)
#define _POST           8086
#define _GET            8080

static void netx_http_post_data(sf_console_cb_args_t *p_args);
static void netx_http_get_data(sf_console_cb_args_t *p_args);

extern NX_IP    g_ip0;
extern NX_DHCP  g_dhcp_client0;

static void netx_http_post_data(sf_console_cb_args_t *p_args)
{
    UNUSED(p_args);

    static NX_TCP_SOCKET sock;
    NX_PACKET *pkt;

    ULONG server = _SERVER;
    UINT port = _POST;
    volatile UINT status;
    static int have_connection = 0;

    char message[300] = "POST /write... HTTP/1.1\r\nHost: ...\r\nContent-Type:...");

    if (have_connection == 0) {
        status = nx_tcp_socket_create(&g_ip0, &sock, "Renesas", NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 256,NX_NULL, NX_NULL);

        if (status != NX_SUCCESS)
            return;
        status = nx_tcp_client_socket_bind(&sock, NX_ANY_PORT, NX_WAIT_FOREVER);
        if (status != NX_SUCCESS) {
            status = nx_tcp_socket_delete(&sock);
            return;
        }
        status = nx_tcp_client_socket_connect(&sock, server, port, NX_WAIT_FOREVER);
        if (status == NX_SUCCESS)
            have_connection = 1;
    }

    if (have_connection) {
        status = nx_packet_allocate(&g_packet_pool0, &pkt, NX_TCP_PACKET, NX_WAIT_FOREVER);
        status = nx_packet_data_append(pkt, message, strlen(message), &g_packet_pool0, NX_WAIT_FOREVER);
        status = nx_tcp_socket_send(&sock, pkt, NX_WAIT_FOREVER);
    }

    status = nx_tcp_socket_disconnect(&sock, NX_WAIT_FOREVER);
    status = nx_tcp_client_socket_unbind(&sock);
    status = nx_tcp_socket_delete(&sock);
    have_connection = 0;

    return;
}

static void netx_http_get_data(sf_console_cb_args_t *p_args)
{
    UNUSED(p_args);

    static NX_TCP_SOCKET sock;
    NX_PACKET *pkt;

    ULONG server = _SERVER;
    UINT port = _GET;
    volatile UINT status;

    static int have_connection = 0;

    char message[] = "GET /data... HTTP/1.1\r\nHost: ... Authorization: Basic ... \r\n\r\n";

    status = nx_tcp_socket_create(&g_ip0, &sock, "Renesas",NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 1024, NX_NULL, NX_NULL);

    if (status != NX_SUCCESS) {
        status = nx_tcp_socket_delete(&sock);
        return;
    }

    // Bind and Connect
    status =  nx_tcp_client_socket_bind(&sock, 5001, NX_WAIT_FOREVER);
    status = nx_tcp_client_socket_connect(&sock, server, port, NX_WAIT_FOREVER);
    if (status == NX_SUCCESS)
        have_connection = 1;

    if (have_connection) {

        // Allocate and Append Message
        status = nx_packet_allocate(&g_packet_pool0, &pkt, NX_TCP_PACKET, NX_WAIT_FOREVER);
        status = nx_packet_data_append(pkt, message, strlen(message), &g_packet_pool0, NX_WAIT_FOREVER);

        // Send and Receive Server Reply
        status = nx_tcp_socket_send(&sock, pkt, NX_WAIT_FOREVER);
        status =  nx_tcp_socket_receive(&sock, &pkt, NX_WAIT_FOREVER);
    }

    status = nx_tcp_socket_disconnect(&sock, NX_WAIT_FOREVER);
    status = nx_tcp_client_socket_unbind(&sock);
    status = nx_tcp_socket_delete(&sock);
    have_connection = 0;

    return;
}

Viewing all articles
Browse latest Browse all 5781

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>