H συναρτήσεις Waiting for client και send file είναι κυρίως αυτές που έχουν το πρόβλημα.
Το λέω για να μην χρειαστεί να διαβάσεις όλο των κώδικα.
#-*- coding: utf-8 -*-
import socket, shutil, sys, os
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from threading import Thread
#_____Global Variables_____#
#Booleans
running = True
server_created = False
join_field_show = False
waiting_for_client = False
#Objects
Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Client = None
#_____Global Variables_____#
#===============================================Waiting_For_Client===============================================#
def Waiting_For_Client():
global Client, waiting_for_client
message = Label(text = "Waiting For Client", fg = "green") #Showing waiting message.
message.place(x = 45, y = 90)
waiting_for_client = True
screen.geometry("190x110")
Client, addr = Socket.accept() #Waiting for a client to join.
messagebox.showinfo(title = "Client Joined!", message = "A client has join the server.") #Showing joined message.
message.destroy()
screen.geometry("190x80")
waiting_for_client = False
file_bytes = Client.recv(2898)
save_path = filedialog.askdirectory()
file = open(save_path+"\\name.gp5", "wb")
file.write(file_bytes)
file.close()
messagebox.showinfo(title = "Received Successfully!", message = "You reiceved the file successfully.")
Socket.close()
return
#===============================================Waiting_For_Client===============================================#
#=================================================Create_Server==================================================#
def Create_Server():
global Client, server_created, Socket
#You cant create a server.
if (join_field_show):
messagebox.showerror(title = "Can't Start!", message = "You can't create a server while trying to join one.")
pass
#Create a server if it's not running.
elif (not server_created):
Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Socket.bind(("", 5555)) #Binding the server to localhost.
Socket.listen(1) #Setting the listening to 1.
#Showing a message to the user.
messagebox.showinfo(title = "Server Started!", message = "Server Created Successfully! \nPlease wait for a client to join.")
wait_client = Thread(target = Waiting_For_Client, name = "Waiting_For_Client") #Creating client wait thread.
wait_client.start() #Starting this thread.
server_created = True
pass
#Server Already Running.
else:
messagebox.showerror(title = "Can't Start!", message = "Server is already created.")
pass
return
#=================================================Create_Server==================================================#
#==================================================Join_Server===================================================#
def Join_Server():
global join_field_show, Socket
Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#You cant join a server while creating one.
if (waiting_for_client):
messagebox.showerror(title = "Can't Join!", message = "You can't join a server while you creating one.")
return
#---------------------------------------------Back Button---------------------------------------------#
def Back():
global join_field_show
destroy_widgets(widgets = [ip_label, ip_entry, port_label, port_entry, join_button, back_button])
screen.geometry("190x80") #Resizing the screen.
join_field_show = False
return
#---------------------------------------------Back Button---------------------------------------------#
#---------------------------------------------Join Button---------------------------------------------#
def Join():
global Socket
#Try to connect to a server.
try:
Socket.connect((ip.get(), int(port.get()) ))
messagebox.showinfo(title = "Connected!", message = "You connected successfully at "+ip.get())
Back()
pass
#Failed to connect.
except:
messagebox.showerror(title = "Failed!", message = "There was a problem connecting at "+ip.get())
Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
return
Send_File()
return
#---------------------------------------------Join Button---------------------------------------------#
screen.geometry("190x160") #Resizing the screen.
join_field_show = True
#-------------Join Server Widgets-------------#
#Ip Lable.
ip_label = Label(text = "IP:", fg = "green")
ip_label.place(x = 30, y = 90)
#Ip Entry.
ip_entry = Entry(textvariable = ip, width = 15)
ip_entry.place(x = 50, y = 90)
#Port Label.
port_label = Label(text = "Port:", fg = "green")
port_label.place(x = 20, y = 110)
#Port Entry.
port_entry = Entry(textvariable = port, width = 15)
port_entry.place(x = 50, y = 110)
join_button = Button(text = "Join", fg = "blue", width = 8, command = Join)
join_button.place(x = 20, y = 135)
back_button = Button(text = "Back", fg = "blue", width = 8, command = Back)
back_button.place(x = 100, y = 135)
#-------------Join Server Widgets-------------#
#==================================================Join_Server===================================================#
#================================================destroy_widgets=================================================#
def destroy_widgets(widgets = []):
for widget in widgets:
widget.destroy()
continue
return
#================================================destroy_widgets=================================================#
#=================================================Exit Program===================================================#
def Exit():
Socket.close()
sys.exit(0)
quit()
return
#=================================================Exit Program===================================================#
def Send_File():
#----------Taking the file----------#
while (running):
path = filedialog.askopenfilename()
#Asking if this is the file he wants to send.
if (messagebox.askyesno(title = "Are You Sure", message = "Are you sure that this is the file you want to send?\n"+path)):
break
#Else take the file again.
else:
continue
pass
#----------Taking the file----------#
#Taking the bytes of the file.
file = open(path, "rb")
file_bytes = file.read()
file.close()
Socket.sendall(file_bytes)
messagebox.showinfo(title = 'Sended!', message = "The file has been send successfuly.")
Socket.close()
return
#=================================================Main Program==================================================#
#---Building The Screen---#
screen = Tk()
screen.geometry("190x80")
screen.title("File Sender")
#---Building The Screen---#
ip = StringVar()
ip.set("25.101.123.202")
port = IntVar()
port.set(5555)
#--------------------------------------------------GUI WIDGETS--------------------------------------------------#
#Creating the widgets.
create_server_button = Button(text = "Create Server", command = Create_Server, width = 20, fg = "red")
join_server_button = Button(text = "Join Server", command = Join_Server, width = 20, fg = "red")
exit_button = Button(text = "Exit", command = Exit, width = 20, fg = "red")
#Packing the widgets.
create_server_button.pack()
join_server_button.pack()
exit_button.pack()
#--------------------------------------------------GUI WIDGETS--------------------------------------------------#
screen.mainloop()
Socket.close()
sys.exit(0)
quit()
#=================================================Main Program==================================================#