Jump to content

Help: Send bytes, receiving data with sockets


LRSSS

Recommended Posts

Hey everyone, I'm new to using sockets and have been trying on my own with no avail to send 4 bytes to a server program, then receive the server's response data for display.

 

Outgoing: 4 bytes, 0xFFFFFFFF

Incoming: 4 bytes output data, 12 bytes reserved

 

I'm not sure whether its my syntax or the way I'm handling the input/output but this is what I've tried so far:

 

With socket_create, freezes at: Sending $i

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "Error: " . socket_strerror(socket_last_error()) . "<br \>";
} else {
    echo "Created socket<br \>";
}

$result = socket_connect($socket, IP, PORT);
if ($result === false) {
    echo "Error: " . socket_strerror(socket_last_error($socket)) . "<br \>";
} else {
    echo "Connected to socket<br \>";
}

// Bytes to send
$i = chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF);

echo "Sending $i<br \>";
socket_write($socket, $i, 4);
    
$input = socket_read($socket, 16);
echo "Response: $input<br \>";

echo "Closing socket...";
socket_close($socket);
?>

 

Any help would be appreciated at this point as searching for a solution online for socket handling in php doesn't turn up revelant infomation on byte sending and receiving data like I'd like to do here.

Link to comment
Share on other sites

try:

 

echo "Sending $i<br \>";

socket_write($socket, $i, 4);

 

echo "Submitted $i; Receiving response...";

 

$input = socket_read($socket, 16);

echo "Response: $input<br \>";

 

Thanks for the suggestion! Right now the script hangs at:

 

Created socket

Connected to socket

Sending ÿÿÿÿ

 

If I stop the page loading it'll then display:

 

Created socket

Connected to socket

Sending ÿÿÿÿ

Submitted ÿÿÿÿ; Receiving response...

 

That's strange, it's as if I'm missing a little something on how to properly read the data being sent back?

Link to comment
Share on other sites

try:

 

echo "Sending $i<br \>";

socket_write($socket, $i, 4);

 

echo "Submitted $i; Receiving response...";

 

$input = socket_read($socket, 16);

echo "Response: $input<br \>";

 

Thanks for the suggestion! Right now the script hangs at:

 

Created socket

Connected to socket

Sending ÿÿÿÿ

 

If I stop the page loading it'll then display:

 

Created socket

Connected to socket

Sending ÿÿÿÿ

Submitted ÿÿÿÿ; Receiving response...

 

That's strange, it's as if I'm missing a little something on how to properly read the data being sent back?

 

Yup apparently there's something wrong with the reading

Link to comment
Share on other sites

@ignace, I was doing some tests to see if anything was being sent back from the server and nothing was being sent back apparently. In short, the server didn't get the right response packet form me I think? I may have narrowed this problem down a little more as I can get the socket to send successfully and attempt to recieve, but in return nothing is outputted.

 

Created socket

Connected to socket

Sending ÿÿÿÿ, 4 bytes

Submitted ÿÿÿÿ; Receiving response...

Response:

Closing socket...

 

I'm guessing that I didn't structure the packet being sent correctly, thus nothing is being read back. Can I get help on how to  structure to following packet to write to the socket?:

 

4 bytes -> 0xFFFFFFFF

 

chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF) feels like it's wrong and I have a feel I may have to use pack() although I'm not familiar with the formats. Anyone with byte packets knowledge can help me out in turning the above into a packet I can send?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.