0

好吧,基本上我一直在尝试了解如何设置多人 Flash 游戏,最近我得出结论使用 Java 编写自己的服务器。经过数小时的尝试,我终于设法做到了,但我遇到了障碍。这是我的服务器和客户端代码。

服务器:

    import java.net.*;
    import java.io.*;

    public class ServerFlash
    {   
        public static void main(String[] args) throws IOException
        {
            ServerSocket serverSocket = null;
            Socket connection = new Socket();
            InputStream in = null;
            boolean listening = true;

            try
            {
                serverSocket = new ServerSocket(14804);
                System.err.println("The server is now bound to port.");
            }
            catch (IOException e)
            {
                System.err.println("Could not bind server to port.");
                System.exit(-1);
            }

            while (listening)
            {
                connection = serverSocket.accept();
                if(connection!=null)
                {
                    System.err.println("Connection to client has been established.");

                    in = connection.getInputStream();
                    listening = false;
                }
            }
            while (!listening)
            {
                int data = in.read();
                if(data!=-1)
                {
                    System.err.println(data);
                }
            }

        }
    }

客户:

var server = new Socket();
server.connect("[insertmyip]",14804);

server.addEventListener(Event.CONNECT, onConnect);
server.addEventListener(Event.CLOSE, onClose);
server.addEventListener(IOErrorEvent.IO_ERROR, onError);
server.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
server.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);

function onConnect(e:Event)
{
    trace("Connection Established.");
}
function onClose(e:Event)
{
    trace("Socket has been Close.");
}
function onError(e:Event)
{
    trace("An Error has occured." + e);
}
function onResponse(e:Event)
{
    trace("Responce has been recieved.");
}
function onSecError(e:Event)
{
    trace("A SecError has occured.");
}

当我启动服务器时,一切正常。但是,当我启动客户端时,它成功连接到服务器,但是服务器突然从客户端接收数据(如果您阅读代码,服务器会读取传入的数据并显示它)并显示大约 15 行代码和它不接受来自客户端的任何新数据。然而,这只发生在客户端从网站(或文件夹)启动时,但当我在 Adob​​e Flash Pro 中打开客户端作为测试时,它可以正常工作。

如果您需要文件并将其移交。

4

0 回答 0