Jump to content

file transfer through socket connection


bozebo

Recommended Posts

Hi, I have been playing around with PHP desktop applications and I am curious of it's potential. I am attmpting to transfer a small file (probably no more than 15 kB) through sockets from a client to a server, both programmed in PHP.

 

I think my bug lies somewhere in the client's handling of the file before it sends it off to the server but I am unsure. I will post the code now and discuss it below:

 

<?php
/* SERVER */
$address = "127.0.0.1";
$port = "10000";

$mysock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

socket_bind($mysock,$address,$port);

socket_listen($mysock,5);

echo "Waiting for a client...\n";

$client = socket_accept($mysock);

echo "Server started\n";

$file = ''; // <-- this is 2 apostrophies by the way
sleep(2);
$input = socket_read($client,2048);
while(!feof($input)){
  $file .= $input;
  $input = socket_read($client,2048);
}

file_put_contents('destination.bmp',$file);

socket_close($client);

socket_close($mysock);

echo "Server ending...\n";
?> 

<?php
/* CLIENT */
$address = "127.0.0.1";
$port = 10000;

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
    echo "socket successfully created.\n";
}

echo "Attempting to connect to '$address' on port '$port'...";
$result = socket_connect($socket, $address, $port);
if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
    echo "successfully connected to $address.\n";
}

$file = file_get_contents('source.bmp');

socket_write($socket,$file);

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

 

I have written some multi-client socket servers before and even run a small (and buggy) orpg, but never sent a file. To simplify things I took code from a basic talkback server I found on php.net and made my alterations. Basically, I run the server and then the client afterwards, it seems to connect fine, but when the client sends the file I get an error in the server window:

"feof() supplied argument is not a valid stream resource"

so, is this error due to how the client sends the file? or how the server attemts to receive it.. or possibly both. I have no idea.

 

Also: I have spent a while googling for some tutorial or example of this in PHP but couldn't find any (kept getting file upload ones ^_^). Probably because there is no particular reason to do this in PHP...

 

Thanks in advance for any help I get.

Link to comment
https://forums.phpfreaks.com/topic/99814-file-transfer-through-socket-connection/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.