Connecting Delphi & JBuilder
Posted: (EET/GMT+2)
Connecting Delphi & JBuilder
Aug 26, 2000
Delphi 5 Professional & Enterprise,
JBuilder 3.5 Professional & Enterprise
When you are building applications for the Internet, it really doesn't matter which modern software development tool you are using. If you are focusing on advanced distributed applications, it is helpful to learn how to make applications written in different programming languages interoperate. In this article you will learn how to make a Delphi application communicate with a JBuilder Java application.
On the Internet, TCP/IP is the protocol of choice. As almost all software tools nowadays support TCP/IP, it is easy to create an application for example with JBuilder and have it communicate with a Delphi application. This way, you could use the most appropriate tool for the given task.
For example, assume you were building a Java application for a handheld appliance, which must communicate to a server application running on Windows. Borland Delphi is one of the best tools currently available for Windows development, so you might use that as your server development tool. On the other hand, you could use JBuilder to develop the Java client application.
Let's see how this might work in practice.
Start with the server
As with all client/server applications, it is generally best to start development from the server application. So, start up your copy of Delphi, and drop a TServerSocket component on the form. The example application represented in this article simply sits and waits for a TCP connection from the port 1234.
Whenever it receives a command with a CRLF character sequence, it sends a simple reply string to the client. Given these specifications, the example server only hooks four events of the TServerSocket component, namely OnClientConnect, OnClientDisconnect, OnClientError and most importantly, OnClientRead.

The Delphi source code for the server application's OnClientRead event handler looks like this:
procedure THelperFrm.ServerSocketClientRead(Sender: TObject; Socket: TCustomWinSocket);
Const Response = 'Hi JBuilder, this is Delphi!';
begin
Query := Query+Socket.ReceiveText;
If (Pos(#13#10,Query) > 0) Then Begin { wait until CRLF }
SetLength(Query,Length(Query)-2); { remove CRLF }
Log.Lines.Insert(0,'Client says: '+Query);
Log.Lines.Insert(0,'Responding: '+Response);
Socket.SendText(Response);
End;
end;
The Java client
After you have the server application compiled and running, it is time to write the client. The example client application was written with Borland JBuilder 3.5, but you should be able to compile it with any Java development tool that supports JDK 1.2.
The code uses the Socket class from the java.net package. To read and write data from the socket, BufferedInputStream and DataOutputStream stream classes are used. There are also other possibilities, so you might want to browse through the Java documentation.
Without too much talk, the code for the client is:
import java.io.*;
import java.net.*;
public class SimpleTCPIPApp {
//Construct the application
public SimpleTCPIPApp() {
}
//Main method
public static void main(String[] args) {
try {
Socket mySocket;
BufferedInputStream isSocket;
DataOutputStream osSocket;
byte[] bBuffer = new byte[256];
int len;
System.out.println("Connecting a Delphi application on port 1234.");
mySocket = new Socket("localhost",1234);
isSocket = new BufferedInputStream(mySocket.getInputStream());
osSocket = new DataOutputStream(mySocket.getOutputStream());
System.out.println("Saying \"Hello\" to server.");
osSocket.writeBytes("Hello\r\n");
System.out.println("Waiting for server to reply.");
len = isSocket.read(bBuffer);
System.out.print("Server said: ");
for (int i = 0; i < len; i++) System.out.print((char)bBuffer[i]);
System.out.println("\r\nClosing down.");
isSocket.close();
osSocket.close();
mySocket.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Running the client
After you have compiled the client, it is time to run it. Make sure that you have the server running on the same machine as the client application because the client assumes that the server is running on the "localhost". If the server is running on a different machine, adjust the Socket() constructor call on the client accordingly.

When you run the client on a command prompt, you should see output something to the following:
M:\WhirlWater\classes>java SimpleTCPIPApp Connecting a Delphi application on port 1234. Saying "Hello" to server. Waiting for server to reply. Server said: Hi JBuilder, this is Delphi! Closing down.
As you can see, it very easy to get Delphi and JBuilder applications communicate! Of course, you might easily extend the application to do something useful, but nonetheless you get the idea how to make different applications communicate with each other.
Download the example code
Download connectingdelphiandjbuilder.zip (173 kB) which contains the sample applications SimpleTCPIPApp and SimpleTCPIPServer. Please note that the client application will require at least JBuilder 3.5 Professional or better. The server application will require Delphi 5 Professional or better.
* * *
Need help developing your JBuilder or Delphi application? Contact us!