From: Marco Trudel <mtrudel@gmx.ch>
To: bluez-users@lists.sourceforge.net
Subject: Re: [Bluez-users] rfcomm question 3
Date: Sat, 05 Feb 2005 14:17:49 +0100 [thread overview]
Message-ID: <4204C77D.1040207@gmx.ch> (raw)
In-Reply-To: <20050205124828.1758.qmail@web60903.mail.yahoo.com>
Hello Michael
Maybe this helps:
---- start code snippet ----
// handle the includes yourself
#define CHANNEL 4
#define QUEUE 10
int main()
{
int sock, client;
socklen_t alen;
struct sockaddr_rc addr;
addr.rc_family = AF_BLUETOOTH;
bacpy(&addr.rc_bdaddr, BDADDR_ANY);
addr.rc_channel = htobs(CHANNEL);
alen = sizeof(addr);
// request a socket from the operating system
if((sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0)
{
printf("socket(...) failed");
exit(1);
}
// bind the socket to the system
if(bind(sock, (struct sockaddr *)&addr, alen) < 0)
{
printf("bind(...) failed\n");
exit(2);
}
// set the socket into listen mode. handle 10 (QUEUE) connections.
if(listen(sock, QUEUE) < 0)
{
printf("listen(...) failed\n");
exit(3);
}
// wait for the next connection or serve the next in the queue
while(client = accept(sock, (struct sockaddr *)&addr, &alen))
{
char remoteAddr[18];
ba2str(&addr.rc_bdaddr, remoteAddr);
printf("connection from %s:\n", remoteAddr);
// send a "hello from server" to client
char out[] = "hello from server";
send(client, out, strlen(out), 0);
printf("> %s\n", out);
// read the answer
char buffer[101]; // buffer size 100, 1 for '\0'
int noReadChars = recv(client, buffer, sizeof(buffer)-1, 0);
buffer[noReadChars] = '\0'; // here the received answer ends
printf("< %s\n", buffer);
// now close the connection
close(client);
}
close(sock);
return 0;
}
---- code snippet end ----
If you start this sample the machine listens on rfcomm channel 4 for
incoming connections. you can now connect with a mobile phone.
If you now search for "c socket programming" on google, you'll be able to
fully understand this code snippet and be able to change it to do what you
want.
regards
Marco
Ka Kin Cheung wrote:
> Hi all users!
> Sorry, but I have to say that I have knowledge about C, but I don't
> have so much knowledge about socket programming. And I would like to
> point out that what I find hard is just to let my main program to listen
> to what bluetooth mobile device wishes to connect to PC, and then let
> the program run even though no bluetooth device is connecting with the
> PC i.e. let PC to be server. Anyway, thank you very much for all of your
> advices. Sorry for any misunderstanding.
> By the way, C is the main programming language of my project, so it
> is not possible for me to use one language more. But I thank Marco for
> advice.
> And I wish to ask question here. I've heard that rfcom_bind can do
> something similar those things do in rctest.c, but until now, I can't
> find any detail about rfcomm_bind, even in this mail-list or in web. So,
> if anyone knows, please kindly inform me here and I'll use it once I
> understand what it does. Sorry for impolite reply but I am eager to
> learn more about this. Thank you very much!
> Michael
> *//*
>
> Hello Ka Kin Cheung
>
> It looks like you haven't that much c or socket programming
> knowledge. So
> if you try to learn c, socket programming, compiling, bluetooth
> programming
> and all relatet topics in one step, that's pretty hard.
>
> What about Java? JSR 82?
> It's fully documentated and you find a lot of samples if you ask
> google...
>
> Take a look at this:
> http://sourceforge.net/projects/avetanabt/
> (It is available for windows as well.)
>
> And a good general programming rule (specially for c/c++):
> Do not use gcc if you haven't already understand what your code's
> supposed
> to do.
>
>
> regards
> Marco
>
>
> Ka Kin Cheung wrote:
> > Hi all,
> > I've added -lbluetooth in the command, and it successes. But when I
> > run the program, the program terminates and doesn't wait for any
> mobile
> > terminal involving into the program. What is the problem about my
> > program? And need I really link the header files in
> bluez-libs/include?
> > If so, should I include like this "bluez-libs-2.14/include/xx.h"?
> But
> > the /usr/include contains the files. So I wish to know why if it
> is so.
> > Here is the syntax of my program.
> > int main (int argc, char **argv)
> > {
> > ...
> > int no_connection;
> > ...
> > printf("\E[H\E[J");
> > printf ("Bluetooth infrared converter\n\n");
> > printf ("Doing initializations !\n");
> > no_connection=0;
> > do_listen(recv_mode);
> > ...
> > while (1)
> > {
> > no_connection++;
> > printf("Now %d mobile terminal(s) is(are) using this program\n");
> > sprintf (command, "at+cscs=i\"8859-1\"\r");
> > ret_code = put_command (command, answer, sizeof (answer), 5, 0);
> > sprintf (command, "AT*EAM=\"Bt-IrConverter\"\r");
> > ret_code = put_command (command, answer, sizeof (answer), 5, 0);
> > submenu_selected = 0;
> > while (!submenu_selected)
> > {
> > ....
> > Thanks very much.
> > Michael
> >
> > */Alain Volmat /* wrote:
> >
> > Hi Michael,
> >
> > ba2str is a function provided by bluez-libs. Linking your
> > program against the bluez-lib might solve your problem.
> >
> > add -lbluetooth to your command line.
> >
> > Alain
> >
> > * Ka Kin Cheung [Thu, 3 Feb 2005 at 18:50 +0800]
> >
> > > Hi all users!
> > > I first give a thank to Alain Volmat that points
> > > out my fault and confusion about rctest.c. Then I put
> > > the parts do_listen and recv_mode in my main porgram.
> > > But when I compile, strange things occur.....
> > > As my program is MySQL C API, I have to compile
> > > with "gcc -c -I/usr/include/mysql XX.c", and it is no
> > > problem. But when I type "gcc -o XX XX.o
> & gt; > -L/usr/lib/mysql -lmysqlclient", following occurs:
> > > bic_midterm.o(.text+0x1c0a): In function `do_listen':
> > > : undefined reference to `ba2str'
> > > collect2: ld returned 1 exit status
> > > I checked with rctest.c, but there is not a function
> > > for ba2str. On the other hand, tv2fl can be accepted
> > > as there is a function in rctest.c. Here are the
> > > include files in my main program.
> > > #ifdef HAVE_CONFIG_H
> > > #include
> > > #endif
> > >
> > >
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > >
> > >
> > > #include
> > > include
> > > #include
> > > ; #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > >
> > > #include
> > >
> > >
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > > #include
> > >
> > >
> > > #include
> > > Let's see if there is any function that is missing. If
> > > there is no function missing, where can I find the
> > > ba2str function? I believe that if ba2str function can
> > > be found declared, the program can be run. Thank you
> > > very much for your kindness.
> > > Michael
> > >
> > >
> >
> _______________________________________________________________________
> > > 新年願望第一位 : 找到友緣人
> > > http://personals.yahoo.com.hk
> > >
> > >
> > > -------------------------------------------------------
> > > This SF.Net email is sponsored by: IntelliVIEW -- Interactive
> > Reporting
> > > Tool for open source databases. Create drag-&-drop reports.
> Save time
> > > by over 75%! Publish reports on the web. Export to DOC, XLS, RTF,
> > etc.
> > > Download a FREE copy at http://www.intelliview.com/go/osdn_nl
> > > _______________________________________________
> > > Bluez-users mailing list
> > > Bluez-users@lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/bluez-users
> > >
> >
> >
> >
> > -------------------------------------------------------
> > This SF.Net email is sponsored by: IntelliVIEW -- Interactive
> Reporting
> > Tool for open source databases. Create drag-&-drop reports. Save time
> > by over 75%! Publish reports on the web. Export to DOC, XLS, RTF,
> etc.
> > Download a FREE c opy at http://www.intelliview.com/go/osdn_nl
> > _______________________________________________
> > Bluez-users mailing list
> > Bluez-users@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/bluez-users
> >
> >
> >
> ------------------------------------------------------------------------
> > 新年願望第一位 : 找到友緣人
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
> Tool for open source databases. Create drag-&-drop reports. Save time
> by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
> Download a FREE copy at http://www.intelliview.com/go/osdn_nl
> _______________________________________________
> Bluez-users mailing list
> Bluez-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/bluez-users
>
>
> ------------------------------------------------------------------------
> 新年願望第一位 : 找到友緣人 <http://hk.personals.yahoo.com>
-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl
_______________________________________________
Bluez-users mailing list
Bluez-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-users
next prev parent reply other threads:[~2005-02-05 13:17 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-01-28 14:06 [Bluez-users] rfcomm question 1 Ka Kin Cheung
2005-01-28 17:36 ` Marcel Holtmann
2005-01-29 5:32 ` Ka Kin Cheung
2005-01-29 14:03 ` Marcel Holtmann
2005-01-31 13:41 ` Ka Kin Cheung
2005-01-31 14:22 ` Marcel Holtmann
2005-01-31 14:30 ` Ka Kin Cheung
2005-01-31 14:38 ` Marcel Holtmann
2005-02-01 6:25 ` [Bluez-users] rfcomm question 3 Ka Kin Cheung
2005-02-01 8:14 ` Marcel Holtmann
2005-02-01 14:39 ` Ka Kin Cheung
2005-02-02 2:49 ` Ka Kin Cheung
2005-02-02 3:10 ` Marcel Holtmann
2005-02-02 4:54 ` Ka Kin Cheung
2005-02-02 5:34 ` Marcel Holtmann
2005-02-03 3:42 ` Ka Kin Cheung
2005-02-03 5:22 ` Alain Volmat
2005-02-03 10:50 ` Ka Kin Cheung
2005-02-03 10:59 ` Alain Volmat
2005-02-03 13:01 ` Ka Kin Cheung
2005-02-03 13:20 ` Marcel Holtmann
2005-02-03 14:26 ` Ka Kin Cheung
2005-02-03 14:54 ` Marcel Holtmann
2005-02-04 16:56 ` Marco Trudel
2005-02-05 12:48 ` Ka Kin Cheung
2005-02-05 13:17 ` Marco Trudel [this message]
2005-02-05 13:51 ` Marcel Holtmann
2005-02-05 14:00 ` Marco Trudel
2005-02-05 14:14 ` Marcel Holtmann
2005-02-05 17:39 ` Marco Trudel
2005-02-05 18:08 ` Marcel Holtmann
2005-02-17 5:37 ` Ka Kin Cheung
2005-02-17 6:28 ` Alain Volmat
2005-02-17 9:08 ` mike
2005-02-17 13:08 ` Marco Trudel
2005-02-17 13:54 ` Ka Kin Cheung
2005-02-17 14:10 ` Marco Trudel
2005-02-17 14:43 ` Ka Kin Cheung
2005-02-17 17:04 ` Marco Trudel
2005-02-18 3:28 ` Ka Kin Cheung
2005-02-19 6:49 ` Marco Trudel
2005-02-19 8:27 ` Ka Kin Cheung
2005-02-19 10:06 ` Marco Trudel
2005-02-19 13:45 ` Ka Kin Cheung
2005-02-18 3:33 ` Ka Kin Cheung
2005-02-18 7:51 ` [Bluez-users] multiple instances of OPUSH server Janice M. Ballesteros
2005-02-17 13:14 ` [Bluez-users] rfcomm question 3 Marco Trudel
2005-02-02 3:16 ` Alain Volmat
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4204C77D.1040207@gmx.ch \
--to=mtrudel@gmx.ch \
--cc=bluez-users@lists.sourceforge.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.