-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcp_client.py
86 lines (68 loc) · 2.44 KB
/
tcp_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>
import socket
import sys
import numpy as np
import struct
import scipy
import time
import datetime
#########################################################
#Sets up a tcp_client to receive data from USRP tcp_sink
#and dump data to file
#########################################################
###GLOBALS
file_size = 9999999 #Number of data points in single traces
no_of_files = 100000000 #Number of traces to be taken
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('127.0.0.1', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
#Set client address and port number
client_address=('127.0.0.1',10000)
while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
connection, client_address = sock.accept()
try:
print >>sys.stderr, 'connection from', client_address
num_of_points = 0 #counter for no. of points
file_no = 0 #counter for no. of files
#Name data dump file
fname="Trace_0000"
##########################################################
#Uncheck these lines for timestamping dump files
##########################################################
# st=time.mktime(datetime.datetime.now().timetuple())
# fname='./Trace_'+str(st)
f = open(fname, 'a')
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(8);
f.write(data)
num_of_points += 1
if(num_of_points == file_size):
f.close()
print "Done.: "+fname
if(file_no == no_of_files):
break
num_of_points = 0
file_no += 1
#New dump file name
fname="Trace_0000"
##########################################################
#Uncheck these lines for timestamping dump files
##########################################################
# st=time.mktime(datetime.datetime.now().timetuple())
# fname='./Trace_'+str(st)
f = open(fname, 'a')
print "Opening new file : "+fname
print "Done creating files.."
finally:
# Clean up the connection
connection.close()