简体   繁体   English

如何使用AC程序在多个终端窗口中输出

[英]How to output in multiple terminal windows with a c program

I am writing a simple chat-room application in c using posix sockets. 我正在使用posix套接字在C中编写一个简单的聊天室应用程序。 However I'll have this issue in sending messages. 但是,在发送消息时会遇到这个问题。

(client1)say something: Hello folks! what are 

client2 said: xyzabc

client3 said: dsgh

Here above is the client1's terminal window where he was trying to send "Hello folks! what are you doing?" 上面是client1的终端窗口,他试图在该窗口中发送“ Hello们!您在做什么?” but before he could write his messsage and press enter , client2 and client3 sent something.(separate thread for receiving messages) 但是在他写出信息并按回车键之前,client2和client3发送了一些消息。(用于接收消息的单独线程)

I'm trying to tackle this issue by using 2 different terminal windows for each client, one for writing the message and another for displaying the chat messages. 我试图通过为每个客户端使用2个不同的终端窗口来解决此问题,一个用于编写消息,另一个用于显示聊天消息。

To begin with I've opened a gnome-terminal window by writing 首先,我通过编写打开gnome-terminal窗口

system("gnome-terminal");

but now, 但现在,

I want to perform some read write operations on the terminal window I've opened and the existing window. 我想在打开的终端窗口和现有窗口上执行一些读写操作。

printf("This is existing window"); //want to print this on existing terminal

printf("this is new terminal window"); //want to print this on new terminal

scanf("%d",&a); //take input from existing window

scanf("%d",&b); //take input from new window

I've read here that I can do it by reading/writing from proper /dev/pts/<n> file. 我在这里阅读可以通过从正确的/dev/pts/<n>文件中进行读取/写入来实现的目的。 But how do I find the n in the /dev/pts/<n> for the current terminal and the new terminal window I just opened? 但是,如何在/dev/pts/<n>找到当前终端和刚打开的新终端窗口中的/dev/pts/<n> Is there any better way of solving the issue? 有解决这个问题的更好方法吗?

One known good way is using the alternate interface to GNU Readline: 一种已知的好方法是使用GNU Readline的备用接口

Some applications need to interleave keyboard I/O with file, device, or window system I/O, typically by using a main loop to select() on various file descriptors. 某些应用程序通常需要通过使用主循环在各种文件描述符上使用select()来使键盘I / O与文件,设备或窗口系统I / O交织。 To accomodate this need, readline can also be invoked as a `callback' function from an event loop. 为了适应这种需求,还可以从事件循环中将readline作为“回调”函数来调用。

The pseudocode skeleton is as follows: 伪代码框架如下:

  rl_callback_handler_install (prompt, my_handler)

  while true
     wait on stdin and socket (select or poll)
     if stdin is ready            
        rl_callback_read_char();
     if socket is ready
        read message from socket
        save readline state
        print message
        restore readline state

   void my_handler(char* line)            
        send line to socket

Saving and restoring readline state is 保存和还原readline状态为

  saved_line = rl_copy_text(0, rl_end);
  rl_save_prompt();
  rl_replace_line("", 0);
  rl_redisplay();

  rl_restore_prompt();
  rl_replace_line(saved_line, 0);
  rl_point = saved_point;
  rl_redisplay();
  free(saved_line);

A complete, if rudimentary, chat program that utilises this method can bee found here . 可以在此处找到使用此方法的完整的,基本的聊天程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM