Hello
i have a problem so i thought maybe you can help me :)
I'm working on a project which is in c, its a bluetooth server that sends and receives a string type message. its using BlueZ library ( compile against -lbluetooth). so there's two functions, bt_send() and bt_recv(), there's also another function called logger(), that creates a log file for messages that are being sent and received.
i already have coded this bluetooth server and its functioning.
i wanted to add a GUI for this , so i got a free web server from this web site wwwdotpaulgriffithsdotnet; The Internet Home of Paul Griffiths) and modified so i could use my own bluetooth server with it.
the modification includes, that from the HTML page that is using a <FORM> element and GET method to send a string to the web server:
GET /send.htm?Text=START HTTP/1.1
the "START" part can differ, it could be START or STOP. but the buffer always looks like this. when the web server receives this buffer, i managed to modified so it can now analyse the buffer and parse the "START" part from the buffer and send a bluetooth message using bt_send() accordingly.
everything on this point is working flawlessly. i compile the code like this:
gcc -http.c server.c -o sim -lbluetooth
http.c so the webserver and server.c for bluetooth sever functionality.
o run it in the terminal as: ./sim
and the terminal goes to the process of hosting the web pages; i simply open my browser and write this in the address bar:
localhost:8080/index.html
when i click on the "send" button the http server prints the command (START or STOP) and the message is sent to the other device ( using the defined MAC address) via my bluetooth server.
now my problem and what i want to achieve is to have the bluetooth receive part at same time of web server hosting pages, i think i need to use threads(pthread) , I've never worked with threads.
that means two process are running at the same time;
one for web server (http.c) for hosting web pages,
and one for bluetooth server receive function to receive incoming messages at any time (bt_recv() in server.c).
the bt_recv() function in server.c is an endless while loop that is always creating a bluetooth socket and makes it listen on a port for any incoming message.
the output that i have in my head is like this:
i run the output file like:
./btserver
i go to the url and i can send perfectly and the http server prints the message and logs it,
and if any device like another computer sends a message from a bluetooth function (like my bt_send() ) , the server would run the bt_recv() function as a thread, receives the message, print the message and logs it.
i would really appriciate your help, and thanking you in advance,
i could send you my source code but how would i?
thanks
Ali.B
Help on bluetooth and threading
Started by alibahaloo, Aug 10 2009 03:00 PM
11 replies to this topic
#1
Posted 10 August 2009 - 03:00 PM
|
|
|
#2
Posted 10 August 2009 - 04:23 PM
Attach your source code. Click "Go Advanced" at the bottom, then click "Manage Attachments" somewhere around there... For reasons beyond my understanding you can't attach .h files. Just change the extension to .txt or something. So I take it you need help with the pthread library? Here's a simple example:
Linux Tutorial: POSIX Threads
What operating system is this for, anyway?
POSIX pthread Reference
Linux Tutorial: POSIX Threads
What operating system is this for, anyway?
POSIX pthread Reference
Edited by dargueta, 10 August 2009 - 04:24 PM.
Added link
sudo rm -rf /
#3
Posted 10 August 2009 - 04:31 PM
im using linux - ubuntu
compiler : gcc
i will attach my source code. .c files are as they are... .h files are as .txt files
i've read those tutorials but no use, it kinda too late to start from begining. i need to submit these project very soon, and I've already done more than minimum requirements, i just wanna do this for learning and getting a better grade, so something like an example or sample code would be really nice,
thanks
compiler : gcc
i will attach my source code. .c files are as they are... .h files are as .txt files
i've read those tutorials but no use, it kinda too late to start from begining. i need to submit these project very soon, and I've already done more than minimum requirements, i just wanna do this for learning and getting a better grade, so something like an example or sample code would be really nice,
thanks
Attached Files
#4
Posted 10 August 2009 - 04:33 PM
So where exactly do you need the pthreads? I see you use fork() just fine.
sudo rm -rf /
#5
Posted 10 August 2009 - 04:39 PM
dargueta said:
So where exactly do you need the pthreads? I see you use fork() just fine.
fork() , but where would you use it?
#6
Posted 10 August 2009 - 04:44 PM
#7
Posted 10 August 2009 - 04:45 PM
dargueta said:
It's in your code already. :)
could u paste a piece of code and show me how to call the bt_recv() function in a fork?
the fork is used in the http server, but how would the bluetooth server be running on a fork?
Edited by alibahaloo, 11 August 2009 - 04:43 AM.
#8
Posted 11 August 2009 - 05:43 AM
anyone?
#9
Posted 12 August 2009 - 01:52 AM
..........?
#10
Posted 12 August 2009 - 04:57 AM
#11
Posted 12 August 2009 - 05:51 AM
thanks :)
#12
Posted 12 August 2009 - 06:10 PM
Um...it looks like you're using _beginthread() in the right place already. I'm pretty sure that's a non-standard Microsoft extension, but you've got the right idea. However, you've got a problem with your current code: you need to keep track of what threads/processes (depends on whether you use fork() or pthreads) are still running and which ones aren't; otherwise you could be leaving zombies if you exit before they're done. I've written an HTTP server before, but I'm not entirely sure in this case whether you'd want to use fork() or a pthread, seeing as you're not really doing much complicated processing. There are advantages and disadvantages to each:
FORKS
Pros: If one client goes haywire, you won't take out the entire server, and other clients can still connect and get their data. You also don't have to deal with concurrency, as all data is copied.
Cons: Lots of overhead in forking. All data is duplicated, as are all open file handles, pipes, etc. You need to close the ones you don't use, or you could eat up system resources really quickly. Communication between forked children is possible, but a chore.
THREADS
Pros: Faster, gets the same job done as forking. Communication between threads is relatively easy, as they share all the same data.
Cons: You need to be careful with concurrency. Data and file handles are not duplicated, so you need to make sure that your threads play nice and share properly, or else you'll get corrupted data and very unhappy clients.
Either way, you need to keep track of your child processes/threads. Your current implementation isn't really geared for that.
FORKS
Pros: If one client goes haywire, you won't take out the entire server, and other clients can still connect and get their data. You also don't have to deal with concurrency, as all data is copied.
Cons: Lots of overhead in forking. All data is duplicated, as are all open file handles, pipes, etc. You need to close the ones you don't use, or you could eat up system resources really quickly. Communication between forked children is possible, but a chore.
THREADS
Pros: Faster, gets the same job done as forking. Communication between threads is relatively easy, as they share all the same data.
Cons: You need to be careful with concurrency. Data and file handles are not duplicated, so you need to make sure that your threads play nice and share properly, or else you'll get corrupted data and very unhappy clients.
Either way, you need to keep track of your child processes/threads. Your current implementation isn't really geared for that.
Edited by dargueta, 12 August 2009 - 06:11 PM.
Reformatted for easier reading
sudo rm -rf /


Sign In
Create Account

Back to top











