aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorT <T@192.168.0.100>2017-03-29 22:30:07 +0300
committerT <T@192.168.0.100>2017-03-29 22:30:07 +0300
commit5b08d3894c743791bfc0707cd6556ccb42fb1ffc (patch)
treec40d50def8232ab16bb18a7bf2a03fd263e2121c
parent59513d6c833e5697e1c7a3beb3816c49799461be (diff)
clienttia
-rw-r--r--client.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/client.py b/client.py
new file mode 100644
index 0000000..8a8f2c1
--- /dev/null
+++ b/client.py
@@ -0,0 +1,78 @@
+'''
+Created on 29.3.2017
+
+@author: T
+'''
+
+import socket
+import time
+import threading
+from datetime import datetime
+import sys
+
+HOST = "127.0.0.1"
+BUFFER_SIZE = 1024
+DEBUG = True
+
+def debug(string):
+ if (DEBUG):
+ print("{} DEBUG: {}".format(datetime.now(), string))
+
+def connect(host, nick):
+ msg = "CONNECT\n" + nick + "\n\0"
+ s.connect((HOST, 2000))
+ debug("connected")
+ s.send(msg.encode(encoding='utf_8'))
+
+def disconnect():
+ msg = "DISCONNECT\n" + nick + "\n\0"
+ s.send(msg.encode(encoding='utf_8'))
+
+def send_message(viesti):
+ msg = "MESSAGE\n" + nick + "\n" + viesti + "\0"
+ s.send(msg.encode(encoding='utf_8'))
+
+def keep_alive():
+ debug("laskuri started")
+ while 1:
+ time.sleep(10)
+ msg = "KEEPALIVE\n" + nick + "\n\0"
+ s.send(msg.encode(encoding='utf_8'))
+ debug("10 sekuntia meni -> KEEPALIVE")
+
+def listen_messages():
+ debug("kuuntelija started")
+ while 1:
+ rec = s.recv(BUFFER_SIZE).decode()
+ osat = rec.split("\n")
+ debug(osat)
+ debug(rec)
+ if osat[0] == "BROADCAST":
+ print(osat[1] + ": " + osat[2])
+
+
+host = input("Syota ip")
+nick = input("Syota nick")
+s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+connect(host, nick)
+
+#luodaan laskuri
+laskuri = threading.Thread(target=keep_alive, args=())
+laskuri.start()
+#luodaan viestien kuuntelija
+kuuntelija = threading.Thread(target=listen_messages, args=())
+kuuntelija.start()
+
+#odotetaan kayttajan kirjoittavan viestin tai lopettavan ohjelman -quit-komennolla
+while 1:
+ syote = input()
+ if syote == "-quit":
+ disconnect()
+ break
+ else:
+ send_message(syote)
+ #Yrityksia poistaa kayttajan syote koska tulee muuten kahteen kertaan
+ #print("", end="\r")
+ #sys.stdout.write("\r")
+
+debug("Stopped") \ No newline at end of file