NATIVE和AIR app之间的套接字通信

时间:2013-12-26 12:54:11

标签: java android actionscript-3 sockets air

我是Socket编程的新手,对我来说没有任何用处。

1. I've an android native app (APP1)
2. I've an AIR for android app (APP2)

我正在使用Intent从APP2启动APP1。当APP2关闭时,我想将一些数据发送到APP1

我想将APP1作为服务器,APP2作为客户端。我想从这两个应用程序发送和接收数据。我正在尝试Socket,但它没有工作。我尝试创建普通的Java类(服务器)文件和AIR应用程序(客户端),我在命令提示符下运行java,在Adobe Flash Professional CS6中运行AIR应用程序,我正在使用localhost和端口{{1在AIR应用程序和Java类中的端口6000,它的工作正常。我能够从服务器和客户端发送和接收数据 但在Android应用程序中应用它时,没有任何作用。我想在同一设备中运行我的应用程序,即在同一设备中发送和接收数据。任何人都可以帮助我...

编辑1:
6000

ServerChat.java

import java.io.*; import java.net.*; public class ServerChat implements Runnable { ServerSocket ser; Socket s; String msg; BufferedReader br1,br2; PrintWriter pw; Thread t1=null,t2=null; public ServerChat() { System.out.println("Server Running..."); try { ser=new ServerSocket(6000); System.out.println("Socket Created"); s=ser.accept(); System.out.println("accepted"); br1=new BufferedReader(new InputStreamReader(System.in)); br2=new BufferedReader(new InputStreamReader(s.getInputStream())); pw=new PrintWriter(s.getOutputStream(),true); } catch(Exception e) {} } public void run() { try { do { if(Thread.currentThread()==t1) { System.out.print("\nServer:"); msg=br1.readLine(); pw.println(msg); } else { msg=br2.readLine(); System.out.println("Client:"+msg);} } while(!msg.equals("Q")); ser.close(); } catch(Exception e) {} } public static void main(String args[]) { ServerChat ser2=new ServerChat(); ser2.t1=new Thread(ser2); ser2.t2=new Thread(ser2); ser2.t1.start(); ser2.t2.start(); } }

ClientChat.java

我刚刚复制了这些代码并将其粘贴到记事本中并将其保存为.java,我编译并在第一个cmd提示符中以import java.io.*; import java.net.*; public class ClientChat implements Runnable { Socket s; String msg; BufferedReader br1,br2; PrintWriter pw; Thread t1=null,t2=null; public ClientChat() { System.out.println("Client Started..."); try { s=new Socket("localhost",6000); br1=new BufferedReader(new InputStreamReader(System.in)); br2=new BufferedReader(new InputStreamReader(s.getInputStream())); pw=new PrintWriter(s.getOutputStream(),true); } catch(Exception e) {} } public void run() { try { do { if(Thread.currentThread()==t1) { msg=br1.readLine(); pw.println(msg); } else { msg=br2.readLine(); System.out.println(msg); } } while(!msg.equals("Q")); } catch(Exception e) {} } public static void main(String args[]) { ClientChat c=new ClientChat(); c.t1=new Thread(c); c.t2=new Thread(c); c.t1.start(); c.t2.start(); } } 然后javac ServerChat.java在{2}不同的命令提示符中运行它clientChat在第二个cmd提示符下。它运行正常。
我将服务器保持原样,即java ServerChat,我在Adobe Flash Professional CS6中创建了一个AIR应用程序,只需添加一个文本框和一个名为ServerChat.java的按钮和_data分别创建了两个名为_sendCustomSocket.as的.as文件,并在其中粘贴以下代码。然后按SocketExample.as运行ServerChat.java和AIR应用程序我可以从java服务器向AIR客户端发送和接收数据。
CTRL+ENTERplease help me to implement my ServerChat.java in my android App

whether localhost works if we run it in actual android device

CustomSocket.as

package { import flash.errors.*; import flash.events.*; import flash.net.Socket; class CustomSocket extends Socket { private var response:String; public function CustomSocket(host:String = null, port:uint = 0) { super(); configureListeners(); if (host && port) { super.connect(host, port); } } private function configureListeners():void { addEventListener(Event.CLOSE, closeHandler); addEventListener(Event.CONNECT, connectHandler); addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); } public function writeln(str:String):void { str += "\n"; try { writeUTFBytes(str); trace("wrote"); flush(); } catch(e:IOError) { trace(e); } } private function sendRequest():void { trace("sendRequest"); response = ""; writeln("GET /"); } private function readResponse():void { var str:String = readUTFBytes(bytesAvailable); response += str; trace("Response:"+response); } private function closeHandler(event:Event):void { trace("closeHandler: " + event); trace(response.toString()); } private function connectHandler(event:Event):void { trace("connectHandler: " + event); sendRequest(); } private function ioErrorHandler(event:IOErrorEvent):void { trace("ioErrorHandler: " + event); } private function securityErrorHandler(event:SecurityErrorEvent):void { trace("securityErrorHandler: " + event); } private function socketDataHandler(event:ProgressEvent):void { trace("socketDataHandler: " + event); readResponse(); } } }

SocketExample.as

0 个答案:

没有答案