Java TCP socket server with PHP client? -


hey there,

i developing tcp socket server in java. client(s) must connect webpage (in php)

well, here come's trouble.. can connect specified host, server can't read packets client send.

if create client in java, works 100%. well, here snippets of code. hope have answer me. because i'm stuck.

this little php script sends:

<?php  set_time_limit(0);  $port = 1337; //the port on connecting "remote" machine $host = "localhost"; //the ip of remote machine (in case it's same machine) $sock = socket_create(af_inet, sock_stream, 0) //creating tcp socket      or die("error: not create socket\n");  $succ = socket_connect($sock, $host, $port) //connecting to server using socket     or die("error: not connect host\n");   $text = "wouter123"; //the text want send server   socket_sendto($sock, $text, strlen($message), msg_eof, '127.0.0.1', '1337'); //socket_write($sock, $text . "\n", strlen($text) + 1) //writing text socket  //      or die("error: failed write socket\n");     $reply = socket_read($sock, 10000, php_normal_read) //reading reply socket    or die("error: failed read socket\n");   echo $reply; ?> 

the socket side is:

package com.sandbox.communication;  public class packethandler {  public string processinput(string theinput) {     string theoutput = null;      if (theinput == "wouter123") {         theoutput = theinput;     }else {         theoutput = "cannot find packet. output packet " + theinput;     }      return theoutput; } 

}

and little code connects packethandler: packethandler ph = new packethandler();

        while ((inputline = in.readline()) != null)         {              outputline = ph.processinput(inputline);              out.println(outputline);         } 

as using readline on input stream make sure clients sending data linefeed.

from javadocs

readline() reads line of text. line considered terminated 1 of line feed ('\n'), carriage return ('\r'), or carriage return followed linefeed.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -