同时输入和输出基于网络的消息传递程序 [英] Simultaneous input and output for network based messaging program

查看:77
本文介绍了同时输入和输出基于网络的消息传递程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,我正在创建一个消息系统,客户端和服务器可以同时来回发送消息.这是我给客户的代码:

In python, I am creating a message system where a client and server can send messages back and forth simeltaneously. Here is my code for the client:

import threading
import socket

# Global variables
host = input("Server: ")
port = 9000
buff = 1024

# Create socket instance
s = socket.socket()

# Connect to server
s.connect( (host, port) )
print("Connected to server\n")


class Recieve(threading.Thread):
    def run(self):
        while True: # Recieve loop
            r_msg = s.recv(buff).decode()
            print("\nServer: " + r_msg)

recieve_thread = Recieve()
recieve_thread.start()

while True: # Send loop
    s_msg = input("Send message: ")

    if s_msg.lower() == 'q': # Quit option
        break

    s.send( s_msg.encode() )

s.close()

我在后台有一个线程来检查服务器消息,并有一个循环输入以将消息发送到服务器.当服务器发送消息并且用户输入立即反弹以为服务器消息腾出空间时,就会出现问题.我希望它使输入保持固定在shell窗口的底部,而输出是从第二行向上打印的,而不会留下第一行.有人告诉我可以使用curses或Queues来做到这一点,但我不确定哪种情况最适合我,也不知道如何在我的项目中实现这些模块.

I have a thread in the background to check for server messages and a looping input to send messages to the server. The problem arises when the server sends a message and the user input is immediately bounced up to make room for the servers message. I want it so that the input stays pinned to the bottom of the shell window, while the output is printed from the 2nd line up, leaving the first line alone. I have been told that you can use curses or Queues to do this, but I am not sure which one would be best in my situation nor how to implement these modules into my project.

任何帮助将不胜感激.谢谢.

Any help would be appreciated. Thank you.

推荐答案

我想要它,以便输入保持固定在外壳的底部 窗口,同时从第二行开始打印输出,保留 一线独行.有人告诉我可以使用诅咒

I want it so that the input stays pinned to the bottom of the shell window, while the output is printed from the 2nd line up, leaving the first line alone. I have been told that you can use curses

这是使用诅咒的客户端代码的补充版本.

Here's a supplemented version of your client code using curses.

import threading
import socket

# Global variables
host = input("Server: ")
port = 9000
buff = 1024

# Create socket instance
s = socket.socket()

# Connect to server
s.connect( (host, port) )
print("Connected to server\n")

import sys
write = sys.stdout.buffer.raw.write
from curses import *
setupterm()
lines = tigetnum('lines')
change_scroll_region = tigetstr('csr')
cursor_up            = tigetstr('cuu1')
restore_cursor       = tigetstr('rc')
save_cursor          = tigetstr('sc')

def pin(input_lines):   # protect input_lines at the bottom from scrolling
        write(save_cursor + \
              tparm(change_scroll_region, 0, lines-1-input_lines) + \
              restore_cursor)

pin(1)

class Recieve(threading.Thread):
    def run(self):
        while True: # Recieve loop
            r_msg = s.recv(buff).decode()
            write(save_cursor+cursor_up)
            print("\nServer: " + r_msg)
            write(restore_cursor)

recieve_thread = Recieve()
recieve_thread.daemon = True
recieve_thread.start()

while True: # Send loop
    s_msg = input("Send message: ")

    if s_msg.lower() == 'q': # Quit option
        break

    s.send( s_msg.encode() )

pin(0)
s.close()

它将更改滚动区域以保留屏幕的底行,暂时进入滚动区域以输出服务器消息,然后最后将其更改回.

It changes the scrolling region to leave out the screen's bottom line, enters the scrolling region temporarily to output the server messages, and changes it back at the end.

这篇关于同时输入和输出基于网络的消息传递程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆