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?

Answers were Sorted based on User's Feedback



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

Answer / afzi

Yes it is Poosible to send an Object using Sockets,

for example refer to below given code snippet.

ObjectOutputStream out = new ObjectOutputStream
(socket.getOutputStream());
out.flush();
ObjectInputStream in = new ObjectInputStream
(socket.getInputStream());
type=(String)in.readObject();
out.writeObject("ok");
in.close();
out.close();
socket.close();

Is This Answer Correct ?    6 Yes 1 No

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

Answer / 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

More Java Networking Interview Questions

How does server know that a client is connected to it or not?

1 Answers  


What is URL?

6 Answers   Wipro,


What is network interface name?

0 Answers  


where u can used in real time "protect" specifier or real world example of protect specifier

1 Answers   CTS,


What are the advantages and disadvantages of java sockets.

0 Answers  


Write the range of multicast socket IP address?

1 Answers  


What is the difference between TCP/IP and UDP?

5 Answers  


Explain a time server?

0 Answers  


can any one tell me how to implement tcng (Traffic Control Next Generation) with HTB (Hierarchical Token Bucket) in Java?which particular class or API is used for that?

0 Answers   Accenture,


Tell me about networking classes and interfaces.

0 Answers   Akamai Technologies,


What is meant by TCP, IP, UDP?

11 Answers   TCS, Wipro,


What is socket in java?

0 Answers  


Categories