writing a client to a socket

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

writing a client to a socket

Post by susrisha »

Hello,
I have a TCP server which runs on a certain port number.
Its a test type of server which just returns the what is sent from the client.
I want to write a client to the same program. I have the following code.

Code: Select all

 
$message = "hello all ";//$argv[1];
$remote_socket = "192.168.1.103:7976";
$stream = stream_socket_client($remote_socket,$errno,$errstr,30);
if(!$stream)
             {
               //echo " $errstr ($errorno)";
               echo "The message could not be sent to the server.. Internal server problem\n".$errstr." ".$errorno;
 
             }
             else
             {
 
                 fwrite($stream,$message );
                 while(!feof($stream))
                 {
                  echo fgets($stream, 1024);
                 }
 
             }
              fclose($stream);
 
That doesnt seem to work or get the information that is returned from the server. Can this problem be solved?
What is wrong with this code? Please help me out
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: writing a client to a socket

Post by it2051229 »

I tried this before, i'll post my PHP code... I used a C# application that listens to some port and waiting for PHP to connect...

Code: Select all

 
    if(isset($_POST["sendMessage"]))
    {
        /*
        * We're going to create a socket for IPv4 which can stream
        * data in TCP or UDP.. but for this experiment, it would be TCP
        */
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        
        /*
        * Next to do is to connect this socket to the C# application
        * who is listening at port 5000.
        */
        if(socket_connect($socket, "localhost", 5000))
        {
            /*
            * Then we're going to send the data to C#
            * and IF it happens that C# will accept the data, it will display it in a message dialog box.
            */
            socket_write($socket, $_POST["message"]);
            
            echo "DATA SENT. OH <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>, IT WORKED.";
        }
        else
        {
            echo "SORRY MATE I TOLD YOU THIS EXPERIMENT WILL NOT WORK";
        }
    }
 
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: writing a client to a socket

Post by susrisha »

Thanks mate.. I too had a c# program that listens on a particular port and sent the information across. This one i am writing for a php socket server. Any how i solved the issue, i forgot to put the socket_write func in the main server program. My bad
Post Reply