ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to have a client socket that connects to a server,and will always listen for incoming commands from the server,BUT it needs to check at the same time if it has something to send to the server.This is done by looking at a MySQL Queue table i populate with commands i need to send the server.
Is this possible in PHP,i.e
thread 1 => Listen for and handle incoming commands
thread 2 => Check for and handle outgoing commands.
If i receive commands while i'm busy sending,my script must be able to process incoming commands as well.
The reason i want to use PHP,is because a lot of the commands is MySQL dependant,i.e there is a lot of queries,selects,etc,and i would really prefer to use PHP for this rather than C or Java.
If this is not possible in PHP,what language should i rather use?Maybe Perl would be easier,but PHP would be my first option?
Don't know about PHP, but Perl can definitely do thrs and MySQL etc. Done it myself... Just use the pre-written CPAN modules here: http://search.cpan.org/
Thanks for your reply.I've got the MySQL part and the Socket part working,but i'm not sure what i need to do for multithreading.Can you maybe direct me to a small example where this is done.Here is my simple client,without multi threading enabled.I marked where i'd like the two threads to run.
Code:
use DBI;
use IO::Socket::INET;
# Create a new socket
$MySocket=new IO::Socket::INET->new(PeerPort=>1234,Proto=>'tcp',PeerAddr=>'192.168.100.58');
$db = DBI->connect("dbi:mysql:database:localhost","root","");
# loop for ever
while (1) {
# loop every 1 second
sleep(1);
#Thread 1 Check if there is messages to send to server socket
$sql = "select * from queuetable";
$sql = $db->prepare($sql);
$sql->execute;
while(@data = $sql->fetchrow_array) {
$message = $data[0];
$MySocket->send($message);
}
#Thread 2 Check for incoming messages from server and display it
$MySocket->recv($received,1024);
print "$received";
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.