0

I have a client and a server in python, my server is consistently sending the time to my client. My client then prints the time received, the length of the message, and that the full message was received. I know how to incorporate a basic GUI, but when I do the flow of data stops exactly where the GUI was placed. I need the GUI to display the time as it receives it, constantly updating. This is my first python project so I'm sure I'm missing something.

I'm honestly unsure of how to go about this. Any ideas are helpful.

This Is my server

import socket
import time


HEADERSIZE = 10

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1243))
s.listen(5)

while True:
    # now our endpoint knows about the OTHER endpoint.
    clientsocket, address = s.accept()
    print(f"Connection from {address} has been established.")

    msg = "Welcome to the server!"
    msg = f"{len(msg):<{HEADERSIZE}}"+msg

    clientsocket.send(bytes(msg,"utf-8"))

while True:
    time.sleep(3)
    msg = f"The time is {time.time()}"
    msg = f"{len(msg):<{HEADERSIZE}}"+msg

    print(msg)

    clientsocket.send(bytes(msg,"utf-8"))

This is my client

import socket

HEADERSIZE = 10

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 1243))

while True:
    full_msg = ''
    new_msg = True
    while True:
    msg = s.recv(60)
    if new_msg:
        print("new msg len:",msg[:HEADERSIZE])
        msglen = int(msg[:HEADERSIZE])
        new_msg = False

    print(f"full message length: {msglen}")

    full_msg += msg.decode("utf-8")

    print(len(full_msg))


    if len(full_msg)-HEADERSIZE == msglen:
        print("full msg recvd")
        print(full_msg[HEADERSIZE:])
        new_msg = True
        full_msg = ''

This is what the server results:

Connection from ('169.254.91.3', 64486) has been established.

29 The time is 1563997982.918763

30 The time is 1563997985.9197025

0 The time is 1563997988.9197063

This is what the client results:

new msg len: b'22 ' full message length: 22

32

full msg recvd

Welcome to the server!

new msg len: b'29 ' full message length: 29

39

full msg recvd

The time is 1563997982.918763

new msg len: b'30 ' full message length: 30

40

full msg recvd

The time is 1563997985.9197025

new msg len: b'30 ' full message length: 30

40

full msg recvd

The time is 1563997988.9197063

Angie
  • 21
  • 2

1 Answers1

1

You can create a Text box to capture the messages received. In order to not blocking the GUI mainloop, you need to use thread to handle server connection. Below is an example:

import socket
import threading
from tkinter import *

HEADERSIZE = 10

# function to handle server connection
def connect_server(logbox):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((socket.gethostname(), 1243))

    while True:
        msg = s.recv(60).decode()
        print(msg)
        msglen = int(msg[:HEADERSIZE])
        if len(msg) == msglen+HEADERSIZE:
            logbox.insert(END, f'{msg[HEADERSIZE:]}\n')
        else:
            logbox.insert(END, 'Received invalid message\n')
        logbox.see(END)

# create the GUI
root = Tk()
logbox = Text(root, width=60, height=10)
logbox.pack(fill=BOTH, expand=1)
# start the server handling thread
threading.Thread(target=lambda:connect_server(logbox), daemon=True).start()
# start the GUI mainloop
root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • Thank You, I know I didn't give you much to go off of but this works and helps me understand a lot. – Angie Jul 25 '19 at 14:43