开发者

Best way to send data in a client-server App 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) becaus开发者_如何学Ce 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.


To be able to deserialize an object, you need to have the concrete class of the object (and, recursively, of all the objects referenced by the object) in the classpath. So yes, the client needs the User class in its classpath.

Have you thought about using RMI instead of using plain sockets? This would probably be much easier.


You could serialize to some intermediat(xml,json,etc.) push that across the socket and bind back to java classes that your client understands.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜