winsock chat

This is a discussion on winsock chat within the Networking/Device Communication forums, part of the General Programming Boards category; hello, i am going to have a go at creating a simple console chat application. now i have been trying ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    102

    winsock chat

    hello,
    i am going to have a go at creating a simple console chat application. now i have been trying in the past few days but, i am stuck. when the data is recived, i want to display it. but if i am typing in a message, i dont get the recevied message displayed till i finish typing?
    how do i stop this problem?
    in vb, its easy because its like... 'on recv_data' blah blah..
    so as soon as the data arrives it displays it..
    hope it makes sense!
    please help.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Well, it depends on how you are sending the data. If you wait till the end of the input to send you will obviously not receive the input on the other end character by character. If you are sending on each character, you should be receiving character by character or close enough. Sending character by character is, of course, very inefficient.

    Maybe you could post some code?

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    ahh, i dont think you understood,
    what i meant is..

    EXAMPLE:

    client sends message..

    server is in the middle of typing a message, the server doesn't display the clients message.. untill he has finished typing the message. the server can only display the clients message when he presses enter, and executes the next bit of code, which would be "cout<<inmessage;"

    im sorry i have no code to post at the moment, i havn't realy started.. this is just a problem i ran into when i was just messing with some code from 'game tutorials'


    i am finding it hard to explain..

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    So, your real problem is that getting input is a blocking operation, which prevents you from processing or displaying an incoming message.

    So you need to either receive messages on another thread or you need a non-blocking way of getting console input.

    Can we assume you are using windows as both methods are non-portable? Note that a GUI program is probably more suitable as it is event driven, like Visual Basic. However, it should be possible in the console.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Two bits of information you will need
    How to read keyboard input without waiting for the user to finish typing FAQ

    How to read a socket without waiting for the other end to send something - manual
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    reading now.
    thank you

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    ok, in the first link, on the second example, is what i need. but i dont understand it...

    Code:
    #include <stdio.h> 
    #include <conio.h> 
    #include <ctype.h> 
    #include <iostream.h>
    
    int main()
    {
      int ch;
      char pword[BUFSIZ];
      int i = 0;
      
      cout<<"Enter your password"<<flush;
      
      while ((ch = getch()) != EOF        
              && ch != '\n'                           
              && ch != '\r' 
              && i < sizeof(pword) - 1)
      {
        if (isalnum(ch))                     //i have never seen 'isalnum' before
        {                                         //does it just check if 'ch' is an int?
          cout<<(char)ch<<flush;    //now what i realy dont understand
          pword[i++] = ch;               //is, this will keep accepting input
        }                                          //untill enter is pressed, why?
      }
    
      pword[i] = '\0';
      
      cout<<"\nYou entered "<<pword;
      
      return 0;
    }
    i have edited it a bit, just so i know whats going on.
    the thing which i dont understand is in the comments in the code..

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    You're supposed to use the keyHit() and getChar() from the third example

    Basically, you loop calling keyHit() and select() to determine if a particular input has anything available to read, then you go read it and deal with it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Here is a basic framework:

    Code:
    bool isReadable(SOCKET s)
    {
    
      /* Use select() to determine if data is waiting to
       * be read on socket. */
    
    }
    
    
    while (TRUE)
    {
    	if (keyHit())
    	{
    		/* A character is available for input */
    
    		char ch = getChar(); /* Get character from input */
    
    		/* Process character. If it is an enter send the message. */
    	}
    	else if (isReadable(my_socket))
    	{
    		/* An incoming message has arrived on the socket
    		 * Read message using recv() and display it on the console */
    	}
    }

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    do i have to use the third example?
    'cause i'm a c++ newbie and i understand none of it.
    and i never put code in my programs that i dont understand..

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    Code:
    #include <stdio.h> 
    #include <conio.h> 
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int ch;
      char text[250];
      int x=0;
    
      cout<<"enter string: "; 
      
    
      while ((ch = getch()) != EOF && ch != '\r' && ch != '\n')
      {
        cout<<(char)ch<<flush;
    	text[x++]=(char)ch;
      }
      text[x]='\0';
      cout<<"\n\n"<<text<<"\n\n";
    
      return 0;
    }
    i think this code would work fine, i would just check for any new data each time a character is inputted?

    ahhh, but i suppose it would still have to wait till a single character is inputted?

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    Quote Originally Posted by Salem
    You're supposed to use the keyHit() and getChar() from the third example

    Basically, you loop calling keyHit() and select() to determine if a particular input has anything available to read, then you go read it and deal with it.

    wouldn't that take up all cpu?
    if it was just in a constant loop? or would you put it on timer to only check for new data every second, or somthing like that..?

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Yes, that would work but you have to make sure you do not enter characters past the end of your buffer!

    As you surmised, if no character is input then a message will not be seen.

    If you like, you can replace the keyHit() and getChar() functions in the code I posted with _kbhit() and getchar() which are available as Visual C++ functions. However, _kbhit() can be somewhat CPU intensive.

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> wouldn't that take up all cpu? <<

    You can alter the keyHit() function so that it waits for a certain timeout for a keystroke to occur. Probably a second or so. This would play nice, using little CPU time.

  15. #15
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    but the problem is, in the third example, i dont understand any of the code, and i want to know whats going on in the program.
    i am realy stuck.. i dont know what to do
    and the problem with my code above, is that, its only possible to display a recieved message when typing..

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 07:32 AM
  2. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 11:11 AM
  3. Requesting Java Chat Client Maintenence
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-02-2003, 12:57 PM
  4. SO close to finishing chat program...!
    By Nakeerb in forum C++ Programming
    Replies: 13
    Last Post: 10-26-2002, 12:24 PM
  5. Rough Portable Chat Design Sketch
    By ggs in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-27-2001, 07:44 AM

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