Thread: accept and work at same time

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25

    accept and work at same time

    I’m trying to create a small server/client tier architecture. You might think this should be posted under socket programming. But my question is of another kind.
    I have seen programs that are able to accept keyboard input – yet are able to work and throw output on the screen. My goal is to create a small dos based chat relay server as a start.
    How would I accomplish this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So do you really mean DOS, or do you mean that black rectangle in the middle of your screen which has a "C:\>" prompt in it?

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    haha, that little black box with the c:\> is dos. Well an altered version of course from Microsoft called the command prompt. If it helps im using windows XP.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by multielements
    haha, that little black box with the c:\> is dos. Well an altered version of course from Microsoft called the command prompt. If it helps im using windows XP.

    ha ha... you're wrong...
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    My first thought would be multithreading but I dont know anything about that or networking, but read up on it to see if that is what you want.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You are wanting to use asynchronous socket operations. What I think he/she is saying is that accept() is causing a block which is causing him/her to have a delay between when he/she can type and when he/she is ready to type. So off to google with ya, I'm too lazy to copy/paste from msdn.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >My first thought would be multithreading but I dont know anything about that or networking, but read up on it to see if that is what you want.

    Well this will strictly depend on what you are planning on doing. A lot of programs make new threads for each individual client. Its always an option you can spend time thinking about.

  8. #8
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You can use a thread running as your receiver and have it check every interval if the socket is readible. If it is, you can receive data and process it accordingly. All of your sending can be done in a blocking manner. To illustrate:
    Code:
    void ReceiveThread(void *pSomething) {
      while (socket is good) {
        if (socket is readible) {
          receive(some data);
          if (it is a chat message)
            print(the chat message on the screen);
        }
      }
    }
    
    int main() {
      connect(to a server);
      start(your ReceiveThread);
      read (some text from keyboard);
      send(some text to server);
      return 0;
    }
    Something along those lines. Keep in mind, of course, that this is a very simplistic pseudocode view from the top of a really tall ladder and there are many other details that must be taken into consideration. But this is a general structure that should do the job for you.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > haha, that little black box with the c:\> is dos.
    Wrong - it's called a console.

    Console programs are 32-bit (DOS was 16), have access to far more memory and a much wider set API calls.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Poor old DOS. She's just an old lady sitting in her rocking chair waiting to die.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    multielements:
    You can look into the function select(), that will tell you whether there's any clients waiting to connect, and if there are THEN you call accept() and it won't block. Otherwise, you could switch to asynchronous winsock of sorts. The same goes for recv().

    Also look into _kbhit(), it's a nonstandard function that lets you know if a key has been pressed, that way you don't have to wait while get() or getline() etc. blocks either.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i cant get my code to work! (hawknl)
    By Guido in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-23-2004, 01:20 AM
  2. help allocating 2d array w/ new
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2004, 08:10 AM
  3. CEdit::OnChar() won't work for derived class!
    By LuckY in forum Windows Programming
    Replies: 0
    Last Post: 02-28-2003, 01:01 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM