Reputation: 13
I've trying to fix this error since the past 3 days and Im slowly getting tired of it tbh, could someone point me into the right direction please?
So, I've written 2 scripts, one is a server and the other one is the client
#!/usr/python
import socket
import os
port = 3000
def clear():
os.system('clear')
print("-:-:-:-:-:Server:-:-:-:-:-")
#Starting Server
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = socket.gethostname()
serversocket.bind((host, port))
serversocket.listen(1)
clear()
clientsocket, addr = serversocket.accept()
print("Connection from: " + str(addr))
while True:
msg = raw_input(" ->")
if msg == "help":
clear()
print("+-----------------------------------+")
print("| Help Menu |")
print("+-----------------------------------+")
print("| help - shows help menu |")
print("+-----------------------------------+")
print("| clear - clears |")
print("+-----------------------------------+")
raw_input("Press ANY key to return")
clear()
elif msg == "clear":
clear()
else:
msg = str(msg).encode("UTF-8")
clientsocket.send(msg)
msg = clientsocket.recv(4096)
print(msg.decode("UTF-8"))
and
#!/usr/python
import socket
import time
def send(msg):
try:
s.send(msg.encode("UTF-8"))
except:
Connect()
def getInstructions():
try:
while True:
msg = s.recv(4096)
inst = msg.decode("UTF-8")
time.sleep(5)
send("!")
except:
Connect()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
def Connect():
try:
print("Connect() connecting to: "+ str(host) + str(3000))
s.connect((host, 3000))
getInstructions()
except:
time.sleep(1)
print("Connect() Exception")
Connect()
Connect()
In a short form what my programs do is
Server starts and wait for incoming traffic
Client starts and sends traffic to server
The client sends every 5 second a string ("!" in this case) if the server wont recieve it the client is trying to reconnect to the server and here is the problem, the client isnt reconnecting, it is stuck inside the Connect() loop since it cant reconnect anymore
I tried to create always a new socket when reconnecting
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
def Connect():
try:
print("Connect() connecting to: "+ str(host) + str(3000))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 3000))
getInstructions()
except:
time.sleep(1)
print("Connect() Exception")
Connect()
And this seemed somehow fixed the problem, the client was able to reconnect to the server but this line did break the code even more, the client was shown as connected by the server but the client didnt recieve any data which got send.
And something else that may be good to say, then the client is connected to the server and then I stop the server the client goes into the "reconnect mode" and then i start the server again, nothing happens, the client is still trying to reconnect (but is failing). For now I have to restart the client in order to reconnect and this isnt really that what im looking for..
Upvotes: 0
Views: 815
Reputation: 13
I finally.. FINALLY! found the reason why this didn't worked.. (I'm not an expert so maybe someone could change this down there to be more "accurate")
I've created an TCP socket connection which saved its connection details probably entirely inside the variable s
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
With connection details I mean its own probably for example the server has static details that won't change that much (192.168.2.100 on port 3000) but the client doesn't use that specific port to communicate with, the client takes some random port which is available and for some reason that was the reason why the client didn't worked (But again that's just my thesis, I don't know it better..)
But here it is what I've changed
#Connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
def Reconnect(): #Ive created a new function called Reconnect, because, .., well it works fine for now
try:
global s #Its accessing the global socket variable
s = "" #blanks it out (not sure if i have to blank it out)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create again a new socket
s.connect((host, port)) # It tries to connect again
getInstructions() # done~
except:
time.sleep(1)
print("Reconnecting")
Reconnect()
def Connect():
try:
print("Connect() connecting to: "+ str(host) + str(port))
s.connect((host, port))
getInstructions()
except:
time.sleep(1)
print("Connect() Exception, trying to Reconnect()")
Reconnect()
But that's why I'm loving programming so much, its ofcourse a 2 sliced knife, but damn, that feels you get after solving a stupid bug with 10sec. worth of typing
Upvotes: 1
Reputation: 21
The server needed to receive commands.
Server.py
import socket
import os
port = 3000
#Starting Server
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = socket.gethostname()
serversocket.bind((host, port))
serversocket.listen(1)
clientsocket, addr = serversocket.accept()
print("Connection from: " + str(addr))
clientsocket.send("-:-:-:-:-: Connected in theServer :-:-:-:-:-")
def menuHelp():
clientsocket.send("\
+-----------------------------------+ \n \
| Help Menu | \n \
+-----------------------------------+ \n \
| help - shows help menu | \n \
+-----------------------------------+ \n \
| clear - clears | \n \
+-----------------------------------+")
while True:
msg = clientsocket.recv(4096)
msg = msg.decode("UTF-8")
if msg == "help":
menuHelp()
elif msg == "clear":
clientsocket.send("Command Clear\n")
else:
clientsocket.send("invalid option\n")
menuHelp()
and the client.
Client.py
import socket
import time
def send(msg):
try:
s.send(msg.encode("UTF-8"))
except:
Connect()
def getInstructions():
try:
msg = s.recv(4096)
print(msg.decode("UTF-8"))
input("Press ANY key to return")
except:
Connect()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
def Connect():
try:
print("Connect() connecting to: "+ str(host) + str(3000))
s.connect((host, 3000))
send("help")
while True:
getInstructions()
cli = input(" ->")
send(cli)
except:
time.sleep(1)
print("Connect() Exception")
Connect()
Connect()
Upvotes: 0