UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » Chat server wouldn't work

#51 2007-03-20 03:29 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

Offline

#52 2007-03-20 04:04 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Chat server wouldn't work

Urg, I missed that you were reading one byte at a time, and that that function is called again and again all the time.

Add some debugging printfs (e.g. printing out the tmp content might be useful), and run the thing through Valgrind.

Offline

#53 2007-03-20 04:06 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

I did. But it seems all okay. Although it isn't working.?

Offline

#54 2007-03-20 06:50 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Chat server wouldn't work

Wait, I don't see any loop inside that function...  Are you just repeatedly calling the
function for every individual character to read?  If so, there's your explanation: your
"tmp" buffer is on the stack, so every time you start the function, you're getting a new
stack buffer, and initializing it to an empty string...  If you want it to maintain state,
you'll need to make it static, and not manually null it out (except probably once you
get the newline, and process it)...  Or, you'll need some other method...  Like maybe
reading more than a single char at a time...  (Which is horribly inefficient anyway, so
you shouldn't generally be doing it...)

Offline

#55 2007-03-20 08:46 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

Offline

#56 2007-03-21 12:29 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Chat server wouldn't work

Offline

#57 2007-03-22 06:43 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

Offline

#58 2007-03-22 08:10 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Chat server wouldn't work

I'm not entirely sure what's going on...  But, this is wrong:

printf("-->> %d <> %s",i,atoi(u[i].chat));

You're using "%s", but passing it an int (the return value from atoi())...  I think you
want to remove the atoi() there...  Or, change to "%d"...

Offline

#59 2007-03-22 08:12 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

I tried both. But they are both 0. And thats the result in the rest of the program. It gets screwed when I got a 'chat request' from another client.:confused:

Offline

#60 2007-03-22 09:33 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Chat server wouldn't work

It contains the string "0", or merely something that atoi() returns the value 0 for?
Those could be very different things...

It looks like 0 is a perfectly valid client ID, so are you sure it's not supposed to be 0?

Oh, wait, I think I see the issue...  The client you are calling request_accept() on
will be the target client which the original client initiated a call to...  And, you never
fill in THAT client's "chat" value inside request_call()...

Plus, as it stands now, the original requesting client could essentially force the
chat open by sending a "y" after requesting, because you leave it in STATE_REQUESTCALL
as well...  You probably need a different state here to distinguish the two cases...
I'd make the target client something like STATE_AWAITREPLY or something, and
only do the request_accept() for that state...

Offline

#61 2007-03-22 10:12 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

Offline

#62 2007-03-22 10:49 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Chat server wouldn't work

But, handle_connections() only seems to be called for newly connected clients,
after prompting them for who they want to talk to...  And, their response has to be
an already connected client...  And, THAT already connected client is the one you
need to change the value of "chat" for, since some newly connected client now
wants to chat with it...

But, that seems a very poor design...  The very first client to connect won't have
anyone else to establish a connection with, since no one else is already connected,
and no future clients will be able to talk to the first client, because he's not yet in
STATE_CHAT...  Basically, you need to rethink your design, I think...

Offline

#63 2007-03-23 04:15 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

Hmm. handle_connections() is called in the for-loop too... So what is the problem? :confused: I am really trying to understand it, but I cant figure out.

Offline

#64 2007-03-23 06:27 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Chat server wouldn't work

It's only called for the client who is initiating the request, NOT for the target of that
request...  As best as I can see, anyway...  And, it then changes the state of the
target (as well as the requester) to STATE_REQUESTCALL...  So, no, the target
just will never go through handle_connections() in that case, from what I can tell...
(And, I don't think you want it to, or you'd have an infinite loop, where they'd each
keep repeatedly trying to request a chat with the other...)  You already know what
value to fill the target's "chat" value with: the index of the requester...  So, there's
no need to go through the handle_connections() prompting, anyway...  You just
want request_call() to fill it in when it informs the target that the requester wants to
chat...

Offline

#65 2007-03-24 11:14 AM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

Ahh, okay. I get it.
But: Where do I have to change my request_call() in then?

Offline

#66 2007-03-24 06:24 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Chat server wouldn't work

void request_call( int i ) {
    int as = atoi(u[i].chat);
    if(u[i].state == STATE_REQUESTCALL) {
        if(u[as].state != STATE_CHAT) {
            u[as].state = STATE_REQUESTCALL;
/**/        sprintf (u[as].chat, "%d", i); /**** This is the change you need! ****/
            send(u[as].fd,"\rINCOMMING CALL! Do you want a conversation with ",49,0);
            send(u[as].fd,u[i].username,strlen(u[i].username),0);
            send(u[as].fd,"\r\n<y/n>",7,0);
        } else {
            send(u[i].fd,"Other client is currently busy.",31,0);
        }
    } else {
        perror("Function request_call() was called without the good state.");
    }
}

Really, you probably should just use an integer to store the index of the peer a given
client is talking to, rather than a char buffer, anyway...

Also, on a side note, in the above code you correctly use "\r\n" (though, I'm not sure
it makes so much sense to use it at that particular spot in the data you're sending,
but whatever), however I notice in lots of the other code you incorrectly send "\n\r",
which you probably don't want to do...

Offline

#67 2007-03-24 06:44 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

What difference does it make to first carriage return the pointer, and than newline it, or first newline and than carriage return it?

Offline

#68 2007-03-24 06:47 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

It works fine now, but the person who says: 'no, I dont wanna chat' gets the message if he wants to chat with hisself...

Offline

#69 2007-03-24 08:16 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Chat server wouldn't work

Offline

#70 2007-05-05 03:01 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

Offline

#71 2007-05-05 06:02 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Chat server wouldn't work

Well, one issue is that probably there will be another "\n" sent by the client following
the "y" or "n", I imagine, and your code for determining when a client has entered a
full number of another client to chat with simply looks for a "\n"...  So, that might be
the problem: that stray newline is being misinterpreted as ending the entry of a
client# to chat with, when in fact it's just left over from the previous y/n prompt...

Basically, I just really don't like the way you're handling your I/O here, at all...  This
silly business with reading a single char at a time and strcat()'ing it up into a full
response is just crazy...  Not to mention horribly inefficient...  You really should just
design a function to read a full line of input, up to and including the trailing "\n" (which
you will then require all client input lines to end with, as part of your protocol)...  You
can do that multiple ways, all of which have been discussed here on the forums many
times in the past...  If you make sure to always read input via this new readline function
of yours, then the easiest way to implement it is to use fdopen() to create a stdio
file pointer from the socket FD (do this ONCE and keep the file pointer stored in
your user struct), then just use fgets() to read the lines...  Alternatively, if you don't
want to (or can't) use stdio, then you can just use recv(MSG_PEEK) into a big
buffer and hunt for a "\n" within it, then from the location of that within the buffer,
you will know exactly how much to really read (without MSG_PEEK) to read the
entire line...

But, the point is, you really should strictly define your network protocol and adhere
to it, rather than just have this mish-mash of kluged-together I/O you've got going
on now...  Just doing that will likely solve a lot of your problems, or at least make it
a hell of a lot easier to debug, anyway...

Offline

#72 2007-05-05 08:38 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Chat server wouldn't work

Hmm, I think I know what my problem is (are).
Does somebody here have a well-written chat-server example?

Offline

  • Index
  • » C
  • » Chat server wouldn't work

Board footer

Powered by FluxBB