multithreading - Simultaneous input and output for network based messaging program -


in python, creating message system client , server can send messages , forth simeltaneously. here code client:

import threading import socket  # global variables host = input("server: ") port = 9000 buff = 1024  # create socket instance s = socket.socket()  # connect server s.connect( (host, port) ) print("connected 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() 

i have thread in background check server messages , looping input send messages server. problem arises when server sends message , user input bounced make room servers message. want input stays pinned bottom of shell window, while output printed 2nd line up, leaving first line alone. have been told can use curses or queues this, not sure 1 best in situation nor how implement these modules project.

any appreciated. thank you.

i want input stays pinned bottom of shell window, while output printed 2nd line up, leaving first line alone. have been told can use curses

here's supplemented version of client code using curses.

import threading import socket  # global variables host = input("server: ") port = 9000 buff = 1024  # create socket instance s = socket.socket()  # connect server s.connect( (host, port) ) print("connected server\n")  import sys write = sys.stdout.buffer.raw.write 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 @ bottom 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 scrolling region leave out screen's bottom line, enters scrolling region temporarily output server messages, , changes @ end.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -