1

私は得続けます:

System.dll System.Net.Sockets.SocketException (0x80004005): ターゲット マシンがアクティブに拒否したため、接続できませんでした

Windows ファイアウォールを介してアプリを許可した後でも。ポート 80、8080、13000 も試しました。

なぜこれは常に起こるのですか?どうすれば修正できますか?

私はこのチュートリアルに従っています: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient(v=vs.110).aspx#Y2523

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TCPTestClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        String msgToSend = "Hello, dude.";

        private void button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                Int32 port = 13000;

                TcpClient client = new TcpClient("localhost", port);

                Byte[] data = System.Text.Encoding.ASCII.GetBytes(msgToSend);

                NetworkStream stream = client.GetStream();
                stream.Write(data, 0, data.Length);

                Console.WriteLine("Send: {0}", msgToSend);

                data = new Byte[256];

                String response = String.Empty;

                Int32 bytes = stream.Read(data, 0, data.Length);
                response = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", response);

                title.Text = response;

                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex);
            }
            catch (SocketException s)
            {
                Console.WriteLine(s);
            }

            Console.WriteLine("Done");
            // All done.
        }
    }
}
4

1 に答える 1

1

リンクされたドキュメントは、問題のポートで実行され、接続を受け入れるサーバーがあることを前提としています。あなたのコードは単なるクライアントです。接続するサーバーが必要です。

Socketサーバーの実行方法については、クラスを参照してください。

于 2012-10-11T18:04:40.913 に答える