从Windows Phone 8传输TCP套接字

时间:2013-02-11 15:31:49

标签: windows-phone-8

我正在尝试使用TCP将数据从客户端(Windows Phone 8)应用程序传输到Windows窗体应用程序(充当服务器)。

在Windows窗体应用中,我正在创建一个像这样的监听器

         TcpListener tcpListener = null;

        IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
        try
        {
            // Set the listener on the local IP address
            // and specifing the port.
            tcpListener = new TcpListener(ipAddress, 13);
            tcpListener.Start();
            output = "Waiting for a connection...";
        }
        catch (Exception e)
        {
            output = "Error: " + e.ToString();
            MessageBox.Show(output);
        }

        while (true)
        {

            Thread.Sleep(10);
             TcpClient tcpClient = tcpListener.AcceptTcpClient();

            byte[] bytes = new byte[256];

            NetworkStream stream = tcpClient.GetStream();
            stream.Read(bytes, 0, bytes.Length);
          }

在windows phone 8 app中我使用托管套接字连接到监听器这样的东西

    public Task SendUsingManagedSocketsAsync(string strServerIP)
    {


        // enable asynchronous task completion
        completionSource = new TaskCompletionSource<object>();
        // create a new socket 
        var socket = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream,
            ProtocolType.Tcp);

        // create endpoint 
        var ipAddress = IPAddress.Parse(strServerIP);
        var endpoint = new IPEndPoint(ipAddress, PORT);

        // create event args 
        var args = new SocketAsyncEventArgs();
        args.RemoteEndPoint = endpoint;
        args.Completed += SocketConnectCompleted;

        // check if the completed event will be raised. If not, invoke the handler manually. 
        if (!socket.ConnectAsync(args))
            SocketConnectCompleted(args.ConnectSocket, args);

        return completionSource.Task;
    }

但我收到连接拒绝的套接字错误。我的方法是正确的,如何通过Windows Phone连接tcp监听器..这里我不能改变Windows窗体应用程序的代码,因为它是我的服务器代码实时..或者是强制改变我的服务器的代码....

0 个答案:

没有答案