Java套接字服务器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java套接字服务器相关的知识,希望对你有一定的参考价值。
我已经失去了它......
我通过FlashDevelop(AS3)构建了一个简单的Flash应用程序,我希望它能够与服务器进行通信。我创建了一个简单的Socket Java应用程序,代码如下:
main.Java:
import org.xsocket.connection.*;
public class Main
{
protected static IServer srv = null;
public static void main(String[] args)
{
try
{
srv = new Server(8090, new xSocketDataHandler());
srv.run();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
protected static void shutdownServer()
{
try
{
srv.close();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
和xSocketDataHandler.java:
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;
import java.util.*;
import org.xsocket.*;
import org.xsocket.connection.*;
public class xSocketDataHandler implements IDataHandler, IConnectHandler, IDisconnectHandler
{
private Set<INonBlockingConnection> sessions = Collections.synchronizedSet(new HashSet<INonBlockingConnection>());
public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException
{
try
{
String data = nbc.readStringByDelimiter(" ");
if(data.trim().length() > 0)
{
System.out.println("Incoming data: " + data);
if(data.equalsIgnoreCase("<policy-file-request/>"))
{
nbc.write("<cross-domain-policy>" +
"<allow-access-from domain="*" secure="false" />" +
"<allow-access-from domain="*" to-ports="8090"/>" +
"</cross-domain-policy>