Communication Problems Delphi Client Csharp Server Socket

Asked

Viewed 285 times

0

I need to implement a communication via socket with server on and a Client in .

I created an example and the same does the communication but for some reason the server in when will give the return to locks the request and when I close the App in the 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.

1 answer

0

I discovered that the problem was in the component I was using in Delphi to create the server, I changed the Tcpserver Component to the Socketserver both from the Internet palette, the code was practically the same, only changed the lines

sRequisicao := Socket.ReceiveText;
Socket.SendText(sRequisicao);

And on the part of csharp to facilitate I used the package Socket which has become simpler to use.

using (var socket = new ConnectedSocket("127.0.0.1", 6500)) 
{
     socket.Send("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><reserva><valor>10</valor></reserva>");
     var data = socket.Receive(); 
     Console.WriteLine(data);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.