Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

Socket

I want to make an applet that can display realtime data, but the data get from another application using Delhpi, from the Delphi Application directly access to SQL database.

I think I'll use socket for communication.
Please provide me the best solution and some code to explain it, and what I need such tutorial or etc.

 
 Thanks
0
JavaBoy060299
Asked:
JavaBoy060299
  • 3
  • 2
  • 2
  • +1
1 Solution
 
ovidiucraciunCommented:
java.net.*; a package that you must study if you want to play with sockets
0
 
Laminamia063099Commented:
Ok, in your Delphi program you must built in the capability to have a listening socket, on some port on the machine its on.  This must be created for the applet to communicate with the program.

In you applet, you must create a socket to connect to the port which the Delphi program is listening on.  Unless you have a signed applet, the Delphi program must be on the same machine that the applet was served from!  

       String host = new String("urlOfHost.com");
       int port = xxx; //port the delphi program is listening on.
        //Create socket
        socket = new Socket(host, port);
       
        //Create streams for reading and writing from/to socket
        sIn = new BufferedReader (new      
                                InputStreamReader(socket.getInputStream()));
        sOut = new PrintStream(socket.getOutputStream());

Now you have to initiate the talking between the two programs  by using the input and output streams to the socket that have been created on both ends.

I can't help you with the Delphi half.  I would suggest asking this question on that topic area as well.  I can help you with the Java side.  I'll post more code if you want!

Laminamia :)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
JavaBoy060299Author Commented:
Hi Laminamia,

Thanks for your quick response.
but I need more assist from you.

I want to know what is the best model, between the Applet request data from Delphi or Delhpi App send data to the applet if the data have changed.

Would you to redraw your answer ? so others can give their comments.
If none give better answer than you do, then points are yours

 Thanks
0
 
jerchCommented:
Hi JavaBoy...
Since you're using an applet that is connecting through a socket, bidirectional communication is possible.  So in this case, I would suggest that the server will be the one to notify the applet if data have changed.  This is better because it saves the client resources instead of the client spawning a thread which polls for updates from time to time. You would usually implement a client polling for updates if you have a request/response protocol such as HTTP such as if you have a servlet or cgi program in the server-side.


cheers...
Jerson
0
 
Laminamia063099Commented:
JavaBoy:

No problem.  I agree with jerch that the Delphi server would notify the client of the data updates.  

When using the socket for communication, whether the client requests or the server just sends the data, the client will still have to read from the socket regularly.  What I would suggest is: if the client does little other than display this data, have it sit it a loop reading from the socket.  When the server sends the data, it will be read from the socket with a call to sIn.readLine() or some such other Input call.  When there is no data in the socket to be read, the client will just wait.  This could be done in one thread, leaving other tasks to the client's main thread.

I can't say this is the best way to do it, only a possible solution.

Another way to set it up would be to have a separate class handle the socket connection and reading.  Whenever data is read, that class could call the class that takes care of the data display, notifying it that the data has arrived and passing it the data as needed.  If this is a way you would like to go, I can help you out here.  Again, it's just one possible solution.

Good luck :)

Laminamia :)
0
 
JavaBoy060299Author Commented:
Hi Laminamia,

I have changed my mind, Now I want both client/server site will be build by Java.

Can you give me some simple working examples, using MSSQL/Access db ?

Thanks

0
 
Laminamia063099Commented:
To access any database through java, all you need to use is the JDBC API (Java database connectivity).

Make sure that you've set up ODBC on your computer (do this in the Control Panel).  In the following example, say that your datasource is named: TestDatabase.

Now, in your Java program, import java.sql.*.

The following would make a small query to your datasource and display the results.

//Load the odbc driver...done once per session
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Create a connection
String datasourceURL = "";
String username = "johndoe";
String password = "something";
Connection conn = DriverManager.getConnection(datasourceURL, username, password);
//Create a Statement
Statement st = conn.createStatement();
//Execute SQL Query
ResultSet rs = st.executeQuery("Select colX from TableX");
//Now you can process the results from the Result set object
System.out.println("Processing result set....");
while (rs.next()){
  System.out.println("ColX Value: " + rs.getString("colX"));
}

This should do it!  If there are any problems with this code, drop a comment.

Good luck.

Laminamia :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now