I have a simple question about socket programming. I've done to implement a server and two clients as following codes, but all of sudden it stops while it communicates each other. I have no idea why it happens all the time.
Would you give me advice, hint, or help?
Thank you for your time.
Client
# simple client
import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ""
port = 12345
buf = 100
s.connect((host, port))
i = 0
timestep = 0
while(1):
k = '01'#+str(timestep)
#1
s.send(k)
print 'sending message is', k
#2
v = s.recv(buf)
print v
if v == 'hold 01':
print 'timestep is', timestep#'send the previous message'
if timestep == 0:
timestep == 0
else:
timestep -= 1
else:
print 'read data'
FILE = open("datainfo1.txt", "r+")
msg = FILE.read()
FILE.close()
#3
while(1):
tmp, msg = msg[:buf], msg[buf:]
s.send(tmp)
print len(tmp)
if len(tmp) < buf:
print 'break'
break
# send file
i+=1
timestep+=1
print 'end'
s.close()
Server
import socket, sys
# set up listening socket
lstn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ""
port = 12345
# bind lstn socket to this port
lstn.bind(("", port))
lstn.listen(5)
# initialize buffer size
buf = 100
# initialize concatenate string, v
v = ''
# initialize client socket list
cs = []
check = [0, 0]
# a fixed number of clients
nc = 1
count = 0
status = [0,0]
j = 0
step = [0,0]
for i in range(nc):
(clnt,ap) = lstn.accept()
clnt.setblocking(0)
cs.append(clnt)
file = open("output_analysis.txt","w+")
while (len(cs) > 0):
clnt = cs.pop(0)
cs.append(clnt)
try:
#1
k = clnt.recv(buf)
print "k=",k,"\n"#[:2]
if k == '01':
print "1st client connected", status[0]
file.write("1st\t")
if status[0] == 0:
v = 'hold 01'
check[0] = 1
elif status[0] == 1:
v = 'go 01'
# status[0] = 0
print v, "the message\n"
file.write(v)
file.write("\t")
#2
clnt.send(v)
if status[0] == 1:
FILE = open("client1.txt","w+")
print 'receive 01'
#3
while(1):
status[0] = 0
print '1st : receiving'
msg = clnt.recv(buf)
print len(msg)
if len(msg) < buf:
print 'break'
#FILE.close()
break
elif k == '02':
print "2nd client connected", status[0]
file.write("2nd\t")
if status[1] == 0:
v = 'hold 02'
check[1] = 1
elif status[1] == 1:
v = 'go 02'
# status[0] = 0
print v, "the message\n"
file.write(v)
file.write("\t")
#2
clnt.send(v)
if status[1] == 1:
FILE = open("client2.txt","w+")
print 'receive 02'
#3
while(1):
status[1] = 0
print '2nd : receiving'
msg = clnt.recv(buf)
print len(msg)
if len(msg) < buf:
print 'break'
#FILE.close()
break
if check[0] == 1:# and check[1] == 1:
print j, 'here\n'
j += 1
for i in range(2):
check[i] = 0
status[i] = 1 # which means, all clients are connected
print check, status
else:
print 'hello'
except: pass
file.close()
lstn.close()
except: pass
You're ignoring all exceptions. This is a bad idea, because it doesn't let you know when things go wrong. You'll need to remove that except handler before you can reasonably debug your code.
One problem is you are setting non-blocking mode:
clnt.setblocking(0)
But never checking that it is safe to send or recv data. It turns out that since the code is ignoring all exceptions, it isn't seeing the following error that I got when I removed the try:
Exception [Errno 10035] A non-blocking socket operation could not be completed immediately
After removing the setblocking call, the code proceeded further, but still has problems.
Describing what problem you are trying to solve would help to understand what the code is trying to do.
Related
I'm writing a program which should add chars to strings in three different processes. If the resulting string matches a given one, the process which has the match, should send an "endflag" to process 0. After process 0 receives the "endflag" he should send a broadcast to all other processes (1 - 3) and exit. The other processes receive the broadcast and should exit too / stop working.
[...]
string_given = "abc"
magic_bytes = "0xDEA7C0DE"
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 !?#/\%&.-_:,;*+<>|(){}'
end_all = None
if rank == 0:
while(True):
recv_data = comm.recv(source=MPI.ANY_SOURCE)
print("0.recv: %s" % (recv_data))
if recv_data == magic_bytes:
end_all = magic_bytes
send_data = bcast(end_all, root=0)
sys.exit()
if rank == 1:
min, max = 1, 2
while(True):
for n in xrange(min, max):
for c in itertools.product(chars, repeat=n):
string = ''.join(c)
print("1.string: %s" % (string))
if string == string_given:
print("1.found: %s = %s" % (string, string_given))
end_all = magic_bytes
comm.send(end_all, dest=0)
recv_data = comm.bcast(end_all, root=0)
if recv_data == magic_bytes:
sys.exit()
if rank == 2:
min, max = 3, 4
while(True):
for n in xrange(min, max):
for c in itertools.product(chars, repeat=n):
string = ''.join(c)
print("2.string: %s" % (string))
if string == string_given:
print("2.found: %s = %s" % (string, string_given))
end_all = magic_bytes
comm.isend(end_all, dest=0)
recv_data = comm.bcast(end_all, root=0)
if recv_data == magic_bytes:
sys.exit()
[...]
The code above produces following output:
3.string: aaaaa
1.string: a
2.string: aaa
And then i think it deadlocks.
How can i prevent my code from the deadlock?
I have successful sending and receiving between test_client and test_server for a few sends and receives. But when I input more than 4 or 5 data and click enter after each at the input() function the program hangs. I think I've narrowed the problem down to likely be either the rdt_rcv() or udt_send() function. I've highlighted with comments the rdt_rcv() function for both sender and receiver and added a comment above the udt_send() function. But I can't see what's wrong with the functions. Of course it could be something else but I think the problem is one of the two functions I mentioned above. Try running the programs by running test_server first and test_client second with rdt.py in the same folder as the 2 test modules.
test_client.py
import socket, rdt, sys
TCP_IP = 'localhost'
TCP_PORT = 10000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
InitialSeqNumber = 0
NextSeqNum = InitialSeqNumber
SendBase = InitialSeqNumber
while 1:
d = input("Input data: ")
data = rdt.rdt_send(d)
data = int(data)
if rdt.rdt_send_called == 1:
checksum = 0xFFFF - data # Assuming 28 byte data
sndpkt = rdt.packet(NextSeqNum, 0, checksum, data)
rdt.udt_send(sndpkt, s)
NextSeqNum = NextSeqNum + sys.getsizeof(data)
#####################################
seq, ack, check, dat = rdt.rdt_rcv(s)
#####################################
rdt.rdt_send_called = 0
print ("Sequence Number: " + str(seq))
print ("ACK Number: " + str(ack))
print ("Checksum Value: " + hex(check))
print ("Data: " + str(dat))
print ('\n')
s.close()
rdt.py
rdt_send_called = None
timeout_event = None
ACK_received = None
BUFFER_SIZE = 1024
class packet:
def __init__(self, seq_num, ack_num, checksum, data):
self.seq_num = seq_num
self.ack_num = ack_num
self.checksum = checksum
self.data = data
def rdt_send(data):
d = data
global rdt_send_called
rdt_send_called = 1
return d
# PROBLEM MAY BE WITH THIS FUNCTION
def udt_send(sndpkt, s):
s.send((str(sndpkt.seq_num)).encode())
s.send((str(sndpkt.ack_num)).encode())
s.send((str(sndpkt.checksum)).encode())
s.send((str(sndpkt.data)).encode())
# PROBLEM IS MOST LIKELY WITH THIS FUNCTION
def rdt_rcv(conn):
field1 = conn.recv(BUFFER_SIZE)
field1 = int(field1)
field2 = conn.recv(BUFFER_SIZE)
field2 = int(field2)
#if field2 != 0:
#global ACK_received
#ACK_received = 1
field3 = (conn.recv(BUFFER_SIZE))
field3 = int(field3)
field4 = (conn.recv(BUFFER_SIZE))
field4 = int(field4)
return field1, field2, field3, field4
def deliver_data(data):
pass
def timeout():
global timeout_event
timeout_event = 1
test_server.py
import socket, rdt, sys
TCP_IP = '127.0.0.1'
TCP_PORT = 10000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
while 1:
dont_send_ACK = 0
#####################################################
seq_num, ack_num, check_sum, data = rdt.rdt_rcv(conn)
#####################################################
print ("Sequence Number: " + str(seq_num))
print ("ACK number: " + str(ack_num))
print ("Checksum Value: " + hex(check_sum))
print ("Data: " + hex(data))
print ('\n')
if data + check_sum != 0xFFFF:
dont_send_ACK = 1 # sender will timeout
else:
rdt.deliver_data(data)
if dont_send_ACK == 0:
ACK = seq_num + sys.getsizeof(data)
checksum = 0xFFFF - ACK
sndpkt = rdt.packet(0, ACK, checksum, 0)
rdt.udt_send(sndpkt, conn)
conn.close()
I have been dealing with sending file which is divided into fragments set by user on input. Problem is, I am getting error:
rec_list[fragIndex - 1] = data
IndexError: list assignment index out of range
I am also sending single string messages like in a chat and it works normally.
I can't find a bug in my code but maybe you can.
Btw, there are variables which might help with math which doesn't fit probably.
fragSize = 3
fragIndex = 216
fragCount = 215
Problem is, total number of fragments should be 215 (precalculated before sending - IS OK), index shouldn't be more than count! That is the problem. And it doesn't happen with strings. Only here with file.
Sending:
fragSize = int(input('Fragment size: ')) #max size of fragment
while True:
message = input('Enter message: ')
fragIndex=0 #reset fragment indexing
#asking for fragment size
if(message[:3] == '-sf'):
fragSize = int(input('Fragment size: '))
And here is sending function for files:
if (message[:2] == '-f'):
mType = 3
if message.startswith('-f'):
message = message[3:]
file_name = message
f=open(file_name,"rb")
contents = f.read()
fragCount = math.ceil(len(contents) / fragSize)
while contents!= '':
data = bytearray()
data.extend(contents[:fragSize])
fragIndex += 1
crc = crc32(data)
header = struct.pack('!hIIII', mType, fragSize, fragIndex, fragCount, crc)
self.sock.sendto(header + bytearray(data), (self.host, self.port))
contents = contents[fragSize:]
Receiving:
while True:
received_chunks = 0
rec_list = []
while True:
data, addr = sock.recvfrom(65535)
header = data[:18]
data = data[18:]
(mType, fragSize, fragIndex, fragCount, crc) = struct.unpack('!hIIII', header)
print(
'\nTyp: ' + str(mType) +
'\nFragSize: ' + str(fragSize) +
'\nFragIndex: ' + str(fragIndex) +
'\nFragCount: ' + str(fragCount) +
'\nCRC: ' + str(crc)
)
if len(rec_list) < fragCount:
need_to_add = fragCount - len(rec_list)
rec_list.extend([''] * need_to_add) # empty list for messages of size fragCount
rec_list[fragIndex - 1] = data
received_chunks += 1
if received_chunks == fragCount:
break # This is where the second while loop ends
This is only if I want to receive message of type file: (because it is divided into more message types)
if mType == 3:
content = b''.join(rec_list)
f = open('filename.py','wb')
f.write(content)
You tried to compare apples to oranges. Well, bytes to str but wikipedia doesn't say anything about that.
while contents!='':
...
contents is a bytes object and '' is a str object. In python 3, those two things can never be equal. Firing up the shell we see that
>>> b''==''
False
>>>
>>> contents = b"I am the very model"
>>> while contents != '':
... if not contents:
... print("The while didn't catch it!")
... break
... contents = contents[3:]
...
The while didn't catch it!
Since all objects have a truthiness (that is, bool(some_object) is meaningful) and bytes objects turn False when they are empty, you can just do
while contents:
....
UPDATE
Not part of the original answer but a question was raised about sending retries back to the client. The server side is sketched in here
while True:
received_chunks = 0
fragCount = -1
rec_list = []
while True:
# wait forever for next conversation
if fragCount == -1:
sock.settimeout(None)
try:
data, addr = sock.recvfrom(65535)
except socket.timeout:
# scan for blank slots in rec_list
retries = [i for i, data in rec_list if not data]
# packet is mType, numFrags, FragList
# TODO: I just invented 13 for retry mtype
sock.sendto(struct.pack("!{}h".format(len(retries+2)), 13,
len(retries), *retries)
continue
# our first packet, set timeout for retries
if fragCount == -1:
sock.settimeout(2)
header = data[:18]
data = data[18:]
(mType, fragSize, fragIndex, fragCount, crc) = struct.unpack('!hIIII', header)
print(
'\nTyp: ' + str(mType) +
'\nFragSize: ' + str(fragSize) +
'\nFragIndex: ' + str(fragIndex) +
'\nFragCount: ' + str(fragCount) +
'\nCRC: ' + str(crc)
)
if len(rec_list) < fragCount:
need_to_add = fragCount - len(rec_list)
rec_list.extend([''] * need_to_add) # empty list for messages of size fragCount
rec_list[fragIndex - 1] = data
received_chunks += 1
if received_chunks == fragCount:
break # This is where the second while loop ends
I am using for loop to get the mac address from database but the loop printed the ble mac address 17 times. I tried using if else to compare the mac address with the mac address stored in database but the output scan nothing. Is there any other way to filter out other ble mac address?
import os
import sys
import struct
import bluetooth._bluetooth as bluez
import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","DB_USER","DB_PASSWORD","DB_NAME" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
sql = "SELECT * FROM Mac_address"
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
for row in results:
NAME = row[0]
mac_address = row[1]
time = row[2]
LE_META_EVENT = 0x3e
LE_PUBLIC_ADDRESS=0x00
LE_RANDOM_ADDRESS=0x01
LE_SET_SCAN_PARAMETERS_CP_SIZE=7
OGF_LE_CTL=0x08
OCF_LE_SET_SCAN_PARAMETERS=0x000B
OCF_LE_SET_SCAN_ENABLE=0x000C
OCF_LE_CREATE_CONN=0x000D
LE_ROLE_MASTER = 0x00
LE_ROLE_SLAVE = 0x01
# these are actually subevents of LE_META_EVENT
EVT_LE_CONN_COMPLETE=0x01
EVT_LE_ADVERTISING_REPORT=0x02
EVT_LE_CONN_UPDATE_COMPLETE=0x03
EVT_LE_READ_REMOTE_USED_FEATURES_COMPLETE=0x04
# Advertisment event types
ADV_IND=0x00
ADV_DIRECT_IND=0x01
ADV_SCAN_IND=0x02
ADV_NONCONN_IND=0x03
ADV_SCAN_RSP=0x04
def returnnumberpacket(pkt):
myInteger = 0
multiple = 256
for c in pkt:
myInteger += struct.unpack("B",c)[0] * multiple
multiple = 1
return myInteger
def returnstringpacket(pkt):
myString = "";
for c in pkt:
myString += "%02x" %struct.unpack("B",c)[0]
return myString
def printpacket(pkt):
for c in pkt:
sys.stdout.write("%02x " % struct.unpack("B",c)[0])
def get_packed_bdaddr(bdaddr_string):
packable_addr = []
addr = bdaddr_string.split(':')
addr.reverse()
for b in addr:
packable_addr.append(int(b, 16))
return struct.pack("<BBBBBB", *packable_addr)
def packed_bdaddr_to_string(bdaddr_packed):
return ':'.join('%02x'%i for i in struct.unpack("<BBBBBB", bdaddr_packed[::-1]))
def hci_enable_le_scan(sock):
hci_toggle_le_scan(sock, 0x01)
def hci_disable_le_scan(sock):
hci_toggle_le_scan(sock, 0x00)
def hci_toggle_le_scan(sock, enable)
cmd_pkt = struct.pack("<BB", enable, 0x00)
bluez.hci_send_cmd(sock, OGF_LE_CTL, OCF_LE_SET_SCAN_ENABLE, cmd_pkt)
def hci_le_set_scan_parameters(sock):
old_filter = sock.getsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, 14)
SCAN_RANDOM = 0x01
OWN_TYPE = SCAN_RANDOM
SCAN_TYPE = 0x01
def parse_events(sock, loop_count=100):
old_filter = sock.getsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, 14)
# perform a device inquiry on bluetooth device #0
# The inquiry should last 8 * 1.28 = 10.24 seconds
# before the inquiry is performed, bluez should flush its cache of
# previously discovered devices
flt = bluez.hci_filter_new()
bluez.hci_filter_all_events(flt)
bluez.hci_filter_set_ptype(flt, bluez.HCI_EVENT_PKT)
sock.setsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, flt )
done = False
results = []
myFullList = []
for i in range(0, loop_count):
pkt = sock.recv(255)
ptype, event, plen = struct.unpack("BBB", pkt[:3])
#print "--------------"
if event == bluez.EVT_INQUIRY_RESULT_WITH_RSSI:
i =0
elif event == bluez.EVT_NUM_COMP_PKTS:
i =0
elif event == bluez.EVT_DISCONN_COMPLETE:
i =0
elif event == LE_META_EVENT:
subevent, = struct.unpack("B", pkt[3])
pkt = pkt[4:]
if subevent == EVT_LE_CONN_COMPLETE:
le_handle_connection_complete(pkt)
elif subevent == EVT_LE_ADVERTISING_REPORT:
#print "advertising report"
num_reports = struct.unpack("B", pkt[0])[0]
report_pkt_offset = 0
for i in range(0, num_reports):
Mac = packed_bdaddr_to_string(pkt[report_pkt_offset + 3:report_pkt_offset + 9])
for Mac in row[1]:
print "-------------"
#print "\tfullpacket: ", printpacket(pkt)
print "\tMAC address: ", row[1] # commented out - don't know what this byte is. It's NOT TXPower
txpower, = struct.unpack("b", pkt[report_pkt_offset -2])
print "\t(Unknown):", txpower
rssi, = struct.unpack("b", pkt[report_pkt_offset -1])
print "\tRSSI:", rssi
break
# build the return string
Adstring = packed_bdaddr_to_string(pkt[report_pkt_offset + 3:report_pkt_offset + 9])
Adstring += ","
Adstring += returnstringpacket(pkt[report_pkt_offset -22: report_pkt_offset - 6])
Adstring += ","
Adstring += "%i" % returnnumberpacket(pkt[report_pkt_offset -6: report_pkt_offset - 4])
Adstring += ","
Adstring += "%i" % returnnumberpacket(pkt[report_pkt_offset -4: report_pkt_offset - 2])
Adstring += ","
Adstring += "%i" % struct.unpack("b", pkt[report_pkt_offset -2])
Adstring += ","
Adstring += "%i" % struct.unpack("b", pkt[report_pkt_offset -1])
#print "\tAdstring=", Adstring
myFullList.append(Adstring)
done = True
sock.setsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, old_filter )
return myFullList
db.close()
The above is save as blescan.py. I run this file below to do a ble scan.
import blescan
import sys
import bluetooth._bluetooth as bluez
dev_id = 0
try:
sock = bluez.hci_open_dev(dev_id)
print "ble thread started"
except:
print "error accessing bluetooth device..."
sys.exit(1)
blescan.hci_le_set_scan_parameters(sock)
blescan.hci_enable_le_scan(sock)
while True:
returnedList = blescan.parse_events(sock, 10)
print "----------"
I am trying to read the data from my Geiger Counter on my Beaglebone, but when I print the result, doesn't include my counter code:
import Adafruit_BBIO.UART as UART
import serial
import time
UART.setup("UART4")
ser = serial.Serial(port = "/dev/ttyO4", baudrate=9600)
r = 0
d = 0
z = 0
minutes = 0
while True:
timeout = time.time() + 60
while True:
x = ser.read()
if ser.isOpen():
print "Serial is open!"
r = r +1
print r
print x
elif x is '0':
d=d+1
#print '.'
elif x is '1':
d=d+1
#print '.'
time.sleep(1)
z=z+d
print "CPM %f " % d
print "total %f" % z
print "minutes %f" % minutes
My output came out as:
Serial is open!
1
1
Serial is open!
2
1
Serial is open!
3
0
There is no break in your inner while loop, so it will loop infinitely. Assuming counter code means the print statements at the end of your code sample, they will never be reached.