Php Program to Send a Message from Client(php program) to Server (java program)

What this program does?


1. Make Use of php Socket Programming including with IP address and port number of Server (java program).
2. First starts a server (server.java) which will be always running listening to a port 8080(Server.java)
3. Client (Client.php) sends a number (message) to the server (Sever.java).
4. Server receives this number or messages from Client.php which is run on your Apache server 

1. This is Client .php treated as Client which is run on your xampp or wamp server.

<?php

$address = '172.16.50.122'; // java server ip address

$port = 8080; // port number

$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));

socket_connect($socket, $address, $port);

$message = '<?xml version="1.0"?>';

$message .= '<root>';

$message .= '<attribute-value attribute="sample attribure">';

$message .= '<value>sample value</value>';

$message .= '</attribute-value>';

$message .= '<text>Sample text</text>';

$message .= '</root>';

echo $status = socket_sendto($socket, $message, strlen($message), 0, $address, $port);

if($status !== FALSE)

{

    $message = '';

    $next = '';

    echo $message .= $next;

}

else

{

    echo "Failed";

}

socket_close($socket);

?>


This is Java Server file(server.java ) which is run on your EDI eclipse whatever you want java Editor.
First we should to start server (server.java) then client (client.php) send the request .

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.ServerSocket;

import java.net.Socket;

public class TCPClient { 

     public static void main(String[] args) throws Exception {

         TCPClient myServ = new TCPClient();

           myServ.run();

        }

        public void run() throws Exception {

            ServerSocket mySS = new ServerSocket(8080);

            Socket SS_accept = mySS.accept();

            InputStreamReader mySR = new InputStreamReader(SS_accept.getInputStream());

            BufferedReader myBR = new BufferedReader(mySR);

            String temp = myBR.readLine();

            System.out.println(temp);

        }      

}

 

Comments