flash 代码
stop();
//用户名
var userName:String;
//声明一个socket
var mySocket:XMLSocket = new XMLSocket();
Security.loadPolicyFile("http://127.0.0.1/flash.xml");
loginBtn.onPress = function()
{
//如果没有输入用户名
if(nameText.text.length ==0)
{
clew.text = "<font color=\"#FF0000\">请输入用户名!</font>";
}
else
{
userName = nameText.text;
//连接到服务器
mySocket.connect("localhost", 5000);
}
}
//事件,当连接服务器成功或者失败时加载的
mySocket.onConnect = function(myStatus)
{
if (myStatus)
{
play();
}
else
{
clew.text = "<font color=\"#FF0000\">连接失败!</font>";
}
};
function sendMessage(msg:String)
{
mySocket.send(msg + "\n");
}
xml代码:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">
<!-- Policy file for xmlsocket://socks.example.com -->
<cross-domain-policy>
<!-- This is a master-policy file -->
<site-control permitted-cross-domain-policies="master-only"/>
<!-- Instead of setting to-ports="*",
administrators can use ranges and commas -->
<!-- This will allow access to ports 123, 456, 457, and 458 -->
<allow-access-from domain="localhost" to-ports="5000" />
<allow-access-from domain="127.0.0.1" to-ports="5000" />
</cross-domain-policy>
java代码:
package Server;
import java.net.Socket;
import java.net.ServerSocket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
/*
*
*/
public class ServerThread extends Thread
{
private Map map;
private Socket socket;
private BufferedReader br;
private PrintWriter pw;
private String userName;
public ServerThread(Socket socket,Map map,String userName)
{
this.socket = socket;
this.map = map;
this.userName = userName;
}
public void run()
{
System.out.println(userName + "启动一个线程了!");
try
{
//建立接受信息
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true)
{
//接收信息
String message = br.readLine();
//发送给新用户的标记;
message=message.replace("<policy-file-request/>","");
//System.out.println("收到的消息:" + message);
if(message.trim().startsWith("newUser"))
{
//System.out.println(userName + "线程信息!再此收到的信息:" + message);
//取出最新登陆用户
Socket socketTemp = (Socket)map.get("newUser");
sendMessage(socketTemp,message);
}
else
{
//向所有的用户发送当前用户登陆的信息
map.remove("newUser");
for(Iterator it = map.values().iterator(); it.hasNext();)
{
Socket socketTemp = (Socket)it.next();
//自己除外的所有用户
if(socketTemp != socket)
{
sendMessage(socketTemp,message);
}
}
}
}
}
catch (Exception e)
{
//异常处理,当有用户异常退出时,向其他用户发出退出命令
System.out.println(userName + "出现异常了");
//删除出现异常的username
map.remove(userName);
if(!map.isEmpty())
{
try
{
System.out.println("发送该用户退出命令");
sendAllUser(map,"exit:" + userName );
}
catch (Exception ee)
{
System.out.println("用户退出异常!ee:" + ee);
}
}
System.out.println("线程错误:");
}
}
public void sendAllUser(Map map,String msg) throws Exception
{
for(Iterator it = map.values().iterator(); it.hasNext();)
{
Socket socketTemp = (Socket)it.next();
//自己除外的所有用户
if(socketTemp != socket)
{
sendMessage(socketTemp,msg);
}
}
}
public void sendMessage(Socket so,String msg) throws Exception
{
PrintWriter tempPw = new PrintWriter(so.getOutputStream());
System.out.println("msg:" + msg);
tempPw.print(msg + "\0");