Flex AS3与Java的Socket通信

编程技术  /  houtizong 发布于 3年前   52

新建flash文件:SocketExample.fla
添加按钮:btnSend
修改文档类为:SocketExample


新建AS3文件:
SocketExample.as

package {     import flash.display.Sprite;     import flash.events.*;     import flash.net.Socket;          public class SocketExample extends Sprite {         private var socket:Socket;         public function SocketExample(  ) {           socket = new Socket(  );                      // Add an event listener to be notified when the connection           // is made           socket.addEventListener( Event.CONNECT, onConnect );                      // Listen for when data is received from the socket server           socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );             //var btnSend:Button = new Button();           btnSend.label = "Send Data";           btnSend.emphasized = true;           btnSend.width = 150;           btnSend.move(20, 20);           addChild(btnSend);                      btnSend.addEventListener(MouseEvent.CLICK, sendData);       }            private function sendData( event:Event ) {                    if (!socket.connected) {                          // Connect to the server               socket.connect( "192.168.2.103", 5678 );          }             trace("send...");           socket.writeUTFBytes("example\n");          socket.flush();            }             private function onConnect( event:Event ):void {           trace( "The socket is now connected..." );        }               private function onSocketData( eventrogressEvent ):void {         trace( "Socket received " + socket.bytesAvailable + " byte(s) of data:" );                  trace(socket.readMultiByte(socket.bytesAvailable, "UTF-8"));       }             }   }  


Java服务器端:
MyServer.java

import java.io.*;    import java.net.*;      public class MyServer {        public static void main(String[] args) throws IOException{            ServerSocket server=new ServerSocket(5678);              while (true) {               Socket client=server.accept();                BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));                PrintWriter out=new PrintWriter(client.getOutputStream());                //while(true){                    String str=in.readLine();                    System.out.println(str);                    out.println("has receive....");                    out.flush();                    if("end".equals(str))                        break;                //}                client.close();            }       }    }   import java.io.*; import java.net.*; public class MyServer { public static void main(String[] args) throws IOException{ ServerSocket server=new ServerSocket(5678); while (true) {Socket client=server.accept(); BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out=new PrintWriter(client.getOutputStream()); //while(true){ String str=in.readLine(); System.out.println(str); out.println("has receive...."); out.flush(); if("end".equals(str)) break; //} client.close(); }} }  

客户端控制台输出:

send...
The socket is now connected...
Socket received 17 byte(s) of data:
has receive....

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客