Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Is it possible to send an object using Sockets, if so, how
it can be?

Answer Posted / roshan tiwari btech cs agra

Objects that implement Serializable may be sent across a socket connection using an ObjectInputStream and ObjectOutputStream combination.

Here are the steps to follow:

First, define an object to send. As an example, we can define a class called Message to encapsulate our communications:
public class Message implements Serializable {
private int senderID;
private String messageText;

public Message(int id, String text) {
senderID = id;
messageText = text;
}
public String getText() {
return messageText;
}
}
Next, instantiate the object, wrap the socket's streams in object streams, then send the message across the socket:
Message sayhey = new Message("123456789", "Hello");

Socket socket = new Socket(host, port);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());

out.writeObject(sayhey);
On the other side of the socket, the message can be retrieved and used by invoking methods on the returned object:
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
Message messageObject = (Message) in.readObject();
String messageText = messageObject.getText();

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is an http server?

952


Tell me about networking classes and interfaces.

1011


To create a socket, you need to know the internet host to which you want to connect?

908


Where is the network interface card located?

926


What is the main difference between Client side Java Script and and Server side Java Script how actually they run on both side with Example.

1046


Explain the generic term internet.

965


What is the Difference between socket and servlet?

1226


What is jhttp web server?

1171


What is network programming in java?

945


What is an http redirector?

935


Explain look for local ports?

1064


What is socket in java?

1016


What are the advantages and disadvantages of sockets?

1005


If you do not want your program to halt while it waits for a connection, put the call to accept( ) in a separate thread?

925


Why socketutil is used?

979