Sending and Receiving from a port

Hi,



I have a Python program which streams this data over a certain port if we send it the command ‘Send’. I’m trying to read the data it streams for my game but according to the tutorial, I can only send a message class object (or its abstraction) to the Server. How can I send a simple string ‘Send’ to the Server and how do I read what the Server streams into a String object?



Thanks…

Use java sockets…

@neogeek

I think you are looking for something like this.



Sudo Code

[java]

private Socket socket;

private BufferedReader in;

private PrintWriter out;



public void connect() {

try {

socket = new Socket(“localhost”, 555555);

in = new BufferedReader(

new InputStreamReader(sock.getInputStream()));

out = new PrintWriter(sock.getOutputStream(), true);

// I would start a thread to listen to the input.

} catch(Exception e) {

// you failed to connect

}

}



public void send(String string) {

out.println(string);

}



// This is what I would put into the thread

public void read() {

String inLine = in.readLine();

}

[/java]



This is a simple example if you understand it you should be able to implement something like this and make it do what you want.



I made a simple NVE with jMonkey using this method and passing XML.

@neogeek



Just because I mentioned it… :slight_smile:

[java]

public class NetListener extends Thread {



private BufferedReader in;



public NetListener(BufferedReader in) {

this.in = in;

}



@Override

public void run() {

String inLine = in.readLine();

while (inLine != null) {



// Do somting with line;



inLine = in.readLine();

}

}



}

[/java]

This came from Killer Game Programming in Java

Net Tour 3D

@neogeek said:
Hi,

I have a Python program which streams this data over a certain port if we send it the command 'Send'. I'm trying to read the data it streams for my game but according to the tutorial, I can only send a message class object (or its abstraction) to the Server. How can I send a simple string 'Send' to the Server and how do I read what the Server streams into a String object?

Thanks..


As others have pointed out, your question is more on the level of regular Java networking. SpiderMonkey uses its own tight Java-to-Java protocol and is probably not well suited for just sending any random data. It can be done but it's likely easier just to do your own networking for that kind of data.

If you are really curious, I could explain more how to prepare SpiderMonkey to receive this sort of data and how you would have to send it.

Thanks all. I’ve been trying to make this work but even the Java Sockets are giving me problems. I have been able to send the Python program the command to send, but the problem now occurs in reading the data from the port streamed by the Python program.



[java]try {

listenSocket();

String line = in.readLine();

System.out.println(“Text received :” + line);

}[/java]



the line [java] String line = in.readLine(); [/java] is giving me problems and the program just hangs there. Although the Python program does stream ‘n’ after each line of data, I’m assuming the readLine function is not detecting it and thus it runs for ever. (“in” is a BufferedReader btw)



Is there any function to read a float value form the stream?

Things get messy when you start streaming data between different platforms/languages/etc.



For example how do you know if the java and python internal representations of a float are compatible? How about network byte order when packing integers, etc.



You may be better off taking a step back and looking at any of the already established multi-language protocols and using them rather than trying to roll your own. For example Google Protocol Buffers are fast and already support multiple languages including Java and Python.

@neogeek said:
Thanks all. I've been trying to make this work but even the Java Sockets are giving me problems. I have been able to send the Python program the command to send, but the problem now occurs in reading the data from the port streamed by the Python program.

[java]try {
listenSocket();
String line = in.readLine();
System.out.println("Text received :" + line);
}[/java]

the line [java] String line = in.readLine(); [/java] is giving me problems and the program just hangs there. Although the Python program does stream 'n' after each line of data, I'm assuming the readLine function is not detecting it and thus it runs for ever. ("in" is a BufferedReader btw)

Is there any function to read a float value form the stream?


zarch is absolutely right. But also, in the above code "listenSocket()" concerns me. How do you know it's hanging in readLine and not that call?