Solved

Socket

Posted on 2000-05-11
8
430 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
Comment Utility
java.net.*; a package that you must study if you want to play with sockets
0
 
LVL 4

Expert Comment

by:jerch
Comment Utility
0
 
LVL 3

Expert Comment

by:Laminamia063099
Comment Utility
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
 

Author Comment

by:JavaBoy060299
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:jerch
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Enhanced For loop vs ForEach Loop 4 54
count7 challenge 12 66
word0 challenge 4 52
Python Assistance 7 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now