diff --git a/bspMCU/My_Lwip.c b/bspMCU/My_Lwip.c index 7ed4fb8..39c9c9d 100644 --- a/bspMCU/My_Lwip.c +++ b/bspMCU/My_Lwip.c @@ -59,6 +59,11 @@ *----------------------------------------------*/ #define PORT 23 // 监听的端口号 #define RECV_SIZE 1024 // 接收缓冲区大小 +#define IAC 255 +#define WILL 251 +#define DO 253 +#define ECHO 1 +#define SGA 3 static int connect_fd = -1; //socket套接字 #if 0 @@ -142,7 +147,20 @@ void luaprint_talnet(const char *fmt, ...) va_end(args); if (len > 0 && connect_fd >= 0) { - write(connect_fd, temp, len); + // 将 \n 转换为 \r\n,分段发送 + int start = 0; + for (int i = 0; i < len; i++) + { + if (temp[i] == '\n') + { + if (i > start) + write(connect_fd, &temp[start], i - start); + write(connect_fd, "\r\n", 2); + start = i + 1; + } + } + if (start < len) + write(connect_fd, &temp[start], len - start); } } @@ -200,20 +218,13 @@ void vTCPServer_Task(void *pvParameters) #ifdef USE_TALNET lua_init(luaprint_talnet); + // 发送telnet协商:服务端负责回显,进入字符模式 + unsigned char telnet_neg[] = { + IAC, WILL, ECHO, + IAC, WILL, SGA + }; + write(connect_fd, telnet_neg, sizeof(telnet_neg)); #endif -// // 第一条指令:要求客户端关闭本地回显,交由服务器处理 -// unsigned char cmd_echo[] = { 255, 254, 1 }; -// // IAC(255) DO(253) ECHO(1) -// -// // 第二条指令:禁用“前进抑制”,强制进入实时字符流模式 -// unsigned char cmd_sga[] = { 255, 254, 3 }; -// // IAC(255) DO(253) SUPPRESS-GO-AHEAD(3) -// -// write(connect_fd, cmd_echo, sizeof(cmd_echo)); -// write(connect_fd, cmd_sga, sizeof(cmd_sga)); - -// int enable = 1; // 1 表示启用 TCP_NODELAY(即禁用 Nagle 算法) -// setsockopt(connect_fd, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)); // 打印连接的客户端 IP 和端口 log_i("New client connected: %s:%d\r\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); @@ -230,20 +241,9 @@ void vTCPServer_Task(void *pvParameters) if (ret > 0) { #ifdef USE_TALNET - lua_repl(temp, ret, 0); + lua_repl(temp, ret, 1); #else write(connect_fd, temp, ret); -#endif -#if 0 - if (xSemaphoreTake(xMutex, portMAX_DELAY) == pdTRUE) - { - int iLen = rd_RingbufferPutForce(g_rb_handle, &temp, ret); - xSemaphoreGive(xMutex); - if (iLen > 0) - { - xSemaphoreGive(xSemaphore); - } - } #endif } else if (ret == 0)