File: server_and_service_registration

package info (click to toggle)
libnet-bluetooth-perl 0.40-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 164 kB
  • ctags: 15
  • sloc: perl: 131; makefile: 7
file content (55 lines) | stat: -rwxr-xr-x 2,041 bytes parent folder | download | duplicates (5)
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
use Net::Bluetooth;

	#### Create a RFCOMM server

	#### Create a new socket object, this is basically calling
	#### the systems socket() call and setting some variable.
	#### The argument can be either "RFCOMM" or "L2CAP".
	my $server_obj = Net::Bluetooth->newsocket("L2CAP");
	die "Socket could not be created!" unless(defined($server_obj));
	print "after socket\n";

	#### Bind to port 1
	if($server_obj->bind(5) != 0) {
		#### Could try another port instead of exiting.
		die "bind error: $!\n";
	}
                                                                                                                   
	print "after bind\n";
	#### Listen with a backlog of 2
	if($server_obj->listen(2) != 0) {
		die "listen error: $!";
	}
	print "after listen\n";
                                                                                                                   
	#### Register a service
	#### $server_obj must be a open and bound socket
	#### The second option is the service ID.
	#### The third option is the service name.
	#### The fourth option is the service description.
	my $service_obj = Net::Bluetooth->newservice($server_obj, "1101", "GPS", "GPS");
	print "new service\n";
	unless(defined($service_obj)) {
		die "Could not register service!";
	}
                                                                                                                   
	#### accept a client connection
	$client_obj = $server_obj->accept();
	unless(defined($client_obj)) {
		die "client accept failed: $!";
	}
                                                                                                                   
	#### Create a Perl filehandle for reading and writing
	#### The filehandle should work with any Perl call that
	#### does not use the sockaddr struct.
	*CLIENT = $client_obj->perlfh();
	foreach(1 .. 1000) {
		print CLIENT "stuff";
	}

	#### close client connection
	close(CLIENT);
	#### stop advertising service
	$service_obj->stopservice();
	#### close server connection
	$server_obj->close();