Author Topic: How do I compare a string with client.read() ?  (Read 13639 times)

0 Members and 1 Guest are viewing this topic.

Offline shermanator

  • Contributor
  • Posts: 17
  • Country: 00
  • User is banned.
How do I compare a string with client.read() ?
« on: September 05, 2013, 06:26:33 pm »
Working on testing out a server ...  I am having trouble comparing a string "test" with the client.read();

char command = client.read();
if (command == 'test') {
  server.write("it worked!");
}

it seems to work if I only compare with a single char, but not a string .. Is this only storing 1 byte or one char in the string or does the client.read() need to be converted into a string?
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9693
  • Country: us
Re: How do I compare a string with client.read() ?
« Reply #1 on: September 05, 2013, 06:30:27 pm »
I think you need to go find "ProgrammingHelpForNewbies.com"...

This is not a homework help forum for elementary programming questions   ::)
I'm not an EE--what am I doing here?
 

Offline shermanator

  • Contributor
  • Posts: 17
  • Country: 00
  • User is banned.
Re: How do I compare a string with client.read() ?
« Reply #2 on: September 05, 2013, 06:32:15 pm »
 :-DD simple question. simple answer.
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9693
  • Country: us
Re: How do I compare a string with client.read() ?
« Reply #3 on: September 05, 2013, 06:35:57 pm »
But you make the classic mistake of assuming the reader knows all the context in your head without having to write any of it down.

How is anyone supposed to know what "client" or "server" you are working with, on what system, nor where "client.read()" is defined or what its function prototype is?

And if you are coding in C++, why don't you read a C++ book?
I'm not an EE--what am I doing here?
 

Offline TopLoser

  • Supporter
  • ****
  • Posts: 1901
  • Country: gb
Re: How do I compare a string with client.read() ?
« Reply #4 on: September 05, 2013, 06:39:31 pm »
2 seconds with google and:

http://arduino.cc/en/Reference/ClientRead

client.read returns a single character. Not a string.


 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: How do I compare a string with client.read() ?
« Reply #5 on: September 05, 2013, 06:42:59 pm »
:-DD simple question. simple answer.

I don't know the answer to my question, but I'm still pretty sure it's simple. Explain, lackeys!

FTFY
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline shermanator

  • Contributor
  • Posts: 17
  • Country: 00
  • User is banned.
Re: How do I compare a string with client.read() ?
« Reply #6 on: September 05, 2013, 06:46:47 pm »
If you don't know why i'm asking or what i'm asking in reference to , then it would be an intelligent response to ask what it is in reference to (if you cannot already see the obvious) rather than assume i'm a "newbie" who's doing a homework assignment  :clap: 

this actually isn't c++ , if you cannot already tell
it's not BASIC
it's not PERL
it's not JAVASCRIPT
it's not PHP
it's not FORTRAN
 :-DD

I'm writing a small program on an Arduino board to test something very basic. I've noticed a lot of questions here regarding Arduino code , hence why i started the thread.

 

Offline shermanator

  • Contributor
  • Posts: 17
  • Country: 00
  • User is banned.
Re: How do I compare a string with client.read() ?
« Reply #7 on: September 05, 2013, 06:47:48 pm »
:-DD simple question. simple answer.

already aware of that link , my question is not in reference to this

"client.read returns a single character. Not a string."

as stated in my post , that's obvious. my question is returning a string and comparing to another string , rather than comparing a single character to a string
« Last Edit: September 05, 2013, 06:52:55 pm by shermanator »
 

Offline DavidDLC

  • Frequent Contributor
  • **
  • Posts: 740
  • Country: us
Re: How do I compare a string with client.read() ?
« Reply #8 on: September 05, 2013, 06:51:17 pm »
So sad to see that people is lately attacking too much to the forum members.

If you do not have something positive to say, just do not do it.

Even if it is homework, if you see that the OP at least is trying something ( not asking how to do it from scratch, or asking somebody else to do it for the OP ), just help. I do not see Shermanator trying to get some advantage from somebody else.

David.
 

Offline 8086

  • Super Contributor
  • ***
  • Posts: 1086
  • Country: gb
    • Circuitology - Electronics Assembly
Re: How do I compare a string with client.read() ?
« Reply #9 on: September 05, 2013, 06:51:53 pm »
If you don't know why i'm asking or what i'm asking in reference to , then it would be an intelligent response to ask what it is in reference to

It would be more intelligent to post the context yourself.

And, indeed, to not piss off those you expect to help you.
 

Offline TopLoser

  • Supporter
  • ****
  • Posts: 1901
  • Country: gb
Re: How do I compare a string with client.read() ?
« Reply #10 on: September 05, 2013, 06:56:10 pm »
You're going to have to read one character at a time and build a string out of them. Probably look for a CR character to indicate the end of a line from the client. Compare that string with what you're looking for.
 

Offline shermanator

  • Contributor
  • Posts: 17
  • Country: 00
  • User is banned.
Re: How do I compare a string with client.read() ?
« Reply #11 on: September 05, 2013, 06:56:39 pm »
"piss off those you expect to help you"

I didn't know you all were so sensitive.

I'll make sure to go ask on a less sensitive forum.

I wasn't aware this was a sensitive crowed that acts like they know shit and assume they know who you are then state they don't know what you're talking about and then respond with cocky dribble because they're somehow "offended" instead of acting like real programmers and communicating a basic answer or at least a somewhat-intelligent response.  :-//

I'll go visit a different forum with real programmers. Adios. Pussies.  :-DD

 :--


« Last Edit: September 05, 2013, 07:01:10 pm by shermanator »
 

Offline shermanator

  • Contributor
  • Posts: 17
  • Country: 00
  • User is banned.
Re: How do I compare a string with client.read() ?
« Reply #12 on: September 05, 2013, 06:57:50 pm »
You're going to have to read one character at a time and build a string out of them. Probably look for a CR character to indicate the end of a line from the client. Compare that string with what you're looking for.

yes, that's obvious.
I am looking for a simple way around that , similar to how you would perform this in C++, perl, php or any other language.
 

Offline 8086

  • Super Contributor
  • ***
  • Posts: 1086
  • Country: gb
    • Circuitology - Electronics Assembly
Re: How do I compare a string with client.read() ?
« Reply #13 on: September 05, 2013, 07:00:40 pm »
I thought you were off.

Bye. Miss you already.
 

Offline shermanator

  • Contributor
  • Posts: 17
  • Country: 00
  • User is banned.
Re: How do I compare a string with client.read() ?
« Reply #14 on: September 05, 2013, 07:06:56 pm »
:blah:

i'll delete the thread so you can all quit your crying
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9693
  • Country: us
Re: How do I compare a string with client.read() ?
« Reply #15 on: September 05, 2013, 07:18:54 pm »
So sad to see that people is lately attacking too much to the forum members.

We are not attacking forum members, we are criticizing a behavior.

The post at the top of the thread was post #1.

This was someone new swooping in out of the blue, with no background and no context, and the first thing they say is "Hey, give me an answer to my simple problem!"

That is simply not polite. You would not burst through the door of a tech meet in real life and, without even saying hello, start out with a basic question like "Hey! How do I wire a battery to a bulb to make the bulb light up?"

Nobody is owed an answer to their rudely asked questions from a group of strangers. This is not a Q&A forum like Stack Overflow.
I'm not an EE--what am I doing here?
 

alm

  • Guest
Re: How do I compare a string with client.read() ?
« Reply #16 on: September 05, 2013, 07:28:18 pm »
Maybe it would be useful to have a sticky similar to How to ask questions the smart way to refer people to?
 

Offline Crazy Ape

  • Regular Contributor
  • *
  • Posts: 181
Re: How do I compare a string with client.read() ?
« Reply #17 on: September 05, 2013, 07:41:27 pm »
Maybe it would be useful to have a sticky similar to How to ask questions the smart way to refer people to?

It could be called "How to piss off those you expect to help you" with a reference to this thread.  :-DD
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 5261
  • Country: de
  • A qualified hobbyist ;)
Re: How do I compare a string with client.read() ?
« Reply #18 on: September 05, 2013, 07:52:32 pm »
this actually isn't c++

Using client.read() and server.write("it worked!") tells something different.
 

Offline baljemmett

  • Supporter
  • ****
  • Posts: 666
  • Country: gb
Re: How do I compare a string with client.read() ?
« Reply #19 on: September 05, 2013, 09:47:27 pm »
acting like real programmers

'Real' programmers don't post completely context-free questions and expect those from whom they're seeking help to guess at everything else (such as what language you're using, what libraries you're coding against, what progress you've made on your problem so far and whether you have the slightest clue what you're doing).  They also don't act like spoilt brats with the "yeah, I know that, but I want you to tell me what tools I have in my toolbox despite not telling you what that might be" attitude when someone does in fact attempt to help.

Unfortunately you don't appear to realise that, which might explain why the response you got was more akin to "FOAD, HTH, HAND"; somehow I suspect you won't actually learn anything from the experience, though.
 

Offline Bored@Work

  • Super Contributor
  • ***
  • Posts: 3932
  • Country: 00
Re: How do I compare a string with client.read() ?
« Reply #20 on: September 05, 2013, 09:59:32 pm »
The sperminator is now pestering "real programmers" *cough* on an arduino forum, where he is properly spoon feed.
I delete PMs unread. If you have something to say, say it in public.
For all else: Profile->[Modify Profile]Buddies/Ignore List->Edit Ignore List
 

Offline baljemmett

  • Supporter
  • ****
  • Posts: 666
  • Country: gb
Re: How do I compare a string with client.read() ?
« Reply #21 on: September 05, 2013, 10:10:25 pm »
The sperminator is now pestering "real programmers" *cough* on an arduino forum, where he is properly spoon feed.

Look how shocked I am.  No, really.   ::)
 

Offline shermanator

  • Contributor
  • Posts: 17
  • Country: 00
  • User is banned.
Re: How do I compare a string with client.read() ?
« Reply #22 on: September 05, 2013, 10:47:25 pm »
Figured it out myself as usual  :-/O
I appreciate the open-source community and for those who contribute , I don't want somebody with the same question to stumble upon this thread only to hit a dead end so i'm posting the successful code below:

Simple chat server with command feature , reads client.read() and stores data into string 'inData' then echos string to server (to confirm we have it). If client types "help" in the server, it back specific text to the client.

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 4);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 255, 0);

// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously

String inData;

void setup() {
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);
  // start listening for clients
  server.begin();
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Chat server address:");
  Serial.println(Ethernet.localIP());
}



void loop() {
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clead out the input buffer:
      client.flush();   
      Serial.println("We have a new client");
      client.println("Hello, client!");
      alreadyConnected = true;
    }

   if (client.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      inData += thisChar;
      // echo the bytes back to the client:
      server.write(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);


// echo to the server what's been received to confirm we have the string
if (thisChar == '\n')
        {
          Serial.print("\nreceived:");
          Serial.print(inData);
              inData = "";         
      }


// if the string is "help" then return message to client

if (inData == "help") {
server.write("\nYou said help!\n");
}
        }
        }
}
 

Offline shermanator

  • Contributor
  • Posts: 17
  • Country: 00
  • User is banned.
Re: How do I compare a string with client.read() ?
« Reply #23 on: September 05, 2013, 10:54:43 pm »
We are not attacking forum members, we are criticizing a behavior.

So you're criticizing behavior based on a question you don't understand. You call somebody a newbie whom you don't even know. You claim they are doing a homework assignment when you have no idea what they're even working on.

Real mature "behavior"   :clap:

Or am I mistaken , you are the self-proclaimed Mr. Know-it-all? If so I sincerely apologize, your heinous
 :-DD
« Last Edit: September 05, 2013, 11:04:10 pm by shermanator »
 

Offline shermanator

  • Contributor
  • Posts: 17
  • Country: 00
  • User is banned.
Re: How do I compare a string with client.read() ?
« Reply #24 on: September 05, 2013, 10:57:36 pm »
yeah, I know that, but I want you to tell me what tools I have in my toolbox despite not telling you what that might be

Scroll up and read the thread captan obvious. My response has nothing to do with what's in my toolbox or not receiving an answer. It has to do with the over-sensitive muppets who like to dribble accusatory remarks because they're too full of themselves to respond in the simple words, like a real programmer would, i.e: "what are you referring to?". It's much more efficient to get to the bottom of something even if you have to admit you don't know what someone is talking about.. It's really that easy. No need to go on a tangent and throw a shit-fit because you assume somebody (you don't even know) is a newbie (because they ask a question you don't understand. That's all.
« Last Edit: September 05, 2013, 11:02:27 pm by shermanator »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf