Flex AS3与Java的Socket通信


新建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....

你可能感兴趣的:(java,.net,socket,Flex,Flash)