The following is my code for server and client. The client receives the first 4 bytes telling the length of the data block, which is then received by the client from the server. Once received, it performs certain calculations and writes it into the file and then receives the next 4 bytes to get the next block and so on. After the first time of running the loop and writing into the file, it fails to receives the next 4 bytes and hence comes out of the loop. Not sure why this is happening. Please let me know where am I going wrong. Thank you in advance!
Server code
import socket
import threading
import os
def Main():
host = '127.0.0.1'
port = 5009
s = socket.socket()
s.bind((host,port))
s.listen(5)
print("Server started")
while True:
c,addr = s.accept()
print("Client connected ip:<" + str(addr) + ">")
c.sendall('1685 2020/03/02 14:42:05 318397 4 1 25 0 0 0 0 1513,094 1516,156 1519,154 1521,969 1525,029 1527,813 1530,921 1533,869 1536,740 1539,943 1542,921 1545,879 1548,843 1551,849 1554,760 1557,943 1560,782 1563,931 1566,786 1569,751 1572,690 1575,535 1578,638 1581,755 1584,759 41 39 33 39 48 44 49 55 61 58 64 55 68 74 68 59 57 74 61 68 58 64 54 47 46 2 25 0 0 0 0 1512,963 1515,935 1518,857 1521,849 1524,655 1527,577 1530,332 1533,233 1536,204 1539,488 1542,571 1545,725 1549,200 1552,430 1555,332 1558,484 1561,201 1564,285 1567,001 1569,870 1572,758 1575,491 1578,512 1581,547 1584,405 48 43 37 42 57 54 59 62 67 58 71 59 77 82 82 64 71 88 77 79 72 73 63 49 50 3 25 0 0 0 0 1513,394 1516,517 1519,536 1522,082 1525,428 1527,963 1531,288 1534,102 1536,659 1539,757 1542,707 1545,627 1548,389 1551,459 1554,406 1557,986 1560,667 1564,103 1567,036 1570,144 1573,189 1575,888 1579,185 1582,323 1585,338 35 36 32 37 57 58 61 64 75 73 70 62 61 62 59 51 52 64 58 62 70 70 64 54 55 4 25 0 0 0 0 1512,658 1515,752 1518,797 1521,707 1524,744 1527,627 1530,871 1534,002 1537,086 1540,320 1543,217 1546,010 1548,660 1551,385 1554,253 1557,074 1560,193 1563,116 1566,043 1568,963 1571,855 1574,957 1577,954 1581,128 1584,273 43 42 39 40 56 50 56 62 65 54 59 62 75 79 73 63 67 77 73 75 68 62 54 51 51 100 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN'.encode())
c.sendall('1685 2020/03/03 14:42:05 318398 4 1 25 0 0 0 0 1513,094 1516,156 1519,154 1521,969 1525,029 1527,812 1530,921 1533,869 1536,740 1539,943 1542,922 1545,878 1548,843 1551,849 1554,760 1557,944 1560,782 1563,931 1566,786 1569,751 1572,691 1575,535 1578,638 1581,755 1584,758 41 39 33 39 48 44 49 55 61 58 64 55 68 74 68 59 57 74 61 68 58 64 54 47 46 2 25 0 0 0 0 1512,963 1515,935 1518,857 1521,849 1524,655 1527,577 1530,332 1533,233 1536,204 1539,489 1542,571 1545,725 1549,200 1552,430 1555,331 1558,484 1561,201 1564,285 1567,002 1569,870 1572,758 1575,491 1578,512 1581,547 1584,405 48 43 37 42 57 54 58 62 67 58 70 59 77 82 82 64 71 88 77 79 72 73 63 49 50 3 25 0 0 0 0 1513,394 1516,517 1519,536 1522,082 1525,427 1527,963 1531,288 1534,102 1536,659 1539,757 1542,707 1545,627 1548,389 1551,459 1554,406 1557,986 1560,666 1564,103 1567,036 1570,144 1573,190 1575,887 1579,185 1582,323 1585,338 34 35 32 37 57 58 61 64 75 73 70 62 61 61 59 51 52 64 58 62 70 70 64 54 55 4 25 0 0 0 0 1512,658 1515,753 1518,797 1521,707 1524,744 1527,627 1530,872 1534,002 1537,086 1540,320 1543,217 1546,011 1548,660 1551,385 1554,253 1557,074 1560,193 1563,116 1566,043 1568,963 1571,855 1574,957 1577,953 1581,129 1584,273 43 42 39 40 56 50 56 62 65 54 59 62 75 79 73 63 67 77 73 75 68 62 54 51 51 100 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN'.encode())
c.sendall('1685 2020/03/04 14:42:05 318398 4 1 25 0 0 0 0 1513,094 1516,156 1519,154 1521,969 1525,029 1527,812 1530,921 1533,869 1536,740 1539,943 1542,922 1545,878 1548,843 1551,849 1554,760 1557,944 1560,782 1563,931 1566,786 1569,751 1572,691 1575,535 1578,638 1581,755 1584,758 41 39 33 39 48 44 49 55 61 58 64 55 68 74 68 59 57 74 61 68 58 64 54 47 46 2 25 0 0 0 0 1512,963 1515,935 1518,857 1521,849 1524,655 1527,577 1530,332 1533,233 1536,204 1539,489 1542,571 1545,725 1549,200 1552,430 1555,331 1558,484 1561,201 1564,285 1567,002 1569,870 1572,758 1575,491 1578,512 1581,547 1584,405 48 43 37 42 57 54 58 62 67 58 70 59 77 82 82 64 71 88 77 79 72 73 63 49 50 3 25 0 0 0 0 1513,394 1516,517 1519,536 1522,082 1525,427 1527,963 1531,288 1534,102 1536,659 1539,757 1542,707 1545,627 1548,389 1551,459 1554,406 1557,986 1560,666 1564,103 1567,036 1570,144 1573,190 1575,887 1579,185 1582,323 1585,338 34 35 32 37 57 58 61 64 75 73 70 62 61 61 59 51 52 64 58 62 70 70 64 54 55 4 25 0 0 0 0 1512,658 1515,753 1518,797 1521,707 1524,744 1527,627 1530,872 1534,002 1537,086 1540,320 1543,217 1546,011 1548,660 1551,385 1554,253 1557,074 1560,193 1563,116 1566,043 1568,963 1571,855 1574,957 1577,953 1581,129 1584,273 43 42 39 40 56 50 56 62 65 54 59 62 75 79 73 63 67 77 73 75 68 62 54 51 51 100 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN'.encode())
c.sendall('1685 2020/03/05 14:42:05 318398 4 1 25 0 0 0 0 1513,094 1516,156 1519,154 1521,969 1525,029 1527,812 1530,921 1533,869 1536,740 1539,943 1542,922 1545,878 1548,843 1551,849 1554,760 1557,944 1560,782 1563,931 1566,786 1569,751 1572,691 1575,535 1578,638 1581,755 1584,758 41 39 33 39 48 44 49 55 61 58 64 55 68 74 68 59 57 74 61 68 58 64 54 47 46 2 25 0 0 0 0 1512,963 1515,935 1518,857 1521,849 1524,655 1527,577 1530,332 1533,233 1536,204 1539,489 1542,571 1545,725 1549,200 1552,430 1555,331 1558,484 1561,201 1564,285 1567,002 1569,870 1572,758 1575,491 1578,512 1581,547 1584,405 48 43 37 42 57 54 58 62 67 58 70 59 77 82 82 64 71 88 77 79 72 73 63 49 50 3 25 0 0 0 0 1513,394 1516,517 1519,536 1522,082 1525,427 1527,963 1531,288 1534,102 1536,659 1539,757 1542,707 1545,627 1548,389 1551,459 1554,406 1557,986 1560,666 1564,103 1567,036 1570,144 1573,190 1575,887 1579,185 1582,323 1585,338 34 35 32 37 57 58 61 64 75 73 70 62 61 61 59 51 52 64 58 62 70 70 64 54 55 4 25 0 0 0 0 1512,658 1515,753 1518,797 1521,707 1524,744 1527,627 1530,872 1534,002 1537,086 1540,320 1543,217 1546,011 1548,660 1551,385 1554,253 1557,074 1560,193 1563,116 1566,043 1568,963 1571,855 1574,957 1577,953 1581,129 1584,273 43 42 39 40 56 50 56 62 65 54 59 62 75 79 73 63 67 77 73 75 68 62 54 51 51 100 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN'.encode())
c.close()
if __name__ == '__main__':
Main()
Client code
import socket
import turtle
from tkinter import *
root = Tk()
canvas = Canvas(root, width = 150, height = 50)
canvas.grid(row = 4, column = 3)
input_label = Label(root, text = "Input all the gratings set straight wavelength values in nm")
input_label.grid(row = 0)
# Green light
green_light = turtle.RawTurtle(canvas)
green_light.shape('circle')
green_light.color('grey')
green_light.penup()
green_light.goto(0,0)
# Red light
red_light = turtle.RawTurtle(canvas)
red_light.shape('circle')
red_light.color('grey')
red_light.penup()
red_light.goto(40,0)
core_string = "Core "
entries = []
label_col_inc = 0
entry_col_inc = 1
core_range = range(1, 5)
for y in core_range:
core_text = core_string + str(y) + '_' + '25'
core_label = Label(root, text = core_text)
entry = Entry(root)
core_label.grid(row=1, column=label_col_inc, sticky=E)
entry.grid(row=1, column=entry_col_inc)
entries.append(entry)
label_col_inc += 2
entry_col_inc += 2
threshold_label = Label(root, text = "Threshold in nm")
entry_threshold = Entry(root)
threshold_label.grid(row = 2, sticky = E)
entry_threshold.grid(row = 2, column = 1)
#dataNo_label = Label(root, text = "Number of data to be collected")
#entry_datNo = Entry(root)
#dataNo_label.grid(row = 3, sticky = E)
#entry_datNo.grid(row = 3, column = 1)
# function to receive TCP data blocks
def getData():
host = '127.0.0.1'
port = 5009
s = socket.socket()
s.connect((host, port))
len_message = s.recv(4)
print('len_msg is', len_message)
# input of the number of data to be received
#dataNo = float(entry_datNo.get())
while len_message:
bytes_length = int.from_bytes(len_message,'big')
data = s.recv(bytes_length)
stringdata = data.decode('utf-8')
rep_str = stringdata.replace(",", ".")
splitstr = rep_str.split()
# received wavelength values
inc = 34
wav_threshold = []
for y in entries:
straight_wav = float(y.get())
print('y is', straight_wav)
wav = float(splitstr[inc])
wav_diff = wav - straight_wav
if wav_diff < 0:
wav_diff = wav_diff * (-1)
wav_threshold.append(wav_diff)
inc += 56
threshold = float(entry_threshold.get())
# writing into the file
data = []
inc1 = 0
col1 = 2
col2 = 6
data.insert(0, (str(splitstr[0])))
data.insert(1, (str(splitstr[1])))
for x in wav_threshold:
if (x > threshold):
red_light.color('red')
green_light.color('grey')
data.insert(col1, (str(splitstr[34 + inc1])))
data.insert(col2,(str(x)))
else:
red_light.color('grey')
green_light.color('green')
data.insert(col1,'-')
data.insert(col2,'-')
inc1 += 56
col1 += 1
col2 += 1
write_file(data)
print('writing...')
len_message = s.recv(4)
print('len_message is', len_message)
print('out of loop')
s.close()
# function to write into the file
def write_file(data):
with open("Output.txt", "a") as text_file:
text_file.write('\t'.join(data[0:]))
text_file.write('\n')
data_button = Button(root, text = "Get data above threshold", command = getData)
data_button.grid(row = 5, column = 0)
root.mainloop()
The same server code works fine with the following Client code:
import socket
def Main():
host = '127.0.0.1'
port = 5001
s = socket.socket()
s.connect((host,port))
i = 0
first_wav = float(input("Enter the threshold for 1-25: "))
second_wav = float(input("Enter the threshold for 2-25: "))
third_wav = float(input("Enter the threshold for 3-25: "))
fourth_wav = float(input("Enter the threshold for 4-25: "))
len_message = s.recv(4)
while len_message:
print(len_message)
while i < 2:
bytes_length = int(len_message.decode())
data = s.recv(bytes_length)
stringdata = data.decode('utf-8')
rep_str = stringdata.replace(",",".")
splitstr = rep_str.split()
maxlength = len(splitstr))
print('1-25 wavelength value:', splitstr[34])
print('2-25 wavelength value:', splitstr[90])
print('3-25 wavelength value:', splitstr[146])
print('3-25 wavelength value:', splitstr[202], '\n')
print('Threshold limit check:')
a = float(splitstr[34])
b = float(splitstr[90])
c = float(splitstr[146])
d = float(splitstr[202])
if a > first_wav:
print('1-25 Above Threshold')
if b > second_wav:
print('2-25 Above Threshold')
if c > third_wav:
print('3-25 Above Threshold')
if d > fourth_wav:
print('4-45 Above Threshold \n')
len_message = s.recv(4)
i+=1
print('i is', i)
else:
print('out of loop')
s.close()
def write_file(data):
with open("Output.txt", "ab") as text_file:
text_file.write(data)
print('data written')
text_file.write('\n'.encode())
def write_excel(data):
df = pd.DataFrame(data)
df.to_excel('Output_Excel.xlsx','Sheet 1')
if __name__ == '__main__':
Main()
´´´