Solved

Socket

Posted on 2000-05-11
8
432 Views
Last Modified: 2010-04-01
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
Comment
Question by:JavaBoy060299
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2799822
java.net.*; a package that you must study if you want to play with sockets
0
 
LVL 4

Expert Comment

by:jerch
ID: 2799870
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2799904
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:JavaBoy060299
ID: 2802803
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
 
LVL 4

Expert Comment

by:jerch
ID: 2802877
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
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2804155
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
 

Author Comment

by:JavaBoy060299
ID: 2832246
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
 
LVL 3

Accepted Solution

by:
Laminamia063099 earned 300 total points
ID: 2832754
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 51
java jdbc batch example 8 40
servlet example 17 51
Tagging and Merging on Branch 1 29
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question