0
I need to implement a communication via socket with server on Delphi and a Client in csharp.
I created an example and the same does the communication but for some reason the server in Delphi when will give the return to csharp locks the request and when I close the App csharp in the Delphi he continues with the Requisition, this example is a string
that I send an XML only to multiply the value and return twice the same.
Server - Delphi 7 using Tcpserver
procedure TForm1.TcpServerAccept(Sender: TObject; ClientSocket: TCustomIpClient);
var
rValor: Real;
sRequisicao: String;
begin
try
try
CoInitialize(nil);
sRequisicao := ClientSocket.Receiveln;
memStatus.Lines.Add(sRequisicao);
XMLDocument1.XML.Text := sRequisicao;
XMLDocument1.Active := True;
rValor := StrToFloat(XMLDocument1.ChildNodes.FindNode('reserva').ChildValues['valor']);
rValor := Self.Multiplicar(rValor);
XMLDocument1.ChildNodes.FindNode('reserva').ChildValues['valor'] := rValor;
sRequisicao := XMLDocument1.XML.GetText;
ClientSocket.Sendln(StringReplace(sRequisicao, sLineBreak, '', [rfReplaceAll]));
except on e: Exception do
begin
memStatus.Lines.Add('Erro: ' + e.Message);
ClientSocket.Sendln('Erro: ' + e.Message);
end;
end;
finally
CoUninitialize;
end;
end;
Client - Csharp - Example taken from MSDN
static void Send()
{
byte[] bytes = new byte[1024];
try
{
IPAddress ipAddress = IPAddress.Parse("192.168.1.164");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 6500);
Socket sender = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
sender.Connect(remoteEP);
Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString());
byte[] msg = Encoding.ASCII.GetBytes("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><reserva><valor>1</valor></reserva>");
int bytesSent = sender.Send(msg);
int bytesRec = sender.Receive(bytes);
Console.WriteLine("Echoed test = {0}",
Encoding.ASCII.GetString(bytes, 0, bytesRec));
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
No Csharp when I run and get on the Line:
int bytesRec = sender.Receive(bytes);
It triggers the server in Delphi and gets stuck on the line:
sRequisicao := ClientSocket.Receiveln;
Note: I have a Client in Delphi 7 and a Server in Csharp, between the same languages, communicate correctly.