The best way to send data to a client-server application using Sockets


Hy, what i am trying to do is an Swing App(using sockets) on the client side that does something , and another App for the server(using serverSocket) which does all the database work according to the request.I have read about sockets and how to use them , my main concern is which is the best way to send data.

Lets just say that i have a collection of users and i want to send it to the client to be shown .Which is the best way to send it ?I thought about serializing the collection and sending it to the client , but i cannot cast to the type (java.util.ArrayList) (just an example) because i don`t know about the user class in the client App,and i cannot access the users specific methods.That raises the question :should I know about the Users in the client App? So any sugestions are good.

PS. For data retrieve i use Hibernate.

To expand on @JB Nizet, RMI would be easier. JMX would be easier still. Web services would be MUCH easier.

Basically, this problem is "solved" for the VAST majority of use cases, and you should be looking at those technologies and frameworks rather than raw sockets. Unless you have a really specific need (and most simply don't), sockets are a waste of time.

Web Services, POX over HTTP, Remote EJBs, Remote JMX, RMI and everything else deal with all of this for you. This wheel does not need to be reinvented.

Grab Glassfish or Tomcat + JAX-WS, and be done with it. Write some logic to solve a problem and make the world a better place instead of plumbing that's been figured out years ago.