title: Socket Communication between Delphi and Java
body: <p>I want to create a socket-communication between Delphi6 and Eclipse at localhost. The Delphi-Code seems to work, i can send data from Delphi-Server to Delphi-Client. Now i want to send data from same Delphi-Server to Java-Client. Following code i used from several examples. The connection between Delphi and Java seems established, but at the while-loop (stdIn.readLine()) the programm doesnt work anymore. There is no exception. The programm just stops to work. How can i read data from Delphi-server?</p>
  
  <p>Thanks!</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.Socket;
  
  public class JavaClientSocketEx {
      public static void main(String[] args) {
          try {
              Socket echoSocket = new Socket("127.0.0.1", 10003);
              PrintWriter out =
                  new PrintWriter(echoSocket.getOutputStream(), true);
              BufferedReader in =
                  new BufferedReader(
                      new InputStreamReader(echoSocket.getInputStream()));
              BufferedReader stdIn =
                  new BufferedReader(
                      new InputStreamReader(System.in));
              String userInput;
              echoSocket.close();
              while ((userInput = stdIn.readLine()) != null) { // at this point the programm doesnt work anymore!
                  out.println(userInput);
                  System.out.println("echo: " + in.readLine());
              }
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } 
      }
  }
  </code></pre>
  
  <p>edit: Delphi-code</p>
  
  <pre><code>unit Unit1;
  
  interface
  
  uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, ScktComp, ExtCtrls, StdCtrls;
  
  type
    CustomRec = record
      Zahl: byte;
  end;
  TForm1 = class(TForm)
      Edit1: TEdit;
      Timer1: TTimer;
      sendenButton: TButton;
      Edit2: TEdit;
      ClientSocket1: TClientSocket;
      ServerSocket1: TServerSocket;
      procedure Timer1Timer(Sender: TObject);
      procedure FormActivate(Sender: TObject);
      procedure sendenButtonClick(Sender: TObject);
      procedure ServerSocket1ClientError(Sender: TObject;
        Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
        var ErrorCode: Integer);
      procedure ServerSocket1ClientRead(Sender: TObject;
        Socket: TCustomWinSocket);
      procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
        ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    private
      { Private-Deklarationen }
    public
      { Public-Deklarationen }
    end;
  
  var
    Form1: TForm1;
    senden : boolean;
  
  implementation
  
  {$R *.dfm}
  
  procedure TForm1.Timer1Timer(Sender: TObject);
  var
  text : string;
  begin
  
          text :=  Edit1.Text;
          ClientSocket1.Socket.SendText(text);
  
  end;
  
  procedure TForm1.FormActivate(Sender: TObject);
  begin
          sendenButton.Caption := 'Send';
          senden := false;
          ClientSocket1.Port := 10003;
          ClientSocket1.Host := '127.0.0.1';
          ClientSocket1.Active := true;
          ServerSocket1.Port := 10003;
          ServerSocket1.Active := true;
  
  end;
  
  procedure TForm1.sendenButtonClick(Sender: TObject);
  begin
  
    if senden then
     begin
          senden := false;
          sendenButton.Caption := 'Send';
          Timer1.Enabled := false;
     end
    else
     begin
          senden :=true;
          sendenButton.Caption := 'Stopp';
          Timer1.Enabled := true;
    end;
  end;
  
  procedure TForm1.ServerSocket1ClientError(Sender: TObject;
    Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
    var ErrorCode: Integer);
  begin
  Errorcode := 0;
  end;
  
  procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
    Socket: TCustomWinSocket);
  var  Rec: CustomRec;
  begin
       edit2.Text := Socket.ReceiveText;
       rec.Zahl:=StrToInt(Edit2.Text);
       Socket.SendBuf(Rec,sizeof(Rec));
  end;
  
  procedure TForm1.ClientSocket1Error(Sender: TObject;
    Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
    var ErrorCode: Integer);
  begin
  Errorcode :=0;
  end;
  
  end.
  </code></pre>
  
  <p>The delphi-client starts to send data form first edit-Element (thats the input) periodicly in one-second-interval after I clicked on "sendenButton". The second edit-Element is the output of the received data. It stops to send, after I clicked this button again.</p>
  
  <p><a href="https://i.stack.imgur.com/Kgyvr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Kgyvr.jpg" alt="enter image description here"></a></p>