LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2007, 01:09 AM   #1
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 188

Rep: Reputation: 30
PHP - Multithreaded Sockets


Hi,

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?

Any suggestions?
 
Old 11-19-2007, 01:14 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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/
 
Old 11-19-2007, 02:24 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi

PHP doesn't have threads, but it can fork. See http://www.php.net/manual/en/function.pcntl-fork.php

Note that after a fork, you get a new process, not a thread, so any communication between the processes has to be coded by yourself.
 
Old 11-19-2007, 02:26 AM   #4
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 188

Original Poster
Rep: Reputation: 30
Hi,

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";
}
 
Old 11-19-2007, 11:06 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Start by reading this: http://perldoc.perl.org/perlthrtut.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP - can't get to sockets to communicate ivanatora Programming 3 11-08-2007 06:54 AM
Sockets In PHP baddah Programming 4 10-17-2007 08:35 AM
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-27-2007 09:49 AM
wget multithreaded AlexiaKeth Linux - Software 1 06-25-2006 04:03 PM
PHP + Sockets Problem aspr Programming 0 06-04-2003 03:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration