如何在临时端口上侦听ACK包

时间:2016-02-23 18:18:51

标签: windows sockets windows-phone-8 winjs tftp

我需要编写一个tftp客户端实现,将Windows Phone 8.1中的文件发送到硬件。 因为我需要能够支持Windows 8.1,所以我需要使用Windows.Networking.Sockets classes

我能够发送我的写请求包但我收到ack包(wireshark)时遇到了麻烦。这个ack包发送到"临时端口"根据TFTP规范,端口根据wireshark被阻止。

我知道如何在特定端口上使用套接字,但我不知道如何能够接收发送到不同(临时)端口的ack包。我需要使用用于该ack包的端口来继续TFTP通信。

我如何能够接收ACK包并继续在不同的端口上工作?我需要将套接字绑定到多个端口吗?我一直试图在微软文档和谷歌上找到答案,但其他实现到目前为止我没有运气。

作为参考我目前的实施:

    try {
        hostName = new Windows.Networking.HostName(currentIP);
    } catch (error) {
        WinJS.log && WinJS.log("Error: Invalid host name.", "sample", "error");
        return;
    }

    socketsSample.clientSocket = new Windows.Networking.Sockets.DatagramSocket();
    socketsSample.clientSocket.addEventListener("messagereceived", onMessageReceived);
    socketsSample.clientSocket.bindEndpointAsync(new Windows.Networking.HostName(hostName), currentPort);

    WinJS.log && WinJS.log("Client: connection started.", "sample", "status");
    socketsSample.clientSocket.connectAsync(hostName, serviceName).done(function () {
        WinJS.log && WinJS.log("Client: connection completed.", "sample", "status");
        socketsSample.connected = true;
        var remoteFile = "test.txt";
        var tftpMode = Modes.Octet;
        var sndBuffer = createRequestPacket(Opcodes.Write, remoteFile, tftpMode);

        if (!socketsSample.clientDataWriter) {
            socketsSample.clientDataWriter =
                new Windows.Storage.Streams.DataWriter(socketsSample.clientSocket.outputStream);
        }

        var writer = socketsSample.clientDataWriter;
        var reader;
        var stream;

        writer.writeBytes(sndBuffer);

        // The call to store async sends the actual contents of the writer 
        // to the backing stream.
        writer.storeAsync().then(function () {
            // For the in-memory stream implementation we are using, the flushAsync call 
            // is superfluous, but other types of streams may require it.
            return writer.flushAsync();
        });
    }, onError);

1 个答案:

答案 0 :(得分:1)

终于找到了问题。 我使用了getOutputStreamAsynch而不是connectAsynch,现在它在客户端套接字上接收消息:

一些代码:

  tftpSocket.clientSocket.getOutputStreamAsync(new Windows.Networking.HostName(self.hostName), tftpSocket.serviceNameConnect).then(function (stream) {
        console.log("Client: connection completed.", "sample", "status");
        var writer = new Windows.Storage.Streams.DataWriter(stream); //use the stream that was created when calling getOutputStreamAsync
        tftpSocket.clientDataWriter = writer; //keep the writer in case we need to close sockets we also close the writer

        writer.writeBytes(sndBytes);

        // The call to store async sends the actual contents of the writer
        // to the backing stream.
        writer.storeAsync().then(function () {
            // For the in-memory stream implementation we are using, the flushAsync call
            // is superfluous, but other types of streams may require it.
            return writer.flushAsync();
        });
    }, self.onError);