0% found this document useful (0 votes)
59 views2 pages

Transmit Data Client Program

This document describes a client program that connects to a server running LabVIEW to transmit and receive data over a socket connection. It uses threading to run transmission and reception concurrently. The client connects to the server, starts two threads for transmission and reception that run continuously while the connection is open, and closes the connection when the user presses Ctrl-C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views2 pages

Transmit Data Client Program

This document describes a client program that connects to a server running LabVIEW to transmit and receive data over a socket connection. It uses threading to run transmission and reception concurrently. The client connects to the server, starts two threads for transmission and reception that run continuously while the connection is open, and closes the connection when the user presses Ctrl-C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Client Program

import socket
import threading
import os
import sys
import select
from time import sleep
class Transmit(threading.Thread):
def run(self):
# Sending data to the server
#print('Transmit started')
global s # connection
global threadRunning
while(threadRunning):
# Read text with 1 sec timeout
i, o, e = select.select( [sys.stdin], [], [], 1 )
if(i):
string = sys.stdin.readline().strip() + '\r\n'
byteArray
=
bytearray(string,
"utf-8")
#
Convert string into a b$
s.sendall(byteArray)
#print('Transmit stopped - threadRunning')
return
class Receive(threading.Thread):
def run(self):
# Receiving data from the server
#print('Receive started')
global s # connection
global threadRunning
while(threadRunning):
# Use 1 second timeout on receive
ready = select.select( [s], [], [], 1 )
if(ready[0]):
data = s.recv(1024).strip()
if(len(data) == 0): # Connection closed by
server
threadRunning = False
else:
print('Received:',
data.decode("utf8")) # Converts thhe receive$
sleep(0.1)
#print('Receive stopped - threadRunning')
return
# Running main program

HOST = '10.0.0.245' # The remote host - windows machine running the LabVIEW
Server
PORT = 2055 # The same port as used by the server - defined in LabVIEW
global threadRunning # Used to stop threads
threadRunning = False
global s
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
os.system('clear')
print('Connection with server established')
try:
print(34 * '-')
print("
M A I N - M E N U")
print(' Press CTRL+C to close connection')
print(34 * '-')
# Create instance of class
threadRunning = True
transmit = Transmit()
receive = Receive()
# Start class
transmit.start()
receive.start()
while(threadRunning):
sleep(0.1)
except KeyboardInterrupt: # Stop program when CTRL+C is pressed
#print('Main stopped')
threadRunning = False
sleep(2)
s.close()
finally:
s.close()

You might also like