Bluetooth Programming in Python: Network Programming using RFCOMM - RFCOMM in the Real World (Page 5 of 5 )
The server will service only one client at a time. It is neither multithreaded nor multi-process based. Let us start. First comes the imports.
from bluetooth import *
Then comes the class that will contain the server functionalities. Its constructor will take the port number on which the server has to listen. It will also call the create_server function.
from bluetooth import *
class rfcomm_server:
def __init__(self,port):
self.port=port
self.create_server()
Next is the create_server method that will create a RFCOMM-based socket and make it listen on the passed port. It then calls the start_server method.
from bluetooth import *
class rfcomm_server:
def __init__(self,port):
self.port=port
self.create_server()
def create_server(self):
self.server_socket=
Bluetooth.BluetoothSocket(Bluetooth.RFCOMM)
self.server_socket.bind(("",self.port))
self.start_server()
Next is the start_server method. This method makes the server listen on the port and then starts the connection. After that it asks the user for the file and and then transfers it.
from bluetooth import *
class rfcomm_server:
def __init__(self,port):
self.port=port
self.create_server()
def create_server(self):
self.server_socket=
Bluetooth.BluetoothSocket(Bluetooth.RFCOMM)
self.server_socket.bind(("",self.port))
self.start_server()
def start_server(self):
while true:
self.server_socket.listen(3)
self.client_socket,address=server_socket.accept()
self.client_socket.send("Enter the file name")
file_name= self.client_socket.recv(2048)
if file_name is not None:
transfer_data=open(file_name,'r').readlines()
for data in transfer_data:
self.client_socket.send(data)
self.client_socket.send("Transfer complete")
Next, let us start the server. To do that we first need to check whether the main function is being executed. If it is being executed, then we create the instance of the server.
from bluetooth import *
class rfcomm_server:
def __init__(self,port):
self.port=port
self.create_server()
def create_server(self):
self.server_socket=
Bluetooth.BluetoothSocket(Bluetooth.RFCOMM)
self.server_socket.bind(("",self.port))
self.start_server()
def start_server(self):
while true:
self.server_socket.listen(3)
self.client_socket,address=server_socket.accept()
self.client_socket.send("Enter the file name")
file_name= self.client_socket.recv(2048)
if file_name is not None:
transfer_data=open(file_name,'r').readlines()
for data in transfer_data:
self.client_socket.send(data)
self.client_socket.send("Transfer complete")
if '__name__'=='__main__':
server=rfcomm_server(20)
That completes the application. It also brings us to the end of this discussion. The discussion until now has not touched upon the topic of service discovery. The next article will be about service discovery. Till then...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |