加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

服务器反推技术,flex flash socket java javascript

发布时间:2020-12-15 01:00:06 所属栏目:百科 来源:网络整理
导读:http://hi.baidu.com/wenliang86/blog/item/844cbfca626ae29ac8176876.html 感谢大家的回答: 问题已经解决了: 如下: 服务器代码: package JSocket; ? ? import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; imp

http://hi.baidu.com/wenliang86/blog/item/844cbfca626ae29ac8176876.html


感谢大家的回答:

问题已经解决了:

如下:

服务器代码:

package JSocket;

?

?

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;

import java.util.List;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

?

public class Server {

private static final int PORT = 3000; // 端口号

private static List<Socket> list = new ArrayList<Socket>(); // 保存连接对象

private ExecutorService exec;

private ServerSocket server;

?

public static void main(String[] args) {

new Server();

}

public Server() {

try {

server = new ServerSocket(PORT);

exec = Executors.newCachedThreadPool();

System.out.println("服务器已启动!");

Socket client = null;

while (true) {

client = server.accept(); // 接收客户连接

list.add(client);

exec.execute(new ChatTask(client));

}

} catch (IOException e) {

e.printStackTrace();

}

}

?

static class ChatTask implements Runnable {

private Socket socket;

private BufferedReader br;

private String msg="";

private String msg1="";

?

public ChatTask(Socket socket) throws IOException {

this.socket = socket;

br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

}

?

public void run() {

new Thread(new Runnable() {

public void run() {

PrintWriter pw;

try {

pw = new PrintWriter(socket.getOutputStream(),true);

int i=0;

while(true){

msg="<cross-domain-policy><allow-access-from domain="" + "*" + "" to-ports="3000"/></cross-domain-policy>";

msg1=i+"你好!";

if(i==0){//如果客户端是第一次访问服务器那么发送,安全策略允许文件,这必须的否则其余的免谈

pw.println(msg);

pw.flush();

}

else{//向客户端发送数据

pw.println(msg1);

pw.flush();

}

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

i++;

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}).start();

}

}

}

客户端代码:

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"?

? xmlns:s="library://ns.adobe.com/flex/spark"?

? xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" applicationComplete="init()">

<fx:Script>

<![CDATA[

import mx.controls.Alert;

import mx.rpc.events.FaultEvent;

import mx.rpc.events.ResultEvent;

import mx.rpc.http.HTTPService;

private var socket:Socket=new Socket();

[Bindable]

public var ip:String;

[Bindable]

public var port:int;

private function init():void{

getIpPort();

}

private function getIpPort():void{

var configService:HTTPService=new HTTPService();

configService.url="config.xml";

configService.resultFormat="e4x";

configService.addEventListener(ResultEvent.RESULT,configResult);

configService.addEventListener(FaultEvent.FAULT,configFault);

configService.send();

}

private function configResult(event:ResultEvent):void

{

var xml:XML=event.result as XML;

this.ip=xml.ip;

this.port=xml.port;

ConncetServer();

}

//config fault

private function configFault(event:mx.rpc.events.FaultEvent):void

{

Alert.show("load config.xml false!");

}

private function ConncetServer():void

{

socket.addEventListener(ProgressEvent.SOCKET_DATA,OnReceived);

socket.addEventListener(Event.CONNECT,onConnected);

socket.addEventListener(Event.CLOSE,closeHandler);

socket.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);

socket.addEventListener(ProgressEvent.PROGRESS,progressHandler);

socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);

this.socket.connect(this.ip,this.port);

}

private function closeHandler(event:Event):void

{

Alert.show("socket is close!");

}

private function ioErrorHandler(event:IOErrorEvent):void

{

Alert.show("socket ioError!");

}

private function progressHandler(event:ProgressEvent):void

{

trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);

}

private function securityErrorHandler(event:SecurityErrorEvent):void

{

trace("securityErrorHandler: " + event);

}

private function onConnected(event:Event):void

{

Alert.show("socket connect success!");

}

private function Send():void

{

//socket.send("message from flex");

}

private function OnReceived(event:ProgressEvent):void

{ ? ?

var receive:String=socket.readMultiByte(socket.bytesAvailable,"gbk");

ExternalInterface.call("addValue",receive);

}

]]>

</fx:Script>

</s:Application>

js代码:

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme() + "://"

+ request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>jsocketTest</title>

<script type="text/javascript"

src="<%=basePath%>jsocketTest/JSocket/jSocket.js"></script>

</head>

<body>

<script type='text/javascript'>

function addValue(value) {

//alert(value);

mySocket.innerHTML += value+"<br/>";

}

</script>

<div id='mySocket' /></div>

? ? <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="300" height="300" id="topFlash"/>

? ? ? ? <param name="movie" value="bin-debug/SocketTest.swf" />

? ? ? ? <param name="quality" value="high" />

? ? ? ? <param name="bgcolor" value="#ffffff" />

? ? ? ? <param name="allowScriptAccess" value="sameDomain" />

? ? ? ? <param name="allowFullScreen" value="true" />

? ? ? ? <!--[if !IE]>-->

? ? ? ? <object type="application/x-shockwave-flash" data="bin-debug/SocketTest.swf" width="100%" height="100%">

? ? ? ? ? ? <param name="quality" value="high" />

? ? ? ? ? ? <param name="bgcolor" value="#ffffff" />

? ? ? ? ? ? <param name="allowScriptAccess" value="sameDomain" />

? ? ? ? ? ? <param name="allowFullScreen" value="true" />

? ? ? ? <!--<![endif]-->

? ? ? ? <!--[if gte IE 6]>-->

? ? ? ? <p>?

? ? ? ? Either scripts and active content are not permitted to run or Adobe Flash Player version

? ? ? ? 10.0.0 or greater is not installed.

? ? ? ? </p>

? ? ? ? <!--<![endif]-->

? ? ? ? ? ? <a href="http://www.adobe.com/go/getflashplayer">

? ? ? ? ? ? ? ? <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" />

? ? ? ? ? ? </a>

? ? ? ? <!--[if !IE]>-->

? ? ? ? </object>

? ? ? ? <!--<![endif]-->

? ? </object>

</body>

</html>

js代码中的swf文件是flexbuilder编译之后的文件,这个要怎么弄到稍微会点flex应该就能明白了!不多说了!

在网上找了这个叫服务器反推技术:具体实现的方式不一样,我用的是java作为tcp/ip服务器,flash作为客户端接收,然后flash和js通信达到可以将服务器的数据反推到html中的目的!有很多问题以上的代码都应该解决了!

遇到问题:

flash安全策略问题!

数据乱码问题!

再次感谢大家!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读