Page 1 of 1

writing a client to a socket

Posted: Sat Jan 03, 2009 12:22 am
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

Re: writing a client to a socket

Posted: Sat Jan 03, 2009 6:41 pm
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";
        }
    }
 

Re: writing a client to a socket

Posted: Mon Jan 05, 2009 8:27 am
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