第二次连接到套接字失败

时间:2017-07-13 11:57:19

标签: c# sockets azure actor azure-service-fabric

我制作了一个服务结构应用程序,它使用actor服务通过Socket与客户端应用程序一起创建消息通道。

客户端应用程序运行异步服务器套接字,如本示例https://technet.microsoft.com/es-es/library/fx6588te(v=vs.85).aspx,我的actor使用客户端IP地址连接它,就像在这个方案中一样:

public static void StartClient(byte[] i_address, string i_message)
{
        // Connect to a remote device.
        try
        {
            ActorEventSource.Current.Message("Creating socket");
            // Establish the remote endpoint for the socket.
            IPEndPoint remoteEP = new IPEndPoint(new IPAddress(i_address), port);
            ActorEventSource.Current.Message("IPAddress created");
            // Create a TCP/IP socket.
            Socket client = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
            ActorEventSource.Current.Message("Client created");
            client.Connect(remoteEP);
            ActorEventSource.Current.Message("Client connected");
            byte[] byteData = Encoding.ASCII.GetBytes(i_message);

            ActorEventSource.Current.Message("Sending: " + i_message);

            client.Send(byteData);

            ActorEventSource.Current.Message("SENT");

            // Release the socket.
            client.Shutdown(SocketShutdown.Both);
            client.Close();

        }
        catch (Exception e)
        {
            ActorEventSource.Current.Message(e.ToString());
        }
}

当它使用本地IP地址运行时,它可以正常工作,但是当它使用公共IP地址工作时,它可以正常工作一次。

client.Connect(remoteEP);上的第二次连接尝试失败

  

System.Net.Sockets.SocketException(0x80004005):连接尝试   失败,因为关联方在a之后没有正确回应   一段时间,或建立的连接失败,因为连接   主机失败

0 个答案:

没有答案
相关问题