www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to send data from php server to java client with sockets

Hybrid View

  1. #1
    Join Date
    Jul 2011
    Posts
    2

    How to send data from php server to java client with sockets

    I have this code in java client:

    Code:
    Socket s = new Socket("someip",1235);
    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
    bw.write("Hello Java\n");
    System.out.println("Write succesful");
    
    String line = "";
    while ((line = br.readLine()) != null){
           System.out.println(line);
    }
    
    bw.close();
    br.close();
    s.close();
    and this code in the php server

    Code:
    $host = "someip";
    $port = 1235;
    
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
    $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
    $result = socket_listen($socket, SOMAXCONN) or die("Could not set up socket listener\n");
    $spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
    $input = socket_read($spawn, 10000, PHP_NORMAL_READ) or die("Could not read input\n");
    echo $input;
    $output = "Hello PHP";
    socket_write($spawn,$output."\n", strlen($output) + 1) or die("Could not write output\n");
    
    socket_close($spawn);
    socket_close($socket);
    When i put comments in these commands:

    Code:
    socket_write($spawn,$output."\n", strlen($output) + 1) or die("Could not write output\n");
    and

    Code:
    while ((line = br.readLine()) != null){
               System.out.println(line);
        }
    i have as a result an echo message "Hello Java" on my php server side. I can send data from java client to server. when i uncomment the above commands i have as a result the php server side (a browser) to load without ending. The same in the client side who is waiting for the data to come.

    The host is the same for both sides (my pc) and the ip is the IPv4 from ipconfig. I have spend many hours in this and i would appreciate any help available...

    The problem is: "When I send data from the Php socket to the Java socket, the PHP page appears to run indefinitely and doesn't echo anything and the Java of course doesn't terminate and doesn't output everything"

  2. #2
    Join Date
    Jun 2010
    Posts
    16
    The solution is that you need to flush the stream on the Java side. Flush the stream right below your 'Write succesful' line (see below):

    Code:
    System.out.println("Write succesful");
    bw.flush();
    That will do it.

  3. #3
    Join Date
    Jul 2011
    Posts
    2
    JUST PERFECT!!!! Thank you very much my friend....

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles