ID:152781
 
Anyone here familiar with the way Java handles sockets and what not. I can not seem to be able to send binary over sockets. I was using PHP to send data to the Byond world using sockets and it works fine. However I am having trouble duplicating in java. I always get the (hit enter to continue) returned. I have tried almost anything to send the hex and or binary values over in a byte like I do in php. But its getting converted to a number or a string and wont send it properly. So if anyone knows how to send the data over java I would be very thankful.
Sockets are basically the same everywhere. If you post some code I'll have a look at it.
In response to Crispy
import java.io.*;
import java.net.*;

public class EchoClient {
public static void main(String[] args) throws IOException {

Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
DataOutputStream test = null;

try {
echoSocket = new Socket("127.0.0.1", 2323);

out = new PrintWriter(echoSocket.getOutputStream(), true);
test = new DataOutputStream(echoSocket.getOutputStream());

in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}

BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
test.writeChar(0x00);
test.writeChar(0x83);
test.writeChar(0x15);
test.writeChar(0x00);
test.writeChar(0x00);
test.writeChar(0x00);
test.writeChar(0x00);
test.writeChar(0x00);
test.writeBytes("Hey Byond World");
test.writeChar(0x00);
test.flush();





if(false)
{
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
}


I have tried all the different outputs but I can not send the correct data over Byond. Heh with PHP I managed it, and I can send java and php to each other.. but... the problem is I cant get java to send the hex or bytes to byond.
In response to DoOmBringer
Firstly, the length of the message should be represented using 2 bytes, not 1. Secondly, the length of the message is not 0x15; it's 15. There's a difference. You either need to put 15, or 0x0F (which is 15 written in hexadecimal format).

    test.writeChar(0x00);
test.writeChar(0x83);
test.writeChar(0x00); // You missed out this line
test.writeChar(15); // This line was wrong
test.writeChar(0x00);
test.writeChar(0x00);
test.writeChar(0x00);
test.writeChar(0x00);
test.writeChar(0x00);
test.writeBytes("Hey Byond World");
test.writeChar(0x00);
test.flush();
In response to Crispy
The length of the message is actually 21 bytes.. Hey Byond World + the 5 bytes before and the 1 byte following it. At least thats how it works in the PHP version. Thanks for the help though, I think Im getting closer to getting it to work.
In response to DoOmBringer
Alright its working now. The only thing missing was the empty byte before the length. Thanks alot Crispy, now I start development on my "secret" project heh.
Heh. wow, I did the same thing 2 months ago, I actually successfuly got it to send and receive messages from byond, I was planning on making BYOND worlds compatible with my Java games, so you can play byond games as a java applet. heh. almost got it working too.