学习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
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |