Go Down

Topic: Ethernet Server expects client data first (Read 926 times) previous topic - next topic

Zorix

I am having trouble trying to work around this issue.  What I'm trying to do is have the Arduino listen on a TCP port and when it receives a TCP connection, dump a string, then disconnect.  I don't need any client data.  However, I haven't figured a way to avoid this.  If I make a connection, the Arduino will sit there until it gets data from the client, then it will print the string and disconnect.  I have tried many different ways, but I can't figure out how to do this.
Currently I am handling it this way:
Code: [Select]

EthernetClient client = server.available();
if(client && client.connected()) {


Thank you.

johnwasser

#1
Feb 01, 2012, 10:41 pm Last Edit: Feb 01, 2012, 10:46 pm by johnwasser Reason: 1
I think your problem is the definition of server.available():

"Gets a client that is connected to the server and has data available for reading."

The code looks like this:
Code: [Select]

EthernetClient EthernetServer::available()
{
 accept();

 for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
   EthernetClient client(sock);
   if (EthernetClass::_server_port[sock] == _port &&
       (client.status() == SnSR::ESTABLISHED ||
        client.status() == SnSR::CLOSE_WAIT)) {
     if (client.available()) {
       // XXX: don't always pick the lowest numbered socket.
       return client;
     }
   }
 }


Maybe you can write something like this:

Code: [Select]

void sendAll(unsigned myPort, String text_to_send)
{
 for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
   {
   EthernetClient client(sock);
   if (EthernetClass::_server_port[sock] == myPort &&
       (client.status() == SnSR::ESTABLISHED ||
        client.status() == SnSR::CLOSE_WAIT))
       {
       client.print(text_to_send);
       client.stop();  // Close the client connection
       }
   }
}


On further looking it appears that server.write(buffer, size) does almost exactly that except it doesn't close the client.  If your client will close the connection after it receives the text you can use server.write().  Not sure how long to wait between calls to .write() to give the client a chance to close the connection before it gets the string again.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Zorix

Thanks for your detailed analysis of the information.  I was actually able to remove the check for available client in the EthernetServer.cpp file.  Doesn't seem to cause any problems in this case.  Probably not the best way to go.  I am a little confused on how to implement the sendAll function you described.

Go Up