0

所以,我有一个服务器/客户端应用程序,在客户端需要登录到服务器的代码中的特定点,所以服务器从客户端请求用户名和密码(然后检查它并等等等等)

这是我的代码,客户端(AS3)

function hwndLogKeyboard(evt:KeyboardEvent):void
{
    if (evt.keyCode == 13)
    {
        var allow:Boolean = false;
        var prompt:String;

        socket.writeMultiByte("002", "us-ascii");
        socket.writeByte(0);
        socket.flush();

        while (socket.bytesAvailable == 0)
        {
            trace("waiting for bytes to read");
        }

        prompt = socket.readMultiByte(2, "us-ascii"); //YOU WERE MISSING ); here originally updated as I was formatting, probably just copy paste error
        trace(prompt);
        if (prompt == "UN")//server has prompted for username information, time to send it
          {  
            socket.writeMultiByte(inputname.text, "us-ascii");
            socket.flush();

            prompt = socket.readMultiByte(2, "us-ascii");

            if (prompt == "PW")//server has prompted for password information, time to send it
              {
                 socket.writeMultiByte(inputpass.text, "us-ascii");
                 socket.writeByte(0);
                 socket.flush();
             }
         }

        //more code in here that i have commented out until this issue is resolved

    }

和服务器端(我肯定问题正在发生,用 C++ 编写)

if(testr >= 0)
{
    string dataR = string(buffer);
    //data was recived from a client - analize the data to find out what it means
    //signals are in the format "xxx" where 'x' is an int between 0-9

    if(dataR == "001")
    {
        cout << "A new client has connected" << endl;
    }
    else if(dataR == "002")//002 is a client attempting to log-on to the server
    {
        cout << "Client requesting log-on" << endl;
        char recvUsername[16];
        const char requestUsername[3] = "UN";
        char recvPassword[16];
        const char requestPassword[3] = "PW";

        send(Client,requestUsername,3,0);//request username from client

        recv(Client,recvUsername,16,0);//got username, now time to request password

        send(Client,requestPassword,16,0);//request password from client

        recv(Client,recvPassword,3,0);//got password, now time to compare them against current records

        cout << recvUsername << endl << recPassword << endl;
    }
    else if(dataR == "003")
    {
        cout << "A client has used chat" << endl;
    }
}
else
{
    cout << "error" << endl << testr << endl << WSAGetLastError << endl;
}

起初我认为这是一个时间问题,也许客户端在服务器可以写入之前从缓冲区读取,因为 AS3 崩溃是因为读取缓冲区是空的!

但是,这是诀窍,我可以将我的移动send(Client,requestUsername,3,0)if(dataR == "001")块内,它工作正常。闪存在寻找数据时可以读取数据。事实上,在客户端的套接字创建之后,我几乎可以将该发送语句放在我的服务器中的任何位置,并且它可以工作,只是不是我需要它的地方。

这就是为什么我添加了

while (socket.bytesAvailable == 0)
{                                   
    trace("waiting for bytes to read");
}

在客户端中,这样它就会一直循环,直到有数据要读取,如果这是问题所在,我会重新编写一些东西来避免那个讨厌的while循环必须在那里。我不明白的是它进入了一个无限循环,用跟踪语句淹没了我。服务器不会将数据写入套接字,而是从代码中的其他任何地方写入数据。我没有收到任何错误,没有编译器投诉,它没有超出范围或类似的东西,这里有些奇怪。

非常感谢您的帮助-泰勒

仍然有这个问题,它在代码中的其他任何地方都可以使用,除了“else if”块内,我完全被难住了。

4

0 回答 0