I have an application that needs to connect via a socket to a server, send an XML request, then receive a response. Sounds pretty simple, right? FWIW, I've never used sockets before.
The vendor of the server has given me sample C#.NET code for connecting/sending/receiving. That's great, and it works great (I've tested it, and it does exactly what it's supposed to do). However, we're using Delphi 5 for this project, and I'm trying to avoid writing a .NET dll that would have to be called from the Delphi app... I'd rather just handle the whole thing in Delphi. We've already had the Indy components for other projects, so I decided I'd give those a go for this.
The problem I'm having is that I can see the XML request I'm sending to the server, but I'm not getting a response back from the server until I've disconnected completely (I can see all of this using Wireshark). When I try to read the response from the server, I get nothing.
The one thing I notice in the .NET socket code is that there is a method that allows you to disconnect the sending portion of the socket. That is what seems to fire the server socket's response. I can't find a method with the Indy components that seems to fit this bill.
Anyone have any suggestions?
Here's my Delphi code:
Here's the working C# code:
The vendor of the server has given me sample C#.NET code for connecting/sending/receiving. That's great, and it works great (I've tested it, and it does exactly what it's supposed to do). However, we're using Delphi 5 for this project, and I'm trying to avoid writing a .NET dll that would have to be called from the Delphi app... I'd rather just handle the whole thing in Delphi. We've already had the Indy components for other projects, so I decided I'd give those a go for this.
The problem I'm having is that I can see the XML request I'm sending to the server, but I'm not getting a response back from the server until I've disconnected completely (I can see all of this using Wireshark). When I try to read the response from the server, I get nothing.
The one thing I notice in the .NET socket code is that there is a method that allows you to disconnect the sending portion of the socket. That is what seems to fire the server socket's response. I can't find a method with the Indy components that seems to fit this bill.
Anyone have any suggestions?
Here's my Delphi code:
Code:
with IdTcpClient1 do
begin
Connect;
//CheckServerResponse is the XML string being sent
WriteBuffer(CHECKSERVERRESPONSE, Length(CHECKSERVERRESPONSE), True);
Sleep(1000);
ReadBuffer(Resp, Length(Resp));
//Resp comes back as an empty string
Disconnect;
//At this point, I can see in Wireshark that the server has sent the response I am expecting.
end;
Here's the working C# code:
Code:
ssSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPAddress hostadd = Dns.Resolve("192.168.40.106").AddressList[0];
IPEndPoint EPhost = new IPEndPoint(hostadd, 2077);
ssSocket.Connect(EPhost);
//variable 'request' constaints the request XML
ssSocket.Send(ASCIIEncoding.ASCII.GetBytes(request));
ssSocket.Shutdown(SocketShutdown.Send);
//Wait for ES Server to send response
int timeOut = 180 * 100000;
if(!ssSocket.Poll(timeOut, SelectMode.SelectRead))
{
throw new Exception("Could not read from SS service. Timed out.");
}
const int iBufferLen = 4096;
byte[] buffer = new byte[iBufferLen];
int iRead = 0;
MemoryStream stResponse = new MemoryStream();
while(0<(iRead = ssSocket.Receive(buffer)))
{
stResponse.Write(buffer, 0, iRead);
}
//Convert response to a plain string xml
string response =
ASCIIEncoding.ASCII.GetString(stResponse.GetBuffer(), 0,
(int)stResponse.Length);
richTextBox1.Text = response;