Socket creation

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Socket creation

610 Views
sachinn434
Contributor I

Hi,

i am using lpc54s018 i am trying to create socket programming but i am getting socket creation failed so  please help me what the main reason  for that is 

 

 

Thanks  

0 Kudos
2 Replies

530 Views
sachinn434
Contributor I

hi,

thanks for reply but i am using  tcpecho_raw_init(); for tcip client and server connection but tcpsend and tcpreceive  is not going continuously 

/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of and a contribution to the lwIP TCP/IP stack.
*
* Credits go to Adam Dunkels (and the current maintainers) of this software.
*
* Christiaan Simons rewrote this file to get a more stable echo example.
*/

/**
* @file
* TCP echo server example using raw API.
*
* Echos all bytes sent by connecting client,
* and passively closes when client is done.
*
*/


#include "lwip/opt.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include "tcpecho_raw.h"
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include "fsl_usart.h"
#include "fsl_debug_console.h"
//#include"USART_HEADER.h"
#include<string.h>
#include "lwip/sys.h"
/* FreeRTOS kernel includes. */
//#include "FreeRTOS.h"
//#include "task.h"
//#include "queue.h"
//#include "timers.h"
#define BUFFER_SIZE 256
int flag=0;
uint8_t Udatabuf[]=" ";
uint8_t buffer[BUFFER_SIZE];
static volatile bool closing = false;
#if LWIP_TCP
static struct tcp_pcb *tcpecho_raw_pcb;
void connected_callback(void *arg, struct tcp_pcb *tpcb, err_t err);


/*! @brief Stack size of the temporary lwIP initialization thread. */
#define INIT_THREAD_STACKSIZE 1024

/*! @brief Priority of the temporary lwIP initialization thread. */
#define INIT_THREAD_PRIO DEFAULT_THREAD_PRIO

/*! @brief Stack size of the thread which prints DHCP info. */
#define PRINT_THREAD_STACKSIZE 512

/*! @brief Priority of the thread which prints DHCP info. */
#define PRINT_THREAD_PRIO DEFAULT_THREAD_PRIO


enum tcpecho_raw_states
{
ES_NONE = 0,
ES_ACCEPTED,
ES_RECEIVED,
ES_CLOSING
};

struct tcpecho_raw_state
{
u8_t state;
u8_t retries;
struct tcp_pcb *pcb;
/* pbuf (chain) to recycle */
struct pbuf *p;
};

static void
tcpecho_raw_free(struct tcpecho_raw_state *es)
{
if (es != NULL) {
if (es->p) {
/* free the buffer chain if present */
pbuf_free(es->p);
}

mem_free(es);
}
}

static void
tcpecho_raw_close(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es)
{
tcp_arg(tpcb, NULL);
tcp_sent(tpcb, NULL);
tcp_recv(tpcb, NULL);
tcp_err(tpcb, NULL);
tcp_poll(tpcb, NULL, 0);

tcpecho_raw_free(es);

tcp_close(tpcb);
}

static void
tcpecho_raw_send(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es)
{

struct pbuf *ptr;
err_t wr_err = ERR_OK;

while ((wr_err == ERR_OK) &&
(es->p != NULL) &&
(es->p->len <= tcp_sndbuf(tpcb))) {
ptr = es->p;

/* enqueue data for transmission */
wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1);

PRINTF(" the pay load is sent %s",ptr->payload);

PRINTF("\r\n");

if (wr_err == ERR_OK) {
u16_t plen;

plen = ptr->len;
/* continue with next pbuf in chain (if any) */
es->p = ptr->next;
if(es->p != NULL) {
/* new reference! */
pbuf_ref(es->p);
}
/* chop first pbuf from chain */
pbuf_free(ptr);
/* we can read more data now */
tcp_recved(tpcb, plen);
} else if(wr_err == ERR_MEM) {
/* we are low on memory, try later / harder, defer to poll */
es->p = ptr;
} else {
/* other problem ?? */
}
}
}

static void
tcpecho_raw_error(void *arg, err_t err)
{
struct tcpecho_raw_state *es;

LWIP_UNUSED_ARG(err);

es = (struct tcpecho_raw_state *)arg;

tcpecho_raw_free(es);
}

static err_t
tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
{
err_t ret_err;
struct tcpecho_raw_state *es;

es = (struct tcpecho_raw_state *)arg;
if (es != NULL) {
if (es->p != NULL) {
/* there is a remaining pbuf (chain) */
tcpecho_raw_send(tpcb, es);
} else {
/* no remaining pbuf (chain) */
if(es->state == ES_CLOSING) {
tcpecho_raw_close(tpcb, es);
}
}
ret_err = ERR_OK;
} else {
/* nothing to be done */
tcp_abort(tpcb);
ret_err = ERR_ABRT;
}
return ret_err;
}

static err_t
tcpecho_raw_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
{

struct tcpecho_raw_state *es;

LWIP_UNUSED_ARG(len);

es = (struct tcpecho_raw_state *)arg;
es->retries = 0;

if(es->p != NULL) {

/* still got pbufs to send */
tcp_sent(tpcb, tcpecho_raw_sent);
tcpecho_raw_send(tpcb, es);

} else {
/* no more pbufs to send */
if(es->state == ES_CLOSING) {
tcpecho_raw_close(tpcb, es);
}
}
return ERR_OK;
}

static err_t
tcpecho_raw_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{

struct tcpecho_raw_state *es;
err_t ret_err;


PRINTF("RECEIVED DATA = %d %s\n",p->len,p->payload);

// not printing this second time it is not print ?

LWIP_ASSERT("arg != NULL",arg != NULL);
es = (struct tcpecho_raw_state *)arg;

if (p == NULL) {
/* remote host closed connection */
PRINTF("P is NULL");
es->state = ES_CLOSING;

if(es->p == NULL) {
PRINTF("es->p NULL");
/* we're done sending, close it */
tcpecho_raw_close(tpcb, es);
} else {
/* we're not done yet */
tcpecho_raw_send(tpcb, es);
}
ret_err = ERR_OK;
} else if(err != ERR_OK) {
/* cleanup, for unknown reason */
if (p != NULL) {
pbuf_free(p);
PRINTF("pbuf_freeL");
}
ret_err = err;
}
else if(es->state == ES_ACCEPTED) {
/* first data chunk in p->payload */
es->state = ES_RECEIVED;
/* store reference to incoming pbuf (chain) */
es->p = p;
tcpecho_raw_send(tpcb, es);
ret_err = ERR_OK;
} else if (es->state == ES_RECEIVED) {
/* read some more data */
if(es->p == NULL) {
es->p = p;
tcpecho_raw_send(tpcb, es);
} else {
struct pbuf *ptr;

/* chain pbufs to the end of what we recv'ed previously */
ptr = es->p;
pbuf_cat(ptr,p);
}
ret_err = ERR_OK;
} else {
/* unkown es->state, trash data */
tcp_recved(tpcb, p->tot_len);
pbuf_free(p);
ret_err = ERR_OK;
}
return ret_err;


}

static err_t
tcpecho_raw_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
{
err_t ret_err;
struct tcpecho_raw_state *es;

LWIP_UNUSED_ARG(arg);
if ((err != ERR_OK) || (newpcb == NULL)) {
return ERR_VAL;
}

/* Unless this pcb should have NORMAL priority, set its priority now.
When running out of pcbs, low priority pcbs can be aborted to create
new pcbs of higher priority. */
tcp_setprio(newpcb, TCP_PRIO_MIN);

es = (struct tcpecho_raw_state *)mem_malloc(sizeof(struct tcpecho_raw_state));
if (es != NULL) {
es->state = ES_ACCEPTED;
es->pcb = newpcb;
es->retries = 0;
es->p = NULL;
/* pass newly allocated es to our callbacks */
tcp_arg(newpcb, es);
tcp_recv(newpcb, tcpecho_raw_recv);
tcp_err(newpcb, tcpecho_raw_error);
tcp_poll(newpcb, tcpecho_raw_poll, 0);
tcp_sent(newpcb, tcpecho_raw_sent);
ret_err = ERR_OK;
} else {
ret_err = ERR_MEM;
}
return ret_err;
}

void tcp_recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p)
{
// process received data here
// ...
PRINTF("tcp_recv_callback done .\n");
// indicate that the data was processed successfully
tcp_recved(pcb, p->tot_len);

// free the packet buffer
pbuf_free(p);
}

void tcp_sent_callback(void *arg, struct tcp_pcb *pcb, u16_t len)
{
// the data has been successfully sent
// ...
PRINTF("tcp_sent_callback done .\n");
// free any memory used to store the sent data
// ...
}
void close_conn(void)
{
closing = true;
tcp_close(tcpecho_raw_pcb);
}

// Define a callback function to handle the connected event
void connected_callback(void *arg, struct tcp_pcb *tpcb, err_t err) {
if (err == ERR_OK) {

PRINTF("Connecting to server............................\n");
PRINTF("\n");
// Connection was successful
PRINTF("Connected to remote endpoint!\n");
PRINTF("\r\n");
PRINTF("***** SERVER IP ->> 192.168.0.150 ********\n");

char str[]="HELLO, V3 NOVUS\n";
int len=strlen(str);

// Send data over the connection
tcp_write(tpcb, "HELLO, V3 NOVUS\n", len, TCP_WRITE_FLAG_COPY); //
flag=1;
} else {
// Connection failed
PRINTF("Failed to connect to remote endpoint.\n");
}
}

#if 0


void vSendTask( void *pvParameters )
{
tcp_sent(tcpecho_raw_pcb,tcpecho_raw_sent);

}

void vReceiveTask( void *pvParameters )
{
tcp_recv(tcpecho_raw_pcb, tcpecho_raw_recv);

}

#endif
void
tcpecho_raw_init(void)
{
tcpecho_raw_pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
if (tcpecho_raw_pcb != NULL) {
err_t err;
PRINTF("NEW_SOCKET_CREATED.\n");
err = tcp_bind(tcpecho_raw_pcb, IP_ANY_TYPE, 9000);
PRINTF("tcp_bind pass .\n");
if (err == ERR_OK) {


// tcpecho_raw_pcb = tcp_listen(tcpecho_raw_pcb);
// tcp_accept(tcpecho_raw_pcb, tcpecho_raw_accept);
// PRINTF("tcp_accept pass .\n");

 

// PRINTF("Waiting for incoming connection...\n");
// for(int i=0;i<1000;i++);// wait for 1 second

 

ip_addr_t ipaddr;
IP4_ADDR(&ipaddr, 192, 168, 0, 150);
u16_t port = 9000;


err_t con = tcp_connect(tcpecho_raw_pcb,&ipaddr,port ,connected_callback);

if(con == ERR_OK) {

PRINTF("tcp_connect pass .\n");
}

#if 0
while(1)
{
USART_ReadBlocking(ENC_USART, Udatabuf,sizeof(Udatabuf));
USART_WriteBlocking(ENC_USART, Udatabuf, sizeof(Udatabuf));
// Send data over the connection
tcp_write(tcpecho_raw_pcb, Udatabuf, sizeof(Udatabuf), TCP_WRITE_FLAG_COPY);
}
#endif

 

 

tcp_sent(tcpecho_raw_pcb,tcpecho_raw_sent);

PRINTF("tcp_recv done .\n");
tcp_recv(tcpecho_raw_pcb, tcpecho_raw_recv);

for(int i=0;i<50000;i++);// increase the delay and check once


// err_t val = tcp_close(tcpecho_raw_pcb);
// PRINTF("tcp_close.%d\n",val);
tcp_err(tpcb, error_callback);

} else {
/* abort? output diagnostic? */
}
} else {
/* abort? output diagnostic? */
}
}

#endif /* LWIP_TCP */

 

 

main

 

/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/

/*******************************************************************************
* Includes
******************************************************************************/
#include "lwip/opt.h"

#if LWIP_TCP

#include "tcpecho_raw.h"
#include "lwip/timeouts.h"
#include "lwip/init.h"
#include "netif/ethernet.h"
#include "ethernetif.h"
#include "fsl_debug_console.h"
#include "board.h"

#include "pin_mux.h"
#include <stdbool.h>
/*******************************************************************************
* Definitions
******************************************************************************/

/* IP address configuration. */
#define configIP_ADDR0 192
#define configIP_ADDR1 168
#define configIP_ADDR2 0
#define configIP_ADDR3 102

/* Netmask configuration. */
#define configNET_MASK0 255
#define configNET_MASK1 255
#define configNET_MASK2 255
#define configNET_MASK3 0

/* Gateway address configuration. */
#define configGW_ADDR0 192
#define configGW_ADDR1 168
#define configGW_ADDR2 0
#define configGW_ADDR3 100

/* MAC address configuration. */
#define configMAC_ADDR {0x02, 0x12, 0x13, 0x10, 0x15, 0x11}

/* Address of PHY interface. */
#define EXAMPLE_PHY_ADDRESS BOARD_ENET0_PHY_ADDRESS

/* System clock name. */
#define EXAMPLE_CLOCK_NAME kCLOCK_CoreSysClk


/*******************************************************************************
* Prototypes
******************************************************************************/

/*******************************************************************************
* Variables
******************************************************************************/

/*******************************************************************************
* Code
******************************************************************************/

/*!
* @brief Interrupt service for SysTick timer.
*/
void SysTick_Handler(void)
{
time_isr();
}

/*!
* @brief Main function.
*/
int main(void)
{
struct netif fsl_netif0;
ip4_addr_t fsl_netif0_ipaddr, fsl_netif0_netmask, fsl_netif0_gw;
ethernetif_config_t fsl_enet_config0 = {
.phyAddress = EXAMPLE_PHY_ADDRESS,
.clockName = EXAMPLE_CLOCK_NAME,
.macAddress = configMAC_ADDR,
};

CLOCK_EnableClock(kCLOCK_InputMux);

/* attach 12 MHz clock to FLEXCOMM0 (debug console) */
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);

BOARD_InitPins();
BOARD_BootClockFROHF96M();
BOARD_InitDebugConsole();

time_init();

IP4_ADDR(&fsl_netif0_ipaddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3);
IP4_ADDR(&fsl_netif0_netmask, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3);
IP4_ADDR(&fsl_netif0_gw, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3);

lwip_init();

netif_add(&fsl_netif0, &fsl_netif0_ipaddr, &fsl_netif0_netmask, &fsl_netif0_gw,
&fsl_enet_config0, ethernetif0_init, ethernet_input);
netif_set_default(&fsl_netif0);
netif_set_up(&fsl_netif0);

tcpecho_raw_init();

PRINTF("\r\n************************************************\r\n");
PRINTF(" CLIENT IP\r\n");
PRINTF("************************************************\r\n");
PRINTF(" IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&fsl_netif0_ipaddr)[0], ((u8_t *)&fsl_netif0_ipaddr)[1],
((u8_t *)&fsl_netif0_ipaddr)[2], ((u8_t *)&fsl_netif0_ipaddr)[3]);
PRINTF(" IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&fsl_netif0_netmask)[0], ((u8_t *)&fsl_netif0_netmask)[1],
((u8_t *)&fsl_netif0_netmask)[2], ((u8_t *)&fsl_netif0_netmask)[3]);
PRINTF(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&fsl_netif0_gw)[0], ((u8_t *)&fsl_netif0_gw)[1],
((u8_t *)&fsl_netif0_gw)[2], ((u8_t *)&fsl_netif0_gw)[3]);
PRINTF("************************************************\r\n");

while (1)
{
/* Poll the driver, get any outstanding frames */
ethernetif_input(&fsl_netif0);

sys_check_timeouts(); /* Handle all system timeouts for all core protocols */
}
}
#endif

 

 

 

please help to solve these

0 Kudos

594 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

For Ethernet module, we use the LWIP stack, I suppose that the LWIP stack does not support socket mechanism, the socket mechanism is used in Linux.

The SDK example provides the http_srv example, pls refer to it.

xiangjun_rong_0-1678155903660.png

In the example, the LPC54018 functions as a server.

Hope it can help you

BR

XiangJun Rong

 

0 Kudos