/* Create a new UDP connection handle */
conn = netconn_new(NETCONN_UDP);
if (conn!= NULL)
{
/* Bind to port 80 (HTTP) with default IP address */
err = netconn_bind(conn, NULL, 80);
if (err == ERR_OK)
{
err = netconn_connect(conn, &ipaddr, 80);
if(err == ERR_OK)
{
printf("Connect server succeed ! \n");
/* Prepare data */
buf_send = netbuf_new();
payload_len = strlen("Hello server !");
data = netbuf_alloc(buf_send, payload_len);
memcpy (data, "Hello server !", payload_len);
/* Send the packet */
netconn_sendto(conn, buf_send, &ipaddr, 80);
/* Free the buffer */
netbuf_delete(buf_send);
while(1)
{
LCD_DbgLog("Wait for UDP data ...");
while(netconn_recv(conn, &buf) != ERR_OK);
LCD_DbgLog(" [OK] ...\n");
/* Get destination ip address and port*/
addr = netbuf_fromaddr(buf);
port = netbuf_fromport(buf);
/* Get the payload and length */
payload_len = buf->p->len;
payload_data = buf->p->payload;
/* Prepare data */
buf_send = netbuf_new();
data = netbuf_alloc(buf_send, payload_len);
memcpy (data, payload_data, payload_len);
/* Send the packet */
netconn_sendto(conn, buf_send, addr, port);
/* Free the buffer */
netbuf_delete(buf_send);
netbuf_delete(buf);
}
}
else
{
LCD_DbgLog("Connect server fail ! \n");
}
}
}
}