论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Flash AS教程
Tag:2.0,3.0菜鸟,游戏,,cs,技巧,源码,,文本,文字,函数,音乐,随机,拖拽,asp,access,xml,mc,视频教程

学习Flex+Java Socket多线程

文章类别:Flash AS | 发表日期:2009-8-8 11:29:58

学习Flex+Java Socket多线程

Socket服务主程序 SocketServer.java

package org.codedata;
import java.net.*;
import java.io.*;

public class SocketServer extends Thread {
private ServerSocket ss;
private Socket socket;
private int port;

private boolean run = true;

public SocketServer(int port){
this.port = port;
start();
}

public void run(){
try {
new SecurityServer();
ss = new ServerSocket(port);
System.out.println("正在启动服务。。。");
System.out.println("服务启动成功,端口("+ port +")");
Message.append("正在启动服务。。。");
Message.append("服务启动成功,端口("+ port +")");

while (run){
socket = ss.accept();
Message.append("收到消息:" + socket);
System.out.println("收到连接" +  socket);
new SocketThread(socket);
}
} catch (IOException e) {
System.out.println("Error:" + e.getMessage());
}
}

public void sstop(){
try{
run = false;
ss.close();
SocketFrame.getJTextArea().append("服务已停止\n");
}catch(Exception e){
System.out.println("发生错误:" + e.getMessage());
}

}
}

Socket服务线程 SocketThread.java

package org.codedata;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketThread extends Thread {

private BufferedReader br;
private PrintWriter pw;
private Socket so;
private boolean status = true;

public SocketThread(Socket socket){
so = socket;
start();
}

public void run(){
try{
while(status){

br = new BufferedReader(new InputStreamReader(so.getInputStream(), "UTF8"));
pw = new PrintWriter(new OutputStreamWriter(so.getOutputStream(), "UTF8"));

String line = br.readLine();
if(line != null){
line = line.trim();
pw.println("收到消息:" + line);
pw.flush();
Message.append("收到消息:" + line);
System.out.println("收到消息:" + line);
}
}
} catch(IOException e){
System.out.println("Error:" + e.getMessage());
}
}

public void close(){
//socketServer.stop();
}
}

Flex主程序: SocketClient.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontFamily="Arial" fontSize="12" applicationComplete="init()">
<mx:Script>
<![CDATA[
import flash.system.fscommand;
import flash.utils.setInterval;
import mx.controls.Alert;
import com.test.MySocketClient;

private var sc:MySocketClient = new MySocketClient();
public function init():void{
connectBtn.addEventListener(MouseEvent.CLICK, connect);
sendBtn.addEventListener(MouseEvent.CLICK, send);

setInterval(function():void{
if(sc.isConnected()){
if(sc.readReady()){
msgBox.text = msgBox.text.replace(/\s*$/g,"");
msgBox.text += msgBox.text == "" ? sc.read() : "\n" + sc.read();
}
}
}, 1000);
}

public function connect(e:MouseEvent):void{
sc.connect(host.text, (int)(port.text));
}

public function send(e:MouseEvent):void{
sc.send(msgInput.text);
msgInput.text = "";
}
]]>
</mx:Script>
<mx:Button id="sendBtn" x="381" y="245" label="发送消息" />
<mx:TextInput id="msgInput" x="29" y="245" width="344" height="22"/>
<mx:TextArea id="msgBox" x="29" y="79" width="422" height="156"/>
<mx:Label x="29" y="58" text="收到的消息" width="69" height="18" fontFamily="Times New Roman"/>
<mx:Label x="348" y="58" text="连接状态:"/>
<mx:Label x="409" y="58" text="未连接"/>
<mx:TextInput id="host" x="67" y="29" width="165" height="21" text="127.0.0.1"/>
<mx:TextInput id="port" x="296" y="29" width="63" height="21" text="999"/>
<mx:Label x="30" y="30" text="主机:"/>
<mx:Label x="260" y="30" text="端口:"/>
<mx:Button id="connectBtn" x="392" y="30" label="连接"/>
</mx:Application>

MySocketClient.as

package com.test
{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
import flash.system.Security;

import mx.controls.Alert;
public class MySocketClient
{
private var socket:Socket = null;
private var connected:Boolean = false;

public function MySocketClient()
{
socket = new Socket();
socket.addEventListener(Event.CONNECT, function(event:Event):void{
connected = true;
trace("connect: " + event);
});
socket.addEventListener(Event.CLOSE, function(event:Event):void {
trace("closeHandler: " + event);
});
socket.addEventListener(ProgressEvent.SOCKET_DATA, function(event:ProgressEvent):void {
trace("socketDataHandler: " + event);
});
socket.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void{
trace("ioErrorHandler: " + event);
});
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(event:SecurityErrorEvent):void{
Alert.show(event.toString());
});
}

public function connect(host:String, port:int):void{
Security.loadPolicyFile("xmlsocket://127.0.0.1:843");
socket.connect(host, port);

}

public function isConnected():Boolean{
return socket.connected ? true : false;
}

public function send(msg:String):void{
socket.writeUTFBytes(msg + "\n");
socket.flush();
}

public function readReady():uint{
return socket.bytesAvailable;
}

public function read():String{
return socket.readUTFBytes(socket.bytesAvailable);
}
}
}

Flex+Java Socket多线程实列.rar

视频教程列表
文章教程搜索
 
Flash AS推荐教程
Flash AS热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058