Recv_mbuf receiving extra characters

Hi All,

Please help with this small problem,

  1. My goal is: to write a simple TCP terminal, ultimately to be used as an SMTP client on an ESP-32.
  2. My actions are: My code is here …
#include "mongoose.h"
#include "mgos.h"
#include <xtensa/hal.h>
#include "string.h"
#include <esp_system.h>

const char *port1 = "tcp://192.168.4.2:1993";
struct mg_mgr mgr;
struct mg_connection *nc;
int timenow; int timeup; int i=0;int exitflag=0;
char messtring[2048]; char numstring[12]; char tcpstring[256];
uintptr_t swtt = 0; uintptr_t swtt1 = 0; 											

static void ev_handlertcp(struct mg_connection *kc, int ev, void *ev_data, void *ud) {
  switch (ev) {
    case MG_EV_CLOSE:
		exitflag=1;
		mg_mgr_free(&mgr);
		printf("tcp connection closed\n");
	break;	
	
	case MG_EV_CONNECT:
		printf("tcp connection start\n");
    break;
	
	case MG_EV_RECV:
		printf("message received from packet sender\n");
		printf("%s",kc->recv_mbuf.buf);
		mbuf_remove(&kc->recv_mbuf,kc->recv_mbuf.len);
		printf("\n");
	break;
	  
    default:
      break;
	}
}

static IRAM void led_timer_cb(void *arg) {	
	timeup = (int) mgos_uptime(); timenow = (int) time(0);
	if (timeup<60||exitflag==1) {printf("System Up Time:  %d  Time Now  %d \r\n",timeup,timenow);}
		messtring[0]='\0';	
		strcpy(messtring,"Time Up:     "); sprintf(numstring,"%d",timeup); strcat(messtring,numstring);
		strcat(messtring,"   Time Now:     ");sprintf(numstring,"%d",timenow);strcat(messtring,numstring);
		strcat(messtring,"\n");
			if (timeup>60&&exitflag==0) {mg_send(nc, messtring, strlen(messtring));}			
				if (exitflag==0) {mg_mgr_poll(&mgr,100);}
		(void) arg;
}

static IRAM void connect_timer_cb(void *arg) {	
	nc = mg_connect(&mgr, port1, ev_handlertcp,NULL);					
	(void) arg;
}

enum mgos_app_init_result mgos_app_init(void) {			
	mg_mgr_init(&mgr, NULL);										
	swtt= mgos_set_timer(5000, MGOS_TIMER_REPEAT, led_timer_cb, NULL);	
	swtt1= mgos_set_timer(40000, 0, connect_timer_cb, NULL);	
return MGOS_APP_INIT_SUCCESS;
}
  1. The result I see is: The program works, and is able to transmit and receive information to Packet Sender. A more complicated version is able to send e-mails via my organization’s SMTP server. However, in both cases extra characters intermittently appear in recv_mbuf, as shown below.
    The intended messages are:
    1
    22
    333
    etc

The ? ? ? stuff is the intermittently received undesired stuff.

............
[Aug  5 15:12:40.143] e[0;32mI (11212) event: sta ip: 192.168.1.19, mask: 255.255.255.0, gw: 192.168.1.1e[0m
[Aug  5 15:12:42.202] System Up Time:  9  Time Now  9
[Aug  5 15:12:47.199] System Up Time:  14  Time Now  14
[Aug  5 15:12:48.690] I (19762) wifi: n:5 2, o:5 0, ap:5 2, sta:5 0, prof:5
[Aug  5 15:12:48.691] I (19772) wifi: station: 80:2b:f9:5c:65:77 join, AID=1, bgn, 40D
[Aug  5 15:12:48.694] e[0;32mI (19802) tcpip_adapter: softAP assign IP to station,IP is: 192.168.4.2e[0m
[Aug  5 15:12:52.194] System Up Time:  19  Time Now  19
[Aug  5 15:12:57.206] System Up Time:  24  Time Now  24
[Aug  5 15:13:02.204] System Up Time:  29  Time Now  29
[Aug  5 15:13:07.201] System Up Time:  34  Time Now  34
[Aug  5 15:13:12.199] System Up Time:  39  Time Now  39
[Aug  5 15:13:17.194] System Up Time:  44  Time Now  44
[Aug  5 15:13:22.181] System Up Time:  49  Time Now  49
[Aug  5 15:13:22.182] tcp connection start
[Aug  5 15:13:27.200] System Up Time:  54  Time Now  54
[Aug  5 15:13:32.209] System Up Time:  59  Time Now  59
[Aug  5 15:13:37.182] message received from packet sender
[Aug  5 15:13:37.183] 1  ?i  ?H  ?
[Aug  5 15:13:47.182] message received from packet sender
[Aug  5 15:13:47.182] 22 ?   ?L  ?
[Aug  5 15:13:57.206] message received from packet sender
[Aug  5 15:13:57.207] 333?i  ?H  ?   ?   ?
[Aug  5 15:14:07.209] message received from packet sender
[Aug  5 15:14:07.209] 4444i  ?H  ?
[Aug  5 15:14:17.209] message received from packet sender
[Aug  5 15:14:17.209] 55555  ?i  ?H  ?   ?
[Aug  5 15:14:27.208] message received from packet sender
[Aug  5 15:14:27.209] 666666 ?i  ?H  ?   ?   ?
[Aug  5 15:14:37.211] message received from packet sender
[Aug  5 15:14:37.212] 7777777?i  ?H  ?   ?
[Aug  5 15:14:47.198] message received from packet sender
[Aug  5 15:14:47.199] 88888888i  ?H  ?   ?
[Aug  5 15:14:57.204] tcp connection closed
[Aug  5 15:15:02.203] System Up Time:  149  Time Now  149
..........
  1. My expectation & question is: What is causing this? How to fix it?

Thanks as always for your continued assistance,

JSW

recv_mbuf is not null terminated:
printf("%.*s",kc->recv_mbuf.len,kc->recv_mbuf.buf);

Hi nliviu,

This works perfectly.

Thanks!

JSW