Thread: Help with Console, need execute 3 functions without pause in loop

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Question Help with Console, need execute 3 functions without pause in loop

    Im trying to make a server, built it already, however, there is a problem, since its a game, well, something like that i need to execute a function for wait for user input (gets()), and a function for get new data from server (recv(sock,buffer,64,0)), and the other one will print data from game ( though its OpenGL, however it will loop in same way )
    However, gets(), and recv() pauses the console, and doesnt continues the loop until user writes a command or server sends data

    I need it to check if user wrote command, or if data sent by server, and that will be inside of the while() loop of the OpenGL drawing, but not possible if it pauses

    I cannot use multithreading since hoping that the people play the game with just 1 core... though i already tryied multithreading and didnt work ( in 5 different ways, last was OpenMP )

    Since its an OpenGL window with Console created with AllocConsole, i have to switch stdin and stdout for it print data by using cout ( if possible, tell me how to print without need of doing soo many changes )

    code is like this:
    Code:
    void waitforCommands()
    {
    
    char bufc[256];
    	char command[50];
    
    		freopen("CONIN$","rb",stdin);   // for be able to write
    		
    		gets(command);	// THIS PAUSES
    		strcpy(bufc,"~3");
    		strcat(bufc,command);
    
    		send(sock,bufc,64,0);		//Send Data
    		bufc[0] = '\0';
    		command[0] = '\0';
    }
    
    void waitforMessages()
    {
    	char buf[256];
    
    freopen("CONOUT$","wb",stdout);  // for print at console
    
    		res=recv(sock,buf,64,0);		// receive data, PAUSES TOO
    
    		cout << buf;
    
    
    		buf[0] = '\0';
    		freopen("CONIN$","rb",stdin);   // for write again
    
    }
    Code is executed inside of a while() when draw OpenGL scene

    Probably its simple, so, if possible, can someone tell me how to do for they execute in loop? without pausing and without multithreading ( i dont think people will have about 3 cores for execute that, also, such waste, 1 core for receive, 1 for send and 1 for draw ), else if have to do with multi threading, can post code of how to? since have tryied many different ways and didnt work

    Thanks by attention and if possible, answer

  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
    To read the console without pausing, read through this console tutorial.
    Win32 Console Applications 1

    To read the network without pausing, read this
    recv Function (Windows)
    search for "nonblocking", then set receive socket to be non-blocking.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Hmmmm

    Readed tutorial... used it in my functions, but it didnt let me to write, and tryied using the MultiThreading method that was there and the functions didnt execute xD
    Last edited by inuyasha016; 12-18-2010 at 02:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turning off While loop in realtime
    By baikal_m in forum C Programming
    Replies: 13
    Last Post: 04-22-2010, 01:56 PM
  2. help with do-while loop!!
    By robski in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2010, 01:29 AM
  3. for loop that calls multiple functions
    By elsparko in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2009, 07:10 AM
  4. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM