0

On most servers running in console mode, the admin is allowed to type in commands while the server is also outputting text like player deaths, etc.

I was just wondering how to replicate this effect. I have searched all over the place and don't even have the slightest idea on how it's done.

Ben
  • 51,770
  • 36
  • 127
  • 149
ILikePizza555
  • 73
  • 1
  • 10
  • @SisMS In most server applications I've used, the input is at the bottom and the output are all the lines above the input line. – ILikePizza555 Sep 30 '12 at 02:40
  • 1
    Which part of this are you not able to achieve? The processing of the input & output (as the answers address currently) or how to print output and receive input separately in the terminal? (f.x the bottom line is for input and the lines above are for output) In the latter case, you can check the [ncurses](http://en.wikipedia.org/wiki/Ncurses) library. – enobayram Sep 30 '12 at 11:16
  • @enobayram The latter, I will definitely try the ncurses library – ILikePizza555 Sep 30 '12 at 16:59

2 Answers2

0

You need to learn about threads.

All programs have at least a thread, but most console applications only have one. This way, a program can only do one thing at a time.

Imagine a program with two threads as two programs that work together towards a goal.

You can create one thread that does the computing, one that prints all the output and one that keeps reading data from the keyboard. This will create a program that can write and read at the same time.

Also, keep in mind that you need to use a multi-processor machine in order for threads to offer true parallelism to your program.

Here is a ruby example that can read and write data from/to the console at the same time:

a = Thread.new do
  while true do
    puts "Hi There!"
    sleep(1)
  end
end

b = Thread.new do
  while true do
    puts gets.strip+"!"
  end
end
a.join
b.join
Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
  • I know about threads, and I figured they are used somehow but I fail to see exactly what each thread does. – ILikePizza555 Sep 30 '12 at 02:39
  • I've added an example that illustrated how to read and write data at the same time. First thread (a) keeps printing "Hi There!" every 1 second, while the second thread (b) keeps reading a string and echos it back to the console. – Ionut Hulub Sep 30 '12 at 02:46
  • The perl script seems to clear some things up for me. Thanks. – ILikePizza555 Sep 30 '12 at 16:59
0

That is accomplished using Threads.

What they do is basically this; the programmer create two threads, and assigns each one a task. One will be for output and the other for input. Since they run cocurrently, they both do their stuff at the same time. The threads report to the same backend processing, where the input is used, and the output is sent to the output thread. Most likely in a loop. This creates the simultaneous effect you want.

You can read more about implemention here, here and here.

Community
  • 1
  • 1
Rivasa
  • 6,510
  • 3
  • 35
  • 64