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

#26 2006-12-29 09:13 PM

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

Re: Chat server wouldn't work

I'm in a good mood, so I'll give you some big hints:

The error is on the line containing:

ufds[i].revents = POLLIN, POLLOUT;

Why, of all possibilities, do you set the revents field?

(For answers, use brain or poll manpage. And no, I don't mean the ',' versus '|'.)

The problem isn't that your code is ugly, the problem is that it misses design and structure, that your way of solving your problems is wrong.

Offline

#27 2006-12-30 10:27 AM

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

Re: Chat server wouldn't work

ahhh I get it.

1) I was forgotten my for loop  (use a "i", but not declared ;-) )
2) .revents had to be .events
3) forgotten to set .fd

isn't it? :rolleyes:

Offline

#28 2007-01-01 12:11 PM

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

Re: Chat server wouldn't work

rc = read(u[i],&buf,1);
                    if (rc == 0) {
                        delete_old_user(i);
                        printf("--test\n");
                    }

Offline

#29 2007-01-01 01:28 PM

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

Re: Chat server wouldn't work

If you use blocking sockets and call read when there is no data (POLLIN is not set), then the call will indeed block. If you don't want that then don't call read and/or set the socket to non-blocking.

Just remove it, it makes no sense to test if a client is connected or not in this way, you're using TCP. Better to send a ping message which must be replied to within a certain time, and if that doesn't happen disconnect the client. Or something.

Offline

#30 2007-01-01 01:41 PM

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

Re: Chat server wouldn't work

Okay, That ping thing is going to happen too. But it works now! (forgotten to set .fd -_-' )

Offline

#31 2007-01-04 12:32 PM

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

Re: Chat server wouldn't work

void get_username(int i) {
/*
 * Gets username from a connecting socket
 */
    int newfd;
    newfd = u[i].fd;
    char buf;
    int j;
    if(u[i].state == STATE_GETUSERNAME) {
        printf("test\n");
        if(recv(newfd,&buf,1,0) == -1) {
            perror("Get_username recv() went wrong...");
        }
        if(buf == '\n') {
            printf("Username: %x\n",u[i].username);
            u[i].state = STATE_SHOWCONNECTIONS;
        } else {
            for(j = 0; j < USERNAME; j++) {
                if(u[i].username[j] == '\0') {
                    u[i].username[j] = buf;
                    u[i].username[j + 1] = '\0';
                }
            }
        }
    } else {
        perror("Function get_username() was called without the good state.");
    }
}

Offline

#32 2007-01-04 03:02 PM

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

Re: Chat server wouldn't work

Erm, if you don't want to receive terminal control characters, then don't use terminal software. Calling it rubbish while it tries to talk to your server is a bit rude.

Just ignore all characters that aren't alphanumerical or whitespaces.

Offline

#33 2007-01-04 03:11 PM

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

Re: Chat server wouldn't work

what does that characters mean?

Offline

#34 2007-01-04 03:47 PM

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

Re: Chat server wouldn't work

Offline

#35 2007-01-04 04:58 PM

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

Re: Chat server wouldn't work

Offline

#36 2007-01-04 10:01 PM

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

Re: Chat server wouldn't work

Ow, my head!  The nesting of that code almost gave me a brain aneurysm trying
to decipher!  Please, learn to use "else if" constructs, instead of nesting ALL sub-ifs
inside the braces of the preceding "else"!

But, anyway, your main problem is that you first read a single char in main(), after
poll() returns, THEN call get_username() which reads another char and stores only
it in the username...  So, you're reading only every other char into the username...

Offline

#37 2007-01-05 03:27 PM

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

Re: Chat server wouldn't work

Host down, so I can't share in the fun. I can guess though: Please split those huge functions up into multiple smaller ones.

Offline

#38 2007-01-05 09:32 PM

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

Re: Chat server wouldn't work

Offline

#39 2007-01-05 10:35 PM

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

Re: Chat server wouldn't work

I think I see what was contributing to my brain hemorrhage: you're using 4-char
indents, but 8-char tabs in that source file... ;-/  I have everything (well, vi and less,
at least) set to use 4-char tabs...  So, when seeing your source file, all double
indents (which you inappropriately represent by a single tab) end up looking like a
single indent (which you represent by 4 space chars) to me, which is what made
everything so hard to parse...  You really should adjust settings in your editor to
either use 4-char tabs, and then use tabs for all indents, or use spaces for all indents
(make sure your editor doesn't try to 'compress' things for you by turning strings of
8+ chars into tabs!)...  But, mixing them both like that is generally a bad idea...
But, anyway, yeah it looks a lot saner laid out on that web page... ;-)  I still think you
could make things look a whole lot neater by using "else if" in a few places, though...

Oh, and itoa()??  That's not a standard function...  Just use sprintf("%d")...

Offline

#40 2007-01-05 10:37 PM

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

Re: Chat server wouldn't work

(Missed the added additional details at the end the first time around...)

I notice you're still not adding "-Wall -W" (or even just "-Wall" would be something)
to your compile command-line...  You really should...

Offline

#41 2007-01-06 11:17 AM

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

Re: Chat server wouldn't work

That's True, in stead of a tab, i use always 4 spaces. But when i need 8, my tab is the same.
My dad teach it me like that.
Oh, yeah. I am using VI. How do i change VI settings for the tab?

Hmm, i never heard about sprintf()? How does it works? (looked in man pages already).    Ah, looked again. sprintf() works!

Offline

#42 2007-01-06 05:42 PM

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

Re: Chat server wouldn't work

Offline

#43 2007-01-06 07:28 PM

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

Re: Chat server wouldn't work

I just want to second all of i3839's great advice...  (I was just far too lazy to actually
go through all the code to comment on it... ;-))

Oh, and how to change tabs in vi...  Well, there are a variety of methods...  But,
what I always do is add the following to my "~/.exrc" (and/or "~/.vimrc", if using vim,
which you should if possible, since it's the most kick-ass vi-clone on the planet! ;-)):

set tabstop=4

(I actually have a lot more than that in .vimrc, but that's all that's relevent here...)
There's also wacky stuff like "softtabstop", but if you use that instead, you better
set "expandtab" as well...  (See ":help <optionname>" in vi for more info...)  I much
prefer setting the true "tabstop" to 4; that way, it's storing real tabs for ALL indents,
and the only consequence for people using tabs of 8 instead of 4 is that things will
look indented too much (or look just right, if they normally indent 8 spaces, like the
kernel coders do, for instance)...

Offline

#44 2007-01-06 07:54 PM

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

Re: Chat server wouldn't work

Thanx to all of you guys!
I really Appreciate it!

I come from the Netherlands, thats why it is a mix of english and dutch.
When the program is finished, my dad will look at it with me, and make the sentences correct.

Offline

#45 2007-03-20 12:24 PM

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

Re: Chat server wouldn't work

Okay, I thought it was the best idea to let it 'rest' a while.
And now, I'm back :P

I'm looking for these peace of code for 3 weeks or something, but it does not want to work :( ...

void request_accept(int i) {
    int newfd;
    char buf;
    char tmp[4];
    int as = atoi(u[i].chat);
   
    tmp[0] = '\0';
    newfd = u[i].fd;

    if(u[i].state == STATE_REQUESTCALL) {
        if(recv(newfd,&buf,1,0) == -1) {
            perror("request_call recv() went wrong...");
        } else {
            printf("%c\n",buf);
            if(buf == '\n') {
                if((strcmp(tmp, "y") == 0) || (strcmp(tmp, "yes") == 0)) {
                    u[i].state = STATE_CHAT;
                    //chat function
                } else if((strcmp(tmp, "n") == 0) || (strcmp(tmp, "no") == 0)) {
                    send(u[as].fd,"\n\rThe other person has rejected the call.",41,0);
                    u[as].chat[0] = '\0';
                    u[as].state = STATE_HANDLECONNECTIONS;
                    u[i].chat[0] = '\0';
                    u[i].state = STATE_HANDLECONNECTIONS;
                } else {
                    send(u[i].fd,"\n\rYou have to enter something like yes or no.",45,0);
                }
            } else {
                if(strlen(tmp) < 4) {
                    strncat(buf,&buf,1); 
                    printf("lol");
                }
                printf("%x %x %x %x\n",buf[0], buf[1], buf[2], buf[3]);
            }
        }
    } else {
        perror("Function request_accept() was called without the good state.");
    }
}

THe problem: It always says: 'Enter something like yes or no'! :(

Offline

#46 2007-03-20 12:35 PM

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

Re: Chat server wouldn't work

That code simply shouldn't even compile at all...  At least not without some serious
complaints, anyway...

char buf;
...
                if(strlen(tmp) < 4) {
                    strncat(buf,&buf,1); 
                    printf("lol");
                }
                printf("%x %x %x %x\n",buf[0], buf[1], buf[2], buf[3]);

"buf" is a single char, not an array...  I think the references to "buf* " are meant to
be "tmp* ", and the first "buf" in the strncat() is meant to be "tmp"...

Offline

#47 2007-03-20 12:41 PM

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

Re: Chat server wouldn't work

Also use strncmp() to check only the first few characters, or check against "y\n", "n\n" and so on.

Offline

#48 2007-03-20 12:43 PM

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

Re: Chat server wouldn't work

hmm.
i changed it into this

} else {
                if(strlen(tmp) < 4) {
                    strncat(tmp,&buf,1); 
                }
            }


But the code does the same thing.
I cut out the buf things. They where for test purpose only.
But this isn't working. :(

Offline

#49 2007-03-20 12:52 PM

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

Re: Chat server wouldn't work

....
                if((strncmp(tmp, "y", 1) == 0)) {
                    u[i].state = STATE_CHAT;
                    //chat function
                } else if((strncmp(tmp, "n", 1) == 0)) {
....

Offline

#50 2007-03-20 03:25 PM

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

Re: Chat server wouldn't work

Well, you receive to buf, while you use tmp for that comparison...

Offline

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

Board footer

Powered by FluxBB