?
Solved

Socket

Posted on 2000-05-11
8
Medium Priority
?
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 900 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 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