兄弟我最近在研究flex+java+socket,第一次交互已经完成了,客户端请求策略文件,服务器发送策略文件给客户端,解决了沙箱安全的问题,但是碰到一个很奇怪的问题 就是客户端和服务端通讯了一次后(策略请求不算) 服务端就得不到客户端的消息了 一直会卡在server.accept() 也就是在等客户端的请求 但是我明明已经发了请求过去了 想请教下各位。。。。
以下是java服务端
以下是flex客户端
以下是java服务端
package com.medou;
import java.io.*;
import java.net.*;
public class Server {
private ServerSocket server;
private BufferedReader reader;
private BufferedWriter writer;
Socket client;
private String xml;
public Server()
{
xml="<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/>"
+"<allow-access-from domain=\"*\" to-ports=\"*\"/>"
+"</cross-domain-policy>";
System.out.println(xml);
//启动843端口
createServerSocket(843);
}
//启动服务器
private void createServerSocket(int port)
{
try {
server = new ServerSocket(port);
System.out.println("服务监听端口:" + port);
while(true) {
//接收客户端的连接
client = server.accept();
InputStreamReader input = new InputStreamReader(client.getInputStream(), "UTF-8");
reader = new BufferedReader(input);
OutputStreamWriter output = new OutputStreamWriter(client.getOutputStream(), "UTF-8");
writer = new BufferedWriter(output);
//读取客户端发送的数据
StringBuilder data = new StringBuilder();
char[] a = new char[50];
System.out.println("11111");
int j = reader.read(a);
for(int i=0;i<j;i++) {
System.out.print(a[i]+",");
if (a[i] != '\0') {
data.append(a[i]);
} else {
break;
}
}
String info = data.toString();
System.out.println("输入的请求: " + info);
//接收到客户端的请求之后,将策略文件发送出去
if(info.indexOf("<policy-file-request/>")>=0)
{
writer.write(xml + "\0");
writer.flush();
writer.close();
reader.close();
client.close();
}
else
{
System.out.println("dasdas");
writer.write("0");
writer.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//调用垃圾收集方法
try
{
if(reader != null){
reader.close();
reader=null;
}
System.out.println("dad");
if(writer != null){
writer.close();
writer=null;
}
if (client != null) {
client.close();
client = null;
}
} catch(Exception ie){}
//System.gc();
}
}
//测试主函数
public static void main(String[] args)
{
new Server();
}
}
以下是flex客户端
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="initApp()">
<mx:Script>
<![CDATA[
import flash.net.Socket;
import flash.utils.ByteArray;
import flash.events.ProgressEvent;
private var socket:Socket;
private var intClient:int;//定义客户端数字出拳
private var intServer:int;//定义服务端数字出拳
private var intSY:int;//定义输赢判断
private var strClient:String;//客户端文字出拳
private var strServer:String;//服务端的文字出拳
internal function initApp():void
{
//Security.loadPolicyFile("http://localhost:9000/crossdomain.xml");
//Security.loadPolicyFile("xmlsocket://localhost:8888");
socket=new Socket();//创建Socket对象
socket.connect("localhost",843);//连接服务器
socket.addEventListener(Event.CONNECT,funConnect);//监听是否连接上服务器
socket.addEventListener(Event.CLOSE,funClose);//监听套接字连接是否关闭
//socket.addEventListener(ProgressEvent.SOCKET_DATA,funSocket);//监听服务器的新信息
}
internal function funConnect(event:Event):void
{
myText.text+="连接已建立\n";
}
internal function funClose(event:Event):void
{
myText.text+="连接已关闭\n";
}
internal function sendMessage(msg:String):void
{
try{
socket.writeUTFBytes(msg);
socket.flush();
}catch(e:IOError){//没有连上服务器就发送数据,会发生IOError异常
trace("未连接服务器");
}
intClient=int(msg);
switch(intClient)//将数字转换成文字用于发布到屏幕上
{
case 0:
strClient="剪刀";
break;
case 1:
strClient="锤";
break;
case 2:
strClient="包袱";
break;
}
myText.text+="你出:"+strClient+"\n";
//myInput.text="";
}
internal function funSocket(event:ProgressEvent):void//接到服务器信息
{
var msg:String="";
while(socket.bytesAvailable) {
msg+=socket.readUTFBytes(socket.bytesAvailable);
}
socket.writeUTFBytes("2");
socket.flush();
myInput.text=msg;
intServer=int(msg);
intSY=intClient-intServer;//通过数值判断输赢
switch(intServer)//将数字转换成文字用于发布到屏幕上
{
case 0:
strServer="剪刀";
break;
case 1:
strServer="锤";
break;
case 2:
strServer="包袱";
break;
}
myText.text+="服务器出:"+strServer+"\n";
//判断输赢的算法
if(intSY>0&&intSY<2)
{
myText.text+="结果:你赢了\n";
}else if(intClient==intServer)
{
myText.text+="结果:平手\n";
}else if(intSY<0&&intSY>-2)
{
myText.text+="结果:你输了\n";
}else if(intSY>=2)
{
myText.text+="结果:你输了\n";
}else if(intSY<=-2)
{
myText.text+="结果:你赢了\n";
}
myText.verticalScrollPosition = myText.maxVerticalScrollPosition;// myText滚动到最下面
}
]]>
</mx:Script>
<mx:TextArea x="10" y="32" width="331" height="263" id="myText"/>
<mx:TextInput x="10" y="303" width="245" id="myInput"/>
<mx:Button x="273" y="304" label="发送" id="myBtn" click="sendMessage(myInput.text)"/>
<mx:Label x="10" y="10" text="规则:0代表剪刀;1代表锤;2代表包袱" width="331" fontWeight="bold"/>
</mx:Application>