Pages: [1]   Go Down
Author Topic: Ethernet Server expects client data first  (Read 848 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
EthernetClient client = server.available();
if(client && client.connected()) {

Thank you.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8669
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
« Last Edit: February 01, 2012, 04:46:54 pm by johnwasser » Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: