0

I am using ESP8266 with the SMING framework.

I found this sample code in the Telnet_TCPServer_TCPClient example.

bool tcpServerClientReceive (TcpClient& client, char *data, int size)
{
   debugf("Application DataCallback : %s, %d bytes \r\n", client.getRemoteIp().toString().c_str(),size );
   debugf("Data : %s", data);
   client.sendString("sendString data\r\n", false);
   client.writeString("writeString data\r\n",0 );
   if (strcmp(data,"close") == 0)
   {
      debugf("Closing client");
      client.close();
   };
   return true;
}

What is the difference between sendString() and writeString() in this sample code? It seems like there is no difference. The outcome is to send string data over to the other TCP party.

4

1 回答 1

2

示例中使用的客户端对象是一个实例,TcpClient而该实例又是对象的派生类TcpConnectionTcpClient确实有一个.sendString方法,并且该.writeString方法是该类的方法TcpConnection(的父类TcpClient

实际上有两种重载方法.writeString

因此,将所有这些信息排除在外,基本上就是这样做的.sendString(在该方法内部调用该.send方法):

if (state != eTCS_Connecting && state != eTCS_Connected) return false;

if (stream == NULL)
    stream = new MemoryDataStream();

stream->write((const uint8_t*)data, len);
asyncTotalLen += len;
asyncCloseAfterSent = forceCloseAfterSent;

return true;

.write方法执行此操作:

   u16_t available = getAvailableWriteSize();
   if (available < len)
   {
       if (available == 0)
           return -1; // No memory
       else
           len = available;
   }

   WDT.alive();
   err_t err = tcp_write(tcp, data, len, apiflags);

   if (err == ERR_OK)
   {
        //debugf("TCP connection send: %d (%d)", len, original);
        return len;
   } else {
        //debugf("TCP connection failed with err %d (\"%s\")", err, lwip_strerr(err));
        return -1;
   }

所以从外观上看,一个是异步的(.sendString),另一个不是(.writeString)。

于 2016-02-02T15:28:36.513 回答