我正在尝试使用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.
谢谢
答案 0 :(得分:0)
不同之处在于,Windows应用程序以登录用户的身份运行,WCF服务以应用程序池的标识运行。
可能发生的情况是应用程序池作为NETWORK SERVICE运行,无法打开端口。
尝试将应用池的标识更改为您的用户,以检查这是否是问题。