0

I have developed a socket server using C# and a client in PHP which connects fine .. i just need to send some data from the client to the server.

I developed the PHP socket client as according to this Past Stackoverflow Question

<?php
$host="127.0.0.1" ;
$port=9875;
$timeout=30;
$sk=fsockopen($host,$port,$errnum,$errstr,$timeout) ;
if (!is_resource($sk)) {
    exit("connection fail: ".$errnum." ".$errstr) ;
} else {

    echo "Connected";
    }
?>

Finally What i required is to send a data (byte array) to the socket server using this PHP client

3 Answers 3

3

fwrite(), see as well the manual page for fsockopen() for examples.

$bytesWritten = fwrite($sk, $string);

If you have an array of bytes, convert it to string before writing:

$string = imlode('', $byteArray);
Sign up to request clarification or add additional context in comments.

Comments

0

From the PHP Documentation:

fwrite($sk, 'A message sent to the server');

Or with arrays :

$array = array(4, '3', 'Foo');
fwrite($sk, serialize($array)); //You'll have to deserialize it on C# side.

Comments

0
$msg = "Your message here";

fwrite($sk, $msg);
// Only if you expect some response
while (!feof($sk)) {
    echo fgets($sk, 128);
}
// Close the stream
fclose($sk);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.