2

我有一个 WebMatrix 3(与 ASP.NET 相同)网页,它打开一个连接到在 Azure 托管的 Linux VM 上运行的服务器进程的套接字,该服务器进程侦听客户端的 TCP 连接。Linux VM 服务器进程也是我的。当我使用 IIS 的本地副本从我的家用 PC 本地运行 WebMatrix 3/ASP.NET 网站时,它工作正常(本地发布)。当我将我的网站发布到网络并且它现在在 Azure 上运行时,我得到了异常:

试图以访问权限禁止的方式访问套接字

真正令人困惑的是,当我从套接字读取时会发生错误,但奇怪的是当我连接到它或事先写入它时不会发生错误。我知道这一点是因为异常消息装饰有当前操作,并且设置为:

等待然后读取来自 ChatScript 服务器的响应。

您可以在下面的代码中看到这一行。Azure 端是否发生了一些事情,可能会阻止从 TCP 连接到 Linux VM 的读取,但允许连接到该 VM 甚至发送?我说“偶数发送”是因为从下面的代码中可以看出,在尝试从该连接中读取之前,我会立即向 Linux VM 进程发送一条消息。

public static string readChatScriptMessage(NetworkStream myNetworkStream)
{
    if (myNetworkStream == null)
        throw new ArgumentNullException("(readChatScriptMessage) The network stream is unassigned.");

    StringBuilder myCompleteMessage = new StringBuilder();

    // Check to see if this NetworkStream is readable.
    if (myNetworkStream.CanRead)
    {
        byte[] myReadBuffer = new byte[1024];
        int numberOfBytesRead = 0;

        // Incoming message may be larger than the buffer size.
        do
        {
            numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);

            myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));

        }
        while (myNetworkStream.DataAvailable);

    }
    else
    {
        if (myNetworkStream == null)
            throw new InvalidOperationException("(readChatScriptMessage) The network stream is unassigned.");
    }

    // Print out the received message to the console.
    return myCompleteMessage.ToString();
} // public static string readChatScriptMessage(NetworkStream myNetworkStream)

    // Lookup the IP address for our chatscript server. (Cache this value
    //  in a later build since GetHostEntry() is reportedly a slow call.)
    ipAddress = Dns.GetHostEntry("myazureapp.cloudapp.net").AddressList[0];

    strCurrentOperation = "Validating URL arguments (parameters).";

    // LoginName, is mandatory.
    strLoginName = checkForValidURLArgument("LoginName", true);

    // BotName, is optional.
    strBotName = checkForValidURLArgument("BotName", false);

    // User message (chat input), is optional.  But remember,
    //  only send a blank message to start a new session
    //  with ChatScript!  After that, send the user's input
    //  each time.
    strMessage = checkForValidURLArgument("Message", false);

    strCurrentOperation = "Connecting to Linux VM TCP server.";

    // OK, we're good to go.  We have the 3 URL arguments we were expecting.

    // Connect to the ChatScript server.
    tcpCli.Connect(ipAddress, 1024);

    strCurrentOperation = "Opening the stream with the server.";

    // Open the stream
    streamChatScript = tcpCli.GetStream();
    StreamReader sr = new StreamReader(streamChatScript);
    BinaryWriter sw = new BinaryWriter(streamChatScript);
    // Create a message to send to the server, using the URL argument values
    //  passed to us.
    ChatMessage cm = new ChatMessage(strLoginName, strBotName, strMessage);

    strCurrentOperation = "Sending the desired chat message to the server.";

    // Send the message to the chat server.
    string strSendChatMsg = cm.ToString();

    // Translate the passed message into ASCII and store it as a Byte array.
    Byte[] data = System.Text.Encoding.ASCII.GetBytes(strSendChatMsg); 

    for (int i = 0; i < strSendChatMsg.Length; i++)
    {
        data[i] = (byte)strSendChatMsg[i];
    }

    // Send the chat message.
    streamChatScript.Write(data, 0, data.Length);

    strCurrentOperation = "Waiting for and then reading the response from the server.";

    strResponseMsg = ChatMessage.readChatScriptMessage(streamChatScript);
4

0 回答 0