Add files via upload
This commit is contained in:
80
basic client.py
Normal file
80
basic client.py
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
from tkinter import *
|
||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
from time import sleep
|
||||||
|
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
|
||||||
|
window = Tk()
|
||||||
|
|
||||||
|
|
||||||
|
window.geometry('1200x600')
|
||||||
|
|
||||||
|
window.title('client')
|
||||||
|
|
||||||
|
|
||||||
|
typingtextbox = Text(window, height=1, width=50)
|
||||||
|
typingtextbox.place(x=250, y=550, anchor=CENTER)
|
||||||
|
|
||||||
|
chattextbox = Text(window, height=30, width=60)
|
||||||
|
chattextbox.place(x=150, y=5)
|
||||||
|
|
||||||
|
thecurrentconnectedserver = Label()
|
||||||
|
thecurrentconnectedserver.place(x=200, y=200)
|
||||||
|
|
||||||
|
|
||||||
|
#ip = input('ip: ')
|
||||||
|
ip = '192.168.1.6'
|
||||||
|
#ip='www.ggsya.ga'
|
||||||
|
#ip = '41.40.24.151'
|
||||||
|
localhost = socket.gethostname()
|
||||||
|
#ip = socket.getaddrinfo('ggsya.ga',9000)[0][-1]
|
||||||
|
print(localhost)
|
||||||
|
print('trying to connect')
|
||||||
|
clientsocket.connect((ip,9000))
|
||||||
|
print('connected')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def recievemessage():
|
||||||
|
print('waiting')
|
||||||
|
clientsocket.settimeout(0.1)
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
incoming_message =clientsocket.recv(1024) # try to receive 1024 bytes
|
||||||
|
except socket.timeout:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
incoming_message = incoming_message.decode()+'\n'
|
||||||
|
chattextbox.insert(INSERT, incoming_message)
|
||||||
|
print(incoming_message)
|
||||||
|
print('waiting')
|
||||||
|
|
||||||
|
|
||||||
|
def sendmessage(event):
|
||||||
|
message = + typingtextbox.get('1.0', 'end-1c')
|
||||||
|
typingtextbox.delete('1.0', END)
|
||||||
|
clientsocket.send(message.encode())
|
||||||
|
|
||||||
|
def sendmessagebtn():
|
||||||
|
message = typingtextbox.get('1.0', 'end-1c')
|
||||||
|
typingtextbox.delete('1.0', END)
|
||||||
|
clientsocket.send(message.encode())
|
||||||
|
|
||||||
|
sendbutton = Button(window, height=1, width=5, fg='red', text='send', command=sendmessagebtn)
|
||||||
|
sendbutton.place(x=260, y=537)
|
||||||
|
|
||||||
|
clientsocket.settimeout(0.1)
|
||||||
|
|
||||||
|
thread = threading.Thread(target=recievemessage)
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
error_label = Label(window, height=1, width=5,text='error here',fg='red')
|
||||||
|
#error_label.place(x=1,y=1)
|
||||||
|
window.bind('<Return>',sendmessage)
|
||||||
|
window.mainloop()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
72
basic server.py
Normal file
72
basic server.py
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
from tkinter import *
|
||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
window = Tk()
|
||||||
|
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
|
||||||
|
|
||||||
|
# def allconn():
|
||||||
|
# global all_conn
|
||||||
|
# all_conn = []
|
||||||
|
#
|
||||||
|
# allconn()
|
||||||
|
|
||||||
|
all_conn = []
|
||||||
|
|
||||||
|
window.geometry('1200x600')
|
||||||
|
host = socket.gethostname()
|
||||||
|
|
||||||
|
ip = '192.168.1.6'
|
||||||
|
s.bind((ip,9000))
|
||||||
|
|
||||||
|
chattext = Text(width = 50)
|
||||||
|
|
||||||
|
chattext.place(x=200, y=200)
|
||||||
|
|
||||||
|
connecteduserslb = Label(text = 'connected users: ')
|
||||||
|
connecteduserslb.place(x=200, y=50)
|
||||||
|
|
||||||
|
names_dic = {}
|
||||||
|
|
||||||
|
def recieveandsend():
|
||||||
|
while 1:
|
||||||
|
all_conn = list(names_dic.keys())
|
||||||
|
for client in all_conn:
|
||||||
|
client.settimeout(0.1)
|
||||||
|
try:
|
||||||
|
msg = f'{names_dic[client]}: {client.recv(1024).decode()}'
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
for client_ in all_conn:
|
||||||
|
try:
|
||||||
|
client_.send(msg.encode())
|
||||||
|
chattext.insert(INSERT, msg)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def accept():
|
||||||
|
global names_dic
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
s.listen(5)
|
||||||
|
connection, address = s.accept()
|
||||||
|
print('listened')
|
||||||
|
except socket.timeout:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
names_dic[connection] = connection.recv(1024).decode()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
accept_thread = threading.Thread(target=accept)
|
||||||
|
accept_thread.start()
|
||||||
|
|
||||||
|
recieveandsend_thread = threading.Thread(target=recieveandsend)
|
||||||
|
recieveandsend_thread.start()
|
||||||
|
|
||||||
|
|
||||||
|
window.mainloop()
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user