Hello, everybody
I am working on a simple client-server program. The server side is a console program which will send (1)the list of files in home directory and (2)the contents of the selected file to the client side. And the client side is a GUI program which sends requests to receive the list of files in the servers home directory and after it received the list of files it shows that list to the user and the user has an opportunity to view thr content of the selected file. The problem is that after sending the list of files because of the garbage collection the connection between the server and client is closed and there is no possibility for the client to request the contents of the selected file from the server. How to owercome this problem? Please, give some hints???

Recommended Answers

All 2 Replies

The garbage collector won't free objects that have still a reference. If it does, and the hell if I know how you figure that out, it is because your code does not have any more links to these objects.
Unless you show your code, there is not much we can say. If the connection is closed (by your code) there is no need to still access these objects.

I doubt your connection is closed because of garbage collection. It is probably closed because your connection is established, your code runs, your method exits, and the program is finished so it closes Sockets etc. Garbage collection does happen, but isn't the cause of your issue. In order to keep your connection alive you'd need to have a while loop or some other loop.

See http://download.oracle.com/javase/tutorial/networking/sockets/examples/KnockKnockClient.java for an example of a connection that would stay open.

See
http://download.oracle.com/javase/tutorial/networking/sockets/ to learn about Sockets in Java.

You should probably implement a system whereby the client/server can have slightly more advanced communication, e.g. client tells server "I want the list of files" then waits for the server to send them, etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.