coder9
coder9

Reputation: 1549

How to send and receive Java objects over Sockets?

I'm coding a concurrent ATM application having a Client and a Server.

I need to know how Socket programming can be used in this to call Server methods (request for his current balance, transfer funds etc) and get Objects returned to Client.

I do not want just to pass text using System.out.println like mentioned in Lesson: All About Sockets

Upvotes: 2

Views: 3832

Answers (4)

coder9
coder9

Reputation: 1549

I found this tutorial really helpful http://java.sun.com/developer/technicalArticles/ALT/sockets/

They say:

RMI = Sockets + Object Serialization + Some Utilities

Thanks guys for contributing!

Upvotes: 1

Jesper
Jesper

Reputation: 206816

Sockets are just a way to open a communication channel between a server and a client. A socket connection just enables you to send bytes from one machine to the other, nothing more. In particular, sockets do not directly provide a way to call methods on another machine.

You will need to implement an application-level protocol on top of sockets, or use some existing (standard) application-level protocol. You could use RMI as suggested by ashiaka, or for example JAX-RS (Java's standard API for RESTful web services). Both these will use sockets to get the data across from one machine to the other.

So yes, it is possible using sockets, but you need to use something on top of that to actually remotely call methods.

Upvotes: 3

Jonas
Jonas

Reputation: 128827

You need to serialize your data, send it, and deserialize it. See Lesson: Basic I/O for an introduction to the Java serialization classes.

Upvotes: 4

ashiaka
ashiaka

Reputation: 4064

You should use Javas RMI Lib for this.

You can find a small example here: http://download.oracle.com/javase/1.3/docs/guide/rmi/getstart.doc.html

Upvotes: 4

Related Questions