Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #////////////////////////////////////////////////
- """
- Here I imported lots of different modules.
- """
- #/////////////////////////////////////////////////
- from Tkinter import * #Used to create the GUI
- from ScrolledText import ScrolledText #Used to create the Scrolled Text
- import socket #Used to create the server
- import sys #Used for exiting in case of issue
- from threading import * #Creates threading capabilities.
- #////////////////////////////////////////////////
- """
- This section of the code is used to define the socket
- server and its respective functions. These functions are
- used by the GUI part to send messages to clients
- """
- #/////////////////////////////////////////////////
- BUFFER = 1024 #Buffer for recv of msg
- import socket #Needed for connectivity
- import sys #needed for exit
- HOST = 'localhost' # Loopback
- PORT = 1234 # Random port that works
- addr = (HOST, PORT) #I create the address based on the HOST and PORT variables
- print addr #so that the user can see the addr
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#creates a socket object 's'
- print 'Socket created' #prints msg
- try: #tries to bind to port
- s.bind((HOST, PORT))
- except socket.error , msg: #if fail, do this
- print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
- sys.exit()
- print 'Socket bind complete' #prints msg
- s.listen(10) #listen for connections
- print 'Socket now listening'
- def SERVER_LOOP(s):
- """Function for accepting connections to server"""
- global conn
- conn, addr = s.accept()
- print "Connected with", addr
- def RECEIVE(conn, BUFFER):
- """Receiving function"""
- while True:
- try:
- #Sending message to connected client
- #This only takes strings (words
- conn.recv(BUFFER)
- except Exception:
- import traceback
- print traceback.format_exc()
- def send_msg(conn, message):
- """Send Messages"""
- #Send some data to the remote server
- my_message = message.get("0.0", END)
- #set the whole string
- conn.sendall(my_message)
- def START_SERVER(addr):
- """Rudimentary client to start server. Helps for testing"""
- client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- client.connect(("localhost", 1234))
- client.send("sup")
- Thread(target = START_SERVER, args = (addr,)).start() #Start client thread
- #////////////////////////////////////////////////
- """
- This code is used to hold the GUI stuff.
- It uses Tkinter and multiple frames to accomplish this task.
- It calls on commands from the Socket Server
- """
- #/////////////////////////////////////////////////
- #Setup Stuff
- root = Tk()
- root.title("HoweIM Server GUI")
- #make my screen dimensions work
- w = 500
- h = 1000
- root.geometry("500x1000")
- #Create the Main-Frame
- Hold_Frame = Frame(root)
- Hold_Frame.grid()
- #Past Frame
- pastFrame = Frame(Hold_Frame, width = 100, height = 300)
- pastFrame.grid()
- pastText = ScrolledText(pastFrame, height = 30,
- width = 50,
- #This line (state = DISABLED) means that the user cannot edit this textbox
- state = DISABLED,
- #This line (relief = GROOVE, bd = 5) tells Tkinter to make a groove aroudn the box, and the width of the border
- #Is now 5
- relief = GROOVE, bd = 5)
- pastText.grid(row = 1)
- #Message Frame
- messageFrame = Frame(Hold_Frame, width = 100, height = 100)
- messageFrame.grid()
- space = Label(messageFrame)
- space.grid()
- message = Text(messageFrame, height = 10, width = 50,
- relief = GROOVE, borderwidth = 5,)
- message.grid()
- send = Button (messageFrame, text = "Send",
- command = lambda: send_msg(s,message))
- send.grid(row = 1, column = 1, sticky = S)
- #Side Frame
- sideFrame = Frame(Hold_Frame)
- root.mainloop() #start gui mainloop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement