Home Backend Development PHP Tutorial PHP SCOKET sends large files, receiving end JAVA_PHP tutorial

PHP SCOKET sends large files, receiving end JAVA_PHP tutorial

Jul 13, 2016 pm 05:53 PM
fsockopen java php send big client take over document end

Client (PHP):

send.php

[php]
$fp = fsockopen("127.0.0.1", 1024, $errno, $errstr, 10);

$filename = '2012_07_23.zip'; //File to be sent

fwrite($fp, $filename . "rn"); //Write the file name. Use .readLine() on the java side. The first line is the file name

$handle = fopen($filename, "r");
 
$contents = fread($handle, filesize($filename));
//fwrite($fp,$contents); //Small files can be sent like this, but large files should be divided into segments
$data_size = 1024 * 1; //1M each time
$data_count = ceil( strlen($contents) / $data_size ); //How many pieces of data are there
for( $i = 0; $i < $data_count; $i ++ )
{
$data = substr( $contents, $i * $data_size, $data_size ); //Write to transmission socket
fwrite($fp,$data); www.2cto.com
}

fclose($fp);

?>

Server (JAVA):
MyApp.java

[java]
import java.io.*;
import java.net.*;
import java.util.Date;
import java.sql.*;

public class MyApp
{
private int x;
       
Public MyApp()
{
        x = 0;
}  
       
Public static void main(String args[]) {
int i = 1, port = 1024;
ServerSocket server=null;
Socket client = null;
         try{
               server=new ServerSocket(port);
System.out.println("Web Server is listening on port" + server.getLocalPort());
for(;;){
client=server.accept();
//Accept the client’s connection request
                        new WebThread(client,i).start();
i++;
                                                                                                                                                }catch(Exception e){System.out.println(e);}
}  
}

WebThread.java
[java]
import java.io.*;
import java.net.*;
import java.util.Date;

class WebThread extends Thread{
Socket socket;//Socket word to connect to the web browser
int counter;//Counter
Public WebThread(Socket cl,int c){
         socket=cl;
counter=c;
}  

Public void run()//Thread body
{

Try
                                                                    DataInputStream inputStream = null;
Try
                                                                                         inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
              }catch(Exception e)
                                                                                                                                                                                                                                                      try 
                                                                                      String savePath = "E:\";
int bufferSize = 8192;
                  byte[] buf = new byte[bufferSize];
                  String filename = new String();
                     filename = inputStream.readLine();
                  String saveFilePath = new String();
                      saveFilePath = savePath + "\" + filename;
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(saveFilePath))));

               System.out.println("The long name of the file: " + filename);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         int read = 0;
If (inputStream != null)
                                                                                                                  read = inputStream.read(buf);
                                                                                                                                                                         If (read == -1) {
break;
                                                                                                                                                                                                                                                  //System.out.println(buf.toString());
fileOut.write(buf, 0, read);
                                                                                                                                   System.out.println("Reception completed, file saved as" + saveFilePath + "n");
fileOut.flush();
fileOut.close();
                    inputStream.close();
              } catch (Exception e) {
System.out.println("Error in receiving message" + e.toString() + "n");
Return;
                                                                                                                                                }catch(Exception e){
         } 
}  
}

Author: junqing124

http://www.bkjia.com/PHPjc/478073.html

truehttp: //www.bkjia.com/PHPjc/478073.htmlTechArticleClient (PHP): send.php [php] ?php $fp = fsockopen(127.0.0.1, 1024 , $errno, $errstr, 10); $filename = 2012_07_23.zip; //The file to be sent fwrite($fp, $filename . rn); //Write the file...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

How to configure the character set and collation rules of MySQL How to configure the character set and collation rules of MySQL Apr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

How to rename a database in MySQL How to rename a database in MySQL Apr 29, 2025 pm 04:00 PM

Renaming a database in MySQL requires indirect methods. The steps are as follows: 1. Create a new database; 2. Use mysqldump to export the old database; 3. Import the data into the new database; 4. Delete the old database.

Composer's Purpose: Managing Project Dependencies in PHP Composer's Purpose: Managing Project Dependencies in PHP Apr 30, 2025 am 12:01 AM

We need Composer because it can effectively manage dependencies of PHP projects and avoid the hassle of version conflicts and manual library management. Composer declares dependencies through composer.json and uses composer.lock to ensure the version consistency, simplifying the dependency management process and improving project stability and development efficiency.

What are the advantages of using Java for web applications that need to run on different servers? What are the advantages of using Java for web applications that need to run on different servers? May 03, 2025 am 12:13 AM

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence? What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence? May 03, 2025 am 12:22 AM

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

See all articles