LRSSS Posted August 26, 2009 Share Posted August 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/171905-help-send-bytes-receiving-data-with-sockets/ Share on other sites More sharing options...
ignace Posted August 26, 2009 Share Posted August 26, 2009 try: echo "Sending $i<br \>"; socket_write($socket, $i, 4); echo "Submitted $i; Receiving response..."; $input = socket_read($socket, 16); echo "Response: $input<br \>"; Quote Link to comment https://forums.phpfreaks.com/topic/171905-help-send-bytes-receiving-data-with-sockets/#findComment-906513 Share on other sites More sharing options...
LRSSS Posted August 26, 2009 Author Share Posted August 26, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/171905-help-send-bytes-receiving-data-with-sockets/#findComment-906951 Share on other sites More sharing options...
ignace Posted August 26, 2009 Share Posted August 26, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/171905-help-send-bytes-receiving-data-with-sockets/#findComment-907027 Share on other sites More sharing options...
LRSSS Posted August 26, 2009 Author Share Posted August 26, 2009 @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? Quote Link to comment https://forums.phpfreaks.com/topic/171905-help-send-bytes-receiving-data-with-sockets/#findComment-907036 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 What you send doesn't actually matter (all the socket knows it's a 4-byte string) it breaks it down into bits and sends it through the wire. I found this: http://www.devshed.com/c/a/PHP/Socket-Programming-With-PHP/ Seems like you are missing a few pieces of the puzzle. Quote Link to comment https://forums.phpfreaks.com/topic/171905-help-send-bytes-receiving-data-with-sockets/#findComment-907361 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.