Atrociouss Posted November 29, 2012 Share Posted November 29, 2012 Note: This is mainly a PHP issue, but the java part is a Bukkit plugin. My TCP Server (made in Java) is one that can send and recieve messages/commands from a client like PuTTy. Everything works perfectly in PuTTy, however when I use a small PHP client I made to test commands, it doesn't recieve the complete output. My PHP is as follows <?php echo '<html><body>'; include('config.php'); $service_port = $_GET['sPort']; $address = $_GET['sIP']; $command = null; $args = null; $api = 0; if (isset($_GET['api'])) { $api = 1; } if (!isset($_GET['sMsg'])) { die('No command given'); } else { $command = trim(strtolower($_GET['sMsg'])); if ($command == null || $command == "") { die('No command given'); } if ((strpos($command, ";")) == false) { /* Args check */ //do nothing, no args } else { $args = explode(";", $command); } } $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { die('Failed to create socket'); } $result = socket_connect($socket, $address, $service_port); if ($result === false) { die('Failed to connect to server'); } $messag = $command; $message = $messag."\n"; $out = ''; socket_write($socket, $message, strlen($message)); $resp = ""; while ($out = socket_read($socket, 2048)) { $resp .= $out; } $response = trim($resp); $rep = array("/", "\\", "-", "_", "(", ")", "'", "|", "Welcome! Please input the password.", ","); $stripped = str_replace($rep, "", $response); echo $stripped; socket_close($socket); echo '</body></html>'; ?> and when I send the command /adminsconnected the following Java code is fired by the server if (sent.equalsIgnoreCase("/adminsconnected")) { sendText("0", true); plugin.log.info("adminsConnected"); } public void sendText(String text, Boolean suppress) { try { if (suppress) { this.toClient.writeBytes(text + "\r\n"); } else { this.toClient.writeBytes(getTime() + " - "+text + "\r\n"); } if (!suppress) MainClass.log.info("[bTS] Sent: \"" + text + "\""); } catch (Exception e) { e.printStackTrace(); plugin.log.severe(e.toString() + " - sendText (suppress)"); } } My output from PHP is blank, however I recieve the "adminsConnected" and PuTTy gives "0" (the intended output) I get an error in my console: 18:25:53 [sEVERE] java.net.SocketException: Software caused connection abort: socket write error 18:25:53 [sEVERE] at java.net.SocketOutputStream.socketWrite0(Native Method) 18:25:53 [sEVERE] at java.net.SocketOutputStream.socketWrite(Unknown Source) 18:25:53 [sEVERE] at java.net.SocketOutputStream.write(Unknown Source) 18:25:53 [sEVERE] at java.io.DataOutputStream.writeBytes(Unknown Source) 18:25:53 [sEVERE] at com.atrociouss.BTS.Users.sendText(Users.java:339) 18:25:53 [sEVERE] at com.atrociouss.BTS.Users.ssendText(Users.java:323) 18:25:53 [sEVERE] at com.atrociouss.BTS.Users.run(Users.java:278) 18:25:53 [sEVERE] java.net.SocketException: Software caused connection abort: socket write error - sendText (suppress) I was wondering if this was a server issue, or a client issue and if its a mistake that I simply haven't been able to see because i've been staring at it for so long, or if theres something going terribly wrong with my code and I should start from scratch. Any help appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/271323-tcp-client-issue-works-in-putty-but-not-in-my-code/ Share on other sites More sharing options...
requinix Posted November 29, 2012 Share Posted November 29, 2012 Taking a guess since I can't see the rest of the Java code, but the server will send "0". However if $out="0" then the while loop will stop (because "0"==false) and you don't get the output. Try more like while (($out = socket_read($socket, 2048) != "") { Quote Link to comment https://forums.phpfreaks.com/topic/271323-tcp-client-issue-works-in-putty-but-not-in-my-code/#findComment-1396076 Share on other sites More sharing options...
Atrociouss Posted November 30, 2012 Author Share Posted November 30, 2012 Taking a guess since I can't see the rest of the Java code, but the server will send "0". However if $out="0" then the while loop will stop (because "0"==false) and you don't get the output. Try more like while (($out = socket_read($socket, 2048) != "") { Ohh I see what your saying. Since my output is 0:0 and the last char is '0' then the while loop is stopping. Is there any way around this? Quote Link to comment https://forums.phpfreaks.com/topic/271323-tcp-client-issue-works-in-putty-but-not-in-my-code/#findComment-1396508 Share on other sites More sharing options...
requinix Posted November 30, 2012 Share Posted November 30, 2012 "0:0"? All I see in the Java code you posted is just "0". What I'm saying is that if $out="0" then $out!=true. The way around it is exactly what I said. Quote Link to comment https://forums.phpfreaks.com/topic/271323-tcp-client-issue-works-in-putty-but-not-in-my-code/#findComment-1396515 Share on other sites More sharing options...
Atrociouss Posted November 30, 2012 Author Share Posted November 30, 2012 "0:0"? All I see in the Java code you posted is just "0". What I'm saying is that if $out="0" then $out!=true. The way around it is exactly what I said. I'm sorry I missread your reply :/ Thanks, you have answered my question Quote Link to comment https://forums.phpfreaks.com/topic/271323-tcp-client-issue-works-in-putty-but-not-in-my-code/#findComment-1396519 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.