Flex AS3与Java的Socket通信
编程技术  /  houtizong 发布于 3年前   52
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")); } } }
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(); }} }
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接