【问题标题】:A request to send or receive data was disallowed because the socket is not connected不允许发送或接收数据的请求,因为套接字未连接
【发布时间】:2013-07-31 03:08:30
【问题描述】:

我正在尝试使用 WCF 代码中的 TCP 套接字连接到远程外部服务器 我的 WCF 服务是具有使用套接字连接到外部服务器的代码的客户端。 此代码向外部服务器发送请求并接收服务器响应

int byteCount = 0;
        Socket m_socClient;
        try
        {

            string query = "My Request String";
            m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse("127:0:0:0");
            System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, 1234);
            EVCommon.Log("COnnecting to" + IPSelected + Port);
            m_socClient.Connect(remoteEndPoint);
            try
            {
            if (m_socClient.Connected)
            {
                EVCommon.Log("Connected to" + IPSelected + Port);   
                var reQuestToSend = string.Format("POST /ZZZZZ HTTP/1.1\r\nContent-Length:{0}\r\n\r\n{1}", query.Length, query);
                byte[] bytesToSend = Encoding.ASCII.GetBytes(reQuestToSend);
                byteCount = m_socClient.Send(bytesToSend, SocketFlags.None);
                byte[] bytesReceived = new byte[1024];
                byteCount = m_socClient.Receive(bytesReceived, SocketFlags.None);
                Response271 = Encoding.ASCII.GetString(bytesReceived);
                m_socClient.Disconnect(false);
                m_socClient.Close(5000);
            }
            }
            catch(Exception ex)
            {
                EVCommon.Log(ex.Message);

            }


        }

如果我用相同的客户端代码制作一个windows应用程序来连接远程服务器,它是成功的。我能够连接、发送和接收 仅当我将 WCF 带入图片时才会出现此错误。代码在 if(m_socket.Connected) 处失败。所以无法连接成功。

A request to send or receive data was disallowed because the socket 
is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. 

谢谢

【问题讨论】:

    标签: wcf sockets tcp


    【解决方案1】:

    区别在于windows应用程序以登录用户的身份运行,而WCF服务以应用程序池的身份运行。

    可能发生的情况是应用程序池作为 NETWORK SERVICE 运行,它无权打开端口。

    尝试将应用程序池的身份更改为您的用户,以检查这是否是问题所在。

    【讨论】:

      猜你喜欢
      • 2014-12-20
      • 2014-08-30
      • 2013-02-03
      • 2013-11-24
      • 2021-08-16
      • 1970-01-01
      • 2018-07-01
      • 2018-06-20
      • 2021-10-11
      相关资源
      最近更新 更多