0

I want to transfer socket file for java server from php client.

Java from java is good job.

But php can not transfer file to java server.

I need to convert to php code from java code or I would like to know to transfer socket file for java server from php client by other method. Please help me... Thank you!!!

  1. Java Client Code.

    File file = new File(fileName);
    Socket socket = new Socket("localhost", 20225);
    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    
    oos.writeObject(file.getName());
    
    FileInputStream fis = new FileInputStream(file);
    byte [] buffer = new byte[100];
    Integer bytesRead = 0;
    
    while ((bytesRead = fis.read(buffer)) > 0) {
        oos.writeObject(bytesRead);
        oos.writeObject(Arrays.copyOf(buffer, buffer.length));
    }
    
    oos.close();
    ois.close();
    
  2. Java Server Code

    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    FileOutputStream fos = null;
    byte [] buffer = new byte[BUFFER_SIZE];
    String filename = null;
    Map<String, Object> retMap = new HashMap<String, Object>();
    
    // 1. Read file name.
    Object obj = ois.readObject();
    
    try {
    
        filename = Config.getInstance().getProperties("dispatcher.policy") + obj;
        fos = new FileOutputStream(new File(filename));   
    
        // 2. Read file to the end.
        Integer bytesRead = 0;
        do {
            obj = ois.readObject();
    
            bytesRead = (Integer)obj;
            obj = ois.readObject();
            buffer = (byte[])obj;
    
            // 3. Write data to output file.
            fos.write(buffer, 0, bytesRead);
    
        } while (bytesRead == BUFFER_SIZE);
    
        logger.info("File transfer success : {} ", filename );
    
    
        fos.close();
    
    }catch(Exception e){
    
    }
    oos.writeObject("OK");
    ois.close();
    oos.close();
    

1 Answer 1

1
$fp = stream_socket_client("tcp://localhost:20225", $error_number, $error_string);
if ( !$fp ) {
    # output what went wrong with a new line at the end
    echo "$error_number ($error_string)\n";
} else {
    fwrite($fp, file_get_contents(FILE_NAME));
}
# uncomment next line if you happen to have more code below
# fclose($fp); # does what it says - closes the resource

stream_socket_client() returns a stream resource or false if failed to do so. It could take three more parameters: timeout, flags and context - consult the documentation for further information if you need to change the default.

I've put tcp:// as socket transport, which is by the way the default value. I've put that as a hint that you may use other transports, most likely from the list of Internet Domain: TCP, UDP, SSL, and TLS or the other list of Unix Domain: Unix and UDG. The latter is a much faster way when using localhost, but you need an OS that supports that and (probably and if possible at all in java) the proper code for the Java server part.

fwrite() is a binary-safe write - it simply writes the binary data passed as a second parameter to the resource passed as the first parameter.

file_get_contents() does exactly what its name says in a binary-safe way, too. The first parameter is the path to the file. It could take a few more parameters - consult the documentation if the defaults doesn't seem to fit.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.