d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Printing Error In Chat Program
12Next
Add Reply New Topic New Poll
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Sep 23 2013 12:21pm
"Check the length of the messages that the chat program run in client mode sends to the chat program run in server mode. If the length of the message is more than 140 characters, the chat program run in client mode should not send the message and instead print an error."

Not real sure on how to make this happen, I'm very new to C and struggling.

I need to make it so it won't allow you to send the message if it's over 140 characters to the server or client when using this chat program. I need a message to pop up saying, can't do this, please try again.

Thanks for your help in advance! :)
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Sep 23 2013 12:32pm
1) open console
2) man strlen
3) ???
4) profit

pic of console output in case you got no console:
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Sep 23 2013 12:34pm
note: strnlen is even better -_-
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Sep 23 2013 12:39pm
Quote (Richter @ Sep 23 2013 01:34pm)
note: strnlen is even better  -_-


I appreciate this information, I'll look into here after class is over and try to implement it in the next couple hours :)
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Sep 23 2013 12:47pm
Am I also going to need an exception handler on this or is this the exception handler in itself?
Putting the max length but if I want it to say "Error, your line is too long to send it's over 140 characters, please try again"

Guess I'm just kinda confused, I'm extremely new to C.

This post was edited by Trev on Sep 23 2013 12:48pm
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Sep 23 2013 01:01pm
exception handler? you talking about try and catch?
googling for try catch and c leads to this page: http://stackoverflow.com/questions/10586003/try-catch-statements-in-c
as you see, it's obviously not what you should look for.

i think you only should write the client?
1) read in the text you should send (until enter is pressed i guess)
2) use strnlen to see if the message is too long or not
3) us "if" to distinct two cases
3a) message too long: printf("too long!\n");
3b) message short enough: send message
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Sep 23 2013 02:53pm
Quote (Richter @ Sep 23 2013 12:01pm)
1) read in the text you should send (until enter is pressed i guess)
2) use strnlen to see if the message is too long or not
3) us "if" to distinct two cases
3a) message too long: printf("too long!\n");
3b) message short enough: send message

this
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Sep 23 2013 07:31pm
Code
while (fgets(buf, sizeof(buf), stdin)) {
buf[MAX_LINE-1] = '\0';
len = strlen(buf) + 1;
send(s, buf, len, 0);


into??

Code
while (fgets(buf, sizeof(buf), stdin)) {
if strlen (140 > buf[MAX_LINE-1] = '\0';) {
printf("Message too long!\n")
else
len = strlen(buf) + 1;
send(s, buf, len, 0);
}



Wow my coding sucks, I have no idea where to put the {} and I know that's not even right lol...

This post was edited by Trev on Sep 23 2013 07:32pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 23 2013 07:42pm
just do it immediately before the send since you already calculated length

though from the look of it, you're sending it in pieces of size buf? so buf has to be > 140 for it to work
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Sep 23 2013 07:53pm
Quote (carteblanche @ Sep 23 2013 08:42pm)
just do it immediately before the send since you already calculated length

though from the look of it, you're sending it in pieces of size buf? so buf has to be > 140 for it to work


Have this, which can be changed.
#define MAX_LINE 256

Few variables:
char buf[MAX_LINE];
int s, new_s;
int len;

Yeah I believe so, sending buf, how would that look in code?
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll