Reputation: 8706
Question
How can I fix my program to make it so that the socket is always connected? Also, please test your code. I have seen many answers about this, but none have worked.
Thank you so much for the help.
Background
For a while, I have worked on a program to send messages over LAN. I have been trying to work on integrating my knowledge of the GUI and of sockets in this project. But, no matter what I seem to do, the socket has been unable to stay connected.
I have asked many questions about this, but no matter what I have not been able to discover the correct fix. I believe that this is my fault - I am self taught and have perhaps not asked the correct questions. This time, I have chosen a blanket statement; please pardon my lack of finesse, but this seems to be the best way as of now. Here is a link to my code on paste bin. I have included the most likely problematic parts below.
Error Message
('localhost', 1234)
Socket created
Socket bind complete
Socket now listening
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in __call__
return self.func(*args)
File "/Volumes/XXMBABANEXX'S USB/Python Programs/Sockets/IM Project/Server/Functions/Simple Server.py", line 148, in <lambda>
command = lambda: send_msg(s,message))
File "/Volumes/XXMBABANEXX'S USB/Python Programs/Sockets/IM Project/Server/Functions/Simple Server.py", line 82, in send_msg
conn.sendall(my_message)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 57] Socket is not connected
Relevant Code
As the error message says that my send_msg
function is having issues, I have decided to include it below.
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)
Thank you so much for all of your help. This issue has been bothering me for months, and I refuse to let it go.
Upvotes: 0
Views: 1274
Reputation: 624
I would start by creating separate python files for the client and the server. If they are going to share common functions and what not, just write a common.py file and import them in both.
After browsing your paste-bin code the biggest error I can see is that your not calling your SERVER_LOOP function.
on line 90, add Thread(target = SERVER_LOOP, args = (s,)).start() #Start server thread
Secondly, the Thread you create on line 91 is going to exit immediately after doing the client.send(). Python garbage collection is going to tear down the socket, no matter how you create it. put a time.sleep(5) in there and wrap client.send("sup") in a while 1==1: loop
I strongly suggest you find some good example code on creating a python TCP client and server in python. Here is a good start: http://wiki.python.org/moin/TcpCommunication
Upvotes: 2
Reputation: 400682
You're sending the data on the wrong socket—you need to be sending it on the client socket, not the listening socket. The general pattern for a server looks like this:
# Set up the listening socket
listen_socket = socket.socket(...)
listen_socket.bind(...)
listen_socket.listen(...)
# Accept connections and process them
while should_continue_running:
client_socket, client_addr = listen_socket.accept()
# All communication with the new client should now happen on client_socket,
# NOT on listen_socket:
client_socket.recv(...)
client_socket.send(...)
...
client_socket.close()
Upvotes: 0
Reputation: 43527
You are trying to run before you know how to crawl.
Throw away the Tk and threading. Look at the SocketServer examples and run them. Work from that so that you can get the basic socket understanding you appear to lack.
Just so you know, StackOverflow people are quite unlikely to go look at your pastebin code. I did look at it and it is redundant and confusing.
Upvotes: 3