PDA

View Full Version : XMLSocket


LOLFlash
06-25-2007, 02:58 PM
Hi all

I'm trying make java socket working but but so far:

I recive message from flash
flash trace when server down
but I cant send data from server to flash

If somebody familiar

out.write(output);
System.out.print("Sending "+output);

Prints fine but I have nothing in flash

var myXML:XMLSocket = new XMLSocket();

myXML.onConnect = handleConnect;
myXML.onData = handleData;
myXML.onXML = handleXML;
myXML.onClose = handleDisconnect;
function doConnection(){
myXML.connect("localhost", 9001);
myXML.send ("HOWDY FROM FLASH" + new Date().toString());

}
function handleConnect(connectionStatus){
connectionStatus ? trace("Connected.") : trace("Connection failed.");
}
function handleData(dta){
trace("Data"+dta)
}
function handleXML(xmlObject){
trace("Object recieved:: "+xmlObject);
}
function handleDisconnect(){
trace("Connection lost.");
}
function closeConnection(){
trace("Closing connection to server.");
myXML.close();
}

function sendData(){

myXML.send(1234567);

};



import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

class Socketer {
int port; // we have one variable that contains the socket

boolean stillRunning = true;

boolean stillTakingConnections = true;
String feedback = "Okay";
OutputStream out;
InputStream in;
ServerSocket listener = null;
Socket client;
SerialServer parent;
Thread listeningThread;

Socketer(int _port, SerialServer _parent) {
//this is a "constructor" method.
//Being the namesake of the class it is called when the object is made.
port = _port;
parent = _parent;
//let's take parameter variables and make it into an instance variable
//remember that parameter variables only last for this method and we
// want to use these again
listeningThread = new Thread(new ListeningThread());
listeningThread.start();
}

public void kill() {

stillTakingConnections = false;
stillRunning = false;
if (listener != null) {
try {
listener.close();
} catch (IOException e) {
System.out.println("Couldn't close port" + port + " " + e);
}
}
if (client != null) {
try {
client.close();
} catch (IOException e) {
System.out.println("Try to close " + e);
}
}
}

public String send(int output) {
try {
if (out != null) {
out.write(output);
return "OKAY";
}
} catch (IOException e) {
System.out.println("I/O Error no more port? " + e);
}
return "NOT OKAY";
}

// this is an inner class, separate class that gets access to all the stuff from the other class
public class ListeningThread implements Runnable{
public void run() {
try {
listener = new ServerSocket(port);
System.out.println("Acception Connection" + listener.getLocalPort());
} catch (IOException e) {
parent.socketStatus("Socket port " + port + " taken");
stillRunning = false;
stillTakingConnections = false;
}
while (stillTakingConnections) {
try { //we might have to catch some errors later.
parent.socketStatus("Waiting for connection on port " + port);
client = listener.accept(); //wait for someone to knock
in = client.getInputStream();
out = client.getOutputStream();

parent.socketStatus("Got Connection on port " + port + " from " + client.getInetAddress().getHostName());
stillRunning = true;
while (stillRunning) {//keep listening for lines
//Thread.sleep(1);

int input = in.read();
if (input == -1) {
stillRunning = false;
parent.socketStatus("Socket Disconnected - 1");
} else {
parent.relayToSerial(input);
}
}//end while listening for more lines
client.close();

} catch (IOException e) {
//parent.socketStatus("Socket Disconnected");
stillRunning = false;
System.out.println("I/O Error They probably left " + e);
}
}

}//end of run
}//end of the inner class ListeningThread

peptobismol
06-25-2007, 07:14 PM
I haven't looked at a java socket in a long while... Just make sure you're outputting to port 9001. And make sure to include the right socket libraries.

LOLFlash
06-26-2007, 01:01 AM
Thanks peptobismol for replay

My barcode reader doesnt give me substituteChar it was 0

i added : boolean ok = mySocket.send(0).equals("OKAY"); after loop

It works now it is from main application:





public void newSocket() {
mySocket = new Socketer(Integer.parseInt(socketPort.getText()), this);
}



public void gotFromSerial(byte[] _byteArray){
//public void relayToSocket(int what) {
for(int i = 0; i < _byteArray.length; i++){
int what = (int) (_byteArray[i] & 0xff);
if ((substituteChar != 0) && (what == substituteChar))what = 0;

boolean ok = mySocket.send(what).equals("OKAY");
}
boolean ok = mySocket.send(0).equals("OKAY"); //this line I added
}

gracecheah
06-29-2007, 03:05 AM
yes, java need to send 0 byte at the end of the message that u want to send to flash. for example like

// Message terminator
char EOF = (char)0x00;

as message terminator.