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
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