You cannot send data to a remote host before it has initiated a connection. That just doesn't make any sense. My question is: do you want your device connect to the remote host, or do you want the remote host initiate a connection to your device?
Right now you're using netconn_accept in your device - that means that you are waiting for the remote host to initiate a connection to your device before your device can signal the remote host. This is entirely expected behavior for the code you've written yet you seem concerned about this. Is this not your intention? If not, why did you code it that way? The other alternative is to have your device initiate the connection to the remote host. There's an example of that use of netconns here. Of course, this involves changes to the other device in your system as well.
So the moral of that story is that you can't send any data when no connection is present, and you're waiting for a connection before you send data. You don't want to wait for the connection, so you have to change your software to initiate the connection rather than wait for the other side to initiate it.
Another problem you might run into is that you want to be able to send and receive data on the same connection at the same time. Most examples I saw for lwip involves blocking calls waiting for data, then reacting to that data by transmitting something back. Sometimes you want to transmit something back without receiving something first. I can help with that too.
This is what worked for me when I created a listening netconn connection.
First, you have to enable timeouts by setting:
#define LWIP_SO_RCVTIMEO 1
Then you have to set up your netconn similarly to this:
pxTCPListener = netconn_new (NETCONN_TCP);
netconn_bind (pxTCPListener, NULL, 23);
netconn_listen (pxTCPListener);
pxNewConnection = netconn_accept (pxTCPListener); //This blocks until connection is accepted
//This is the important line!
pxNewConnection->recv_timeout = 10; //note This is millseconds - lwip works in ms
//This loops until the connection is closed
while(!ERR_IS_FATAL(pxNewConnection->err)) { //Fatal errors include connections being closed, reset, aborted, etc
//This netconn_recv call will now wait 10ms for any new data, then return
if ((pxRxBuffer = netconn_recv (pxNewConnection)) != NULL)
{
//Handle received data
}
//Here, do any transmits you want
} //End of while loop from above
That code will allow you to do transmits and receives at the same time without worrying about blocking.
netconn_connect()
...I tried to use same and different port for the server and client, but still failed – eepty