Jump to content

Tcp Client Issue - Works In Putty But Not In My Code


Atrociouss

Recommended Posts

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!

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) != "") {

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?

"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 :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.