0

I am currently posting an XML string to a TCP socket on an external server.

<?xml version='1.0'?><Data MODULE='TEST' FUNCTION='ISAVAILABLE'/>

The TCP socket is then meant to respond with a 0 or a 1 depending on the message sent.

Here is the code I am working with:

<?php
//error_reporting(0);
set_time_limit(0);

if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Couldn't create socket: [$errorcode] $errormsg \n");
}

//echo "Socket created \n";

if(!socket_connect($sock , 'xxx.xxx.xxx.xxx' , 4010))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not connect: [$errorcode] $errormsg \n");
}

//echo "Connection established \n\r";

$message = "<?xml version='1.0'?><Data MODULE='TEST' FUNCTION='ISAVAILABLE'/>";

if(!socket_send( $sock , $message , strlen($message) , 0))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not send data: [$errorcode] $errormsg \n");
}

socket_sendto($sock, $message, strlen($message), 0, 'xxx.xxx.xxx.xxx', 4010);

//echo "Message send successfully \n\r";

if(socket_recv( $sock , $buf , 2048 , MSG_WAITALL ) === FALSE)
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not receive data: [$errorcode] $errormsg \n");
echo $buf  . "\r\n";
}   

echo  "<pre>" . $buf . "</pre>";
socket_close($sock);

The socket currently responds with a 0, but when I test the exact same method in C# it posts a 1.

My question is am I missing something or mis-using a function?

This is my first time using the socket functions so forgive my noobness when it comes to this.

Thanks

EggieT
  • 1
  • 1
  • That's not a WebSocket you are using but a plain TCP socket, see [Differences between TCP sockets and web sockets, one more time](http://stackoverflow.com/questions/16945345/differences-between-tcp-sockets-and-web-sockets-one-more-time). If the server expects a Websocket then you should not use a plain socket. – Steffen Ullrich Sep 17 '16 at 16:29
  • @SteffenUllrich Oh I see, apoloiges. I will alter my question to reflect your comments. – EggieT Sep 17 '16 at 16:32

0 Answers0