Introduction: Python TCP Client + Threads

Picture of Python TCP Client + Threads

Hello everybody!

This is my very first instructable, and I am very happy being joined to this community, and hope you love this and my others instructables and find them useful. English is not my first language so sorry for any mistake.

This instructable is part of a bigger instructable which I'm going to post soon, but it is worth looking to as a separate and standalone project.

As the name shows this instructable is a TCP client written in Python 2 and uses threading as heart of it, it has features like trying to reconnect until host is available, sending and receiving data( if not, why we are here then?!), it also implements a feature I call it heartbeat to ensure that the connection with server is open or not by waiting for and sending chunks of data when the connection is idle.

Step 1: A Little About TCP Connection

Transmission Control Protocol or TCP enables two hosts to establish a connection and exchange streams of data. TCP guarantees delivery of data and also guarantees that packets will be delivered in the same order in which they were sent.

In TCP we have server and client, the server is the side that will listen on a specific port for incoming connections and the client is the one that connects to a listening server, in another view, client is the initiator of the TCP session,

Step 2: About the Program

I believe the code it self contains enough comment so that those with some programming skill can at least understand the whole program flow, if not the entire code, but just to add more insight

First of all I started a new thread that will run the tcpConnect() function, this function will try to connect to port 5445(I love this port ) on the same machine, the IP and port can be changed by user, as socket.connect function will throw an exception if connecting fail, I used a try except to form a retry method for connection, so the thread will loop until the connection is established, then it will set proper flags and starts a new thread that is responsible for reading from socket, the thread will call a user defined call back function whenever it detects a full line of data ending with \r\n,

for now and in this project we only print the received data,

I also implemented a timer class that was responsible for calling a user defined timeout callback function on a regular user specified interval, in this timeout callback we increment two flags HBCounter and HBSent, these flags are used to identify how long the network has been idle in both directions,i.e. server to client and client to server, you may ask why we need these two? because we wanna ensure that the connection is up and healthy, and TCP will check this by default after say two hours of inactivity, which is pretty long time, so we use this mechanism to reduce this time to an acceptable value,

You can safely remove the timer for test purposes, but in order to have a higher level of reliability you should use the timer, if so the server you use must be capable of sending heartbeat packets to the client or the client will drop the connection and establishes a new one after the some timer based condition in the code is met,

Step 3: The Program Itself

I'm not a professional python developer and I've been recently introduced to python, so the code may not be optimized in term of python,

feel free to use, edit or share the code but keep in mind that the code here has no guarantee to work in the desired way, so use it AT YOUR OWN RISK.

the code still has bugs I know about, I try to solve them, but also have to say it is enough for my final project( as I said this is a project inside a bigger one), the code can also be improved by adding GUI to it

I hope U like this instructable, if so please like me and/or comment, I'll be happy,thanks

Source code

Source code(mirror)

Comments

kashan.ahmad (author)2015-07-16

hi. the link for code is not working. kindly check.

hessam2013 (author)kashan.ahmad2015-07-16

Hi, thanks for comment, added a mirror,please use that in case of failure

kashan.ahmad (author)hessam20132015-07-17

Thank you. Mirror is working fine :)

tomatoskins (author)2015-07-16

Really cool first instructable! I'm excited to see more from you in the future!

hessam2013 (author)tomatoskins2015-07-16

Hi, thanks, I'll try to make my instructables more interesting

About This Instructable

3,571views

20favorites

License:

More by hessam2013:Python TCP client + threads
Add instructable to: