Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Socket - Keep Alive?
#1
I am trying to write a small python program that just sends data out over a port socket. I can get it to work when there is a server that is listening, but I can't get it to work when there is nothing there. I've tried searching but haven't found anything that will work without a receiving server.

I am basically looking for a way that will attempt to connect, if it fails, wait X and try again.

Anyone have any suggestions or examples they can share?

This is what I have so far.....
Quote:import socket
import time
import string

host = '192.168.1.11' #socket.gethostname()
port = 9999
BUFFER_SIZE = 2000
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

def testConnection(host, port, socket):
connected = False

while connected == False:
try:
socket.connect((host, port))
connected = True
socket.send("Hello")
except Exception as e:
print ("Exception is %s" % (e))
time.sleep(2)
connected = False

testConnection(host, port, socket)

And this is what I'm getting in return.
Quote:Exception is [Errno 61] Connection refused
Exception is [Errno 22] Invalid argument
Reply


Messages In This Thread
Socket - Keep Alive? - by CDitty - Nov-21-2017, 07:42 PM
RE: Socket - Keep Alive? - by heiner55 - Nov-22-2017, 03:23 AM
RE: Socket - Keep Alive? - by Windspar - Nov-28-2017, 09:42 PM
RE: Socket - Keep Alive? - by wavic - Nov-29-2017, 10:02 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020