LWIP netconn TCP UDP test instance

Source: Internet
Author: User
Tags gettext

This article by oneself from the network to look up the data to be already tested available

Can be used in a project with just a change

To the children's shoes in need of reference

Platform (LM3S9B92+UCOSII+LWIP)



Online IP address translator (binary decimal 16 binary conversion)

Http://www.ab126.com/goju/1840.html


implemented under the LWIP Netconn API function

TCP Server

TCP Client

UDP test



UDP Process

conn = Netconn_new (NETCONN_UDP); To create a UDP connection
Netconn_bind (Conn,ip_addr_any, 80); Bind Port number
Netconn_connect (Conn,&destip,destport);
Netconn_send (CONN,UDPNETBUF); Collect or send
NETCONN_DELETE (conn);





The main program (three threads only open a UDP TCP test is commented on each open one can be tested)

#include <includes.h> #include "lwip/sys.h" #include "user/my_tcp_client.h" #include "utils/uartstdio.h" /* Application tasks/#define Task_input_prio #define TASK_LWI P_prio 3 #define TASKINPUTSTCK 1024 #define TASKLWIPSTCK 1024 static OS_STK App_taskstartstk[app_c
Fg_task_start_stk_size];
OS_STK TASKINPUT_STACK[TASKINPUTSTCK];
OS_STK TASKLWIP_STACK[TASKLWIPSTCK];
extern struct netif lwip_netif;
extern void Stellarisif_input (void *arg);
extern void Httpd_thread (void *arg);
extern void My_lwip_init (void);

extern void Tftp_thread (void *arg);


static void App_taskstart (void *p_arg);

	int main (void) {bsp_preinit ();

	Bsp_intdisall ();                                                   Osinit ();           /* Initialize "uc/os-ii, the Real-time Kernel" * * Ostaskcreateext (void (*) (void *)) App_taskstart,
	      /* Create the start task * *          (void *) 0, (OS_STK *) &app_taskstartstk[app_cfg_task_start_stk_size-1], (int8u) App_cfg_task_start_prio, (int16u) App_cfg_task_start_prio, (OS_STK *) & Amp App_taskstartstk[0], (int32u) app_cfg_task_start_stk_size, (void *) 0, (int16u) (Os_task_opt_stk_chk |

	OS_TASK_OPT_STK_CLR));
Osstart ();


    } static void App_taskstart (void *p_arg) {(void) P_arg;                                             Bsp_postinit ();										    /* Initialize BSP functions * * tmr_tickinit ();
    
    /* Initialize the Systick * * My_lwip_init (); Ostaskcreate (Stellarisif_input, (void *) &lwip_netif, &taskinput_stack[taskinputs
	Tck-1], Task_input_prio); OstaskCreate (Httpd_thread,/*ostaskcreate (Tftp_thread, (void *) 0, &tasklwip_stack[tasklwi
                 Pstck-1], Task_lwip_prio);////* Ostaskcreate (My_tcp_client_thread, (void *) 0, &tasklwip_stack[tasklwipstck-1], Task_lwip_prio);//Sys_thread_new ("Tcpserv", my_tcp_s  Erver_thread, (void *) 0, 1024, 2);   Test TCP server//Sys_thread_new ("TCPCLT", My_tcp_client_thread, (void *) 0, 1024, 2);              Test TCP client Sys_thread_new ("UDP", My_udp_thread, (void *) 0, 1024, 2);                                               Test UDP #if (os_task_stat_en > 0) osstatinit ();
	/* Determine CPU capacity */#endif for (;;)                            {ostasksuspend (os_prio_self);                  /* The start task can pended here.

 */  
	}
}


Test program

Header file

#ifndef  __my_tcp_client_h__
#define  __my_tcp_client_h__



void my_tcp_server_thread (void *arg);
void My_tcp_client_thread (void *arg);
void My_udp_thread (void *arg);
#endif


. C file

 #include "includes.h"/* Uc/os interface * * #include "lwip/api.h" #include <stdio.h> #include <st ring.h> #include "user/my_tcp_client.h" #include "utils/uartstdio.h"//***************************************** //TCP server-side test program////********************************************************* void My_tcp_server_
    Thread (void *arg) {struct Netconn *conn, *newconn = NULL;
	struct NETBUF *tcpnetbuf;
    
    Char gettext[]= "Hello i am server!";      conn = Netconn_new (NETCONN_TCP);           /* Create a TCP connection/Netconn_bind (conn,null,80);                 /* Bind the address and the listening port number * * NETCONN_LISTEN (conn);
    /* Enter the monitoring state * * UARTPRINTF ("\033[2jtcp listening\n");    while (1) {newconn = netconn_accept (conn); /* Block current process to have data receive/if (Newconn!= NULL) {/* Send data/* * Netconn_write (Newconn
                , (void *) &gettext,sizeof (getText), netconn_nocopy);   Netbuf_delete (TCPNETBUF);      
            Netconn_close (Newconn);
      /* Close connection/while (Netconn_delete (newconn)!= err_ok) ostimedlyhmsm (0, 0, 1, 0); }}//*********************************************************/////TCP Client Test program////************************
    void My_tcp_client_thread (void *arg) {struct Netconn *conn;
struct NETBUF *tcpnetbuf;
	Char *dat;
	err_t Myerr;
	Char text[]= "Hello i am client!"; 		  static struct ip_addr ServerIP;       Target machine IP static unsigned short serverport;		 Target machine port number serverip.addr = Htonl (0XC0A8016A);    
    192.168.1.106 serverport=7000;      conn = Netconn_new (NETCONN_TCP);           /* Create a TCP connection/Netconn_bind (conn,null,7000);
	/* Bind the address and the listening port number * * * OSTIMEDLYHMSM (0, 0, 3, 0); 
	uartprintf ("\033[2jtcp connect....\n");			 Myerr=netconn_connect (Conn,&serverip,serverport);
	Connect host CNN, target IP, port number if (myerr==-1) {uartprintf ("\033[2jtcp connect err\n");}else{uartprintf ("\033[2jtcp connect ok\n");
* * Create a new NETBUF/Tcpnetbuf =netbuf_new ();
	Netbuf_alloc (Tcpnetbuf, 40);
 
    /* Quote this text to NETBUF * * NETBUF_REF (tcpnetbuf,text,sizeof (text));
			while (1) {ostimedlyhmsm (0, 0, 3, 0);
			/* Send Text * * Netconn_send (CONN,TCPNETBUF);
            Netconn_write (conn, (void *) &text,sizeof (Text), netconn_nocopy);
			OSTIMEDLYHMSM (0, 0, 3, 0);
			/* Delete conn and buf * * NETCONN_DELETE (conn);
    Netbuf_delete (TCPNETBUF); }//*********************************************************////TCP Client Test program////******************************
        void My_udp_thread (void *arg) {static struct netconn *conn;
        static struct Netbuf *udpnetbuf; 		  static struct ip_addr destip;       Target machine IP static unsigned short destport;


		Target machine port number unsigned char array[]= "Hello i am udp!";		 DESTIP.ADDR = htonl (0XC0A8016A);  
		   192.168.1.106 destport=7000;
* * To create a new NETBUF * *Udpnetbuf =netbuf_new ();       conn = Netconn_new (NETCONN_UDP);   Create UDP connection Netconn_bind (Conn,ip_addr_any, 80);
		 Bound port number while (1) {ostimedlyhmsm (0, 0, 3, 0);
		 uartprintf ("\033[2judp connec...\n");
		/* Connect Remote Host * * Netconn_connect (Conn,&destip,destport);	
		
		* * To establish a new NETBUF/udpnetbuf= netbuf_new ();
		/* Refer to this text to NETBUF */netbuf_ref (Udpnetbuf,array, sizeof (Array));
			/* Send Text * * Netconn_send (CONN,UDPNETBUF);	
		
		OSTIMEDLYHMSM (0, 0, 3, 0);
		/* Delete conn and buf * * NETCONN_DELETE (conn);	   			Netbuf_delete (UDPNETBUF);
 Free buffer}}


Finally, attach a reference to a TCP server to receive and return the received data


/* Grab New connection. */
Newconn = netconn_accept (conn);

/* Process the new connection. */
if (newconn)
{
struct NETBUF *buf;
void *data;
u16_t Len;

while ((buf = Netconn_recv (newconn))!= NULL)

{
Todo
{
Netbuf_data (buf, &data, &len);
Netconn_write (newconn, data, Len, netconn_copy);
}
while (Netbuf_next (BUF) >= 0);

Netbuf_delete (BUF);
}

/* Close connection and discard connection identifier. */
Netconn_close (Newconn);
Netconn_delete (Newconn);


Reference article http://www.amobbs.com/thread-5059971-1-1.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.