Sockets

This is a discussion on Sockets within the C Programming forums, part of the General Programming Boards category; Hi i wrote a server for a simple chat program and im having a little trouble with my buffer that ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    69

    Sockets

    Hi i wrote a server for a simple chat program and im having a little trouble with my buffer that im sending back and forth. My buffer takes in what the person says and writes it to the other person. But im having trouble with over lapping when i say something shorter then the last statment to the other person. I have tried searching for a null character at the end of each buffer but for some reason thats not working here is my code that is having the problem. Each time i write it needs to be the exact size of the character array that is being sent and strlen() isn't working for some reason. if you need any more information just ask me.

    Code:
    {
    
                    char buf[] = "Start chatting now\n";
                    write( io_sd, buf, strlen( buf ) );
                    write( io_sd2, buf, strlen( buf ) );
                    do
                    {
                            read( io_sd, buf_io, cap);
                            for(i=0; i<cap; i++)
                            {
                                    tmp[i] = buf_io[i];
                                    count++;
                                    if(tmp[i] == '\0')
                                    {
                                            break;
                                    }
                            }
                            printf("%d", count);
                            write( io_sd2, buf_io, count );
                            read( io_sd2, buf_io, cap );
                            for(i=0; i<cap; i++)
                            {
                                    tmp[i] = buf_io[i];
                                    count++;
                                    if(tmp[i] == '\0')
                                    {
                                            break;
                                    }
                            }
                            printf("%d", count);
    
                            write( io_sd, buf_io, count );
    
                    }while(1);
    
            }
    here are some test runs

    Code:
    Person #1
    
    [ ~]$ telnet localhost 15003
    Trying 127.0.0.1...
    Connected to localhost.localdomain (127.0.0.1).
    Escape character is '^]'.
    Start chatting now
    test
    this is a longer test
    shorter test
    
    Person #2
    [ ~]$ telnet localhost 15003
    Trying 127.0.0.1...
    Connected to localhost.localdomain (127.0.0.1).
    Escape character is '^]'.
    Start chatting now
    test
    this is a longer test
    shorter test
    er test
    Mab8v b8Ab8C

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you're reading from the keyboard, shouldn't you be looking for \n rather than \0? More importantly, read will tell you how many characters were read; why not use that information?

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    write( io_sd, buf, strlen( buf ) );
    you do not send nul-char, why do you expect to receive it?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    OK well i got my problem fixed, and the last thing i need to do is get this to quit out when someone types quit. i tried if(strcmp(buf_io, "quit") == 0) then break; but this wont work for some odd reason.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I like vart's answer better. Also: if you're using count as a parameter that gets added to, shouldn't it get reset to 0 every now and again?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Dr Saucie View Post
    OK well i got my problem fixed, and the last thing i need to do is get this to quit out when someone types quit. i tried if(strcmp(buf_io, "quit") == 0) then break; but this wont work for some odd reason.
    I'd bet buf_io has an \n at the end of it.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    tried the \n nothing happened still

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Dr Saucie View Post
    tried the \n nothing happened still
    Then debug to see what value is in your variable. See what value check has (if you managed to get that reset each time).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 02:43 PM
  2. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  3. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 05:52 AM
  4. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM
  5. Sockets, questions.
    By Vber in forum C Programming
    Replies: 2
    Last Post: 12-26-2002, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21