Is it possible to send an object using Sockets, if so, how
it can be?
Answers were Sorted based on User's Feedback
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 |
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 |
How we can make simple java program for Server/Client Communication
What is the difference between TCP and UDP ?
What is the Difference between socket and servlet?
What is the difference between TCP/IP and UDP?
What is Domain Naming Service (DNS)?
Explain the generic term internet.
What is an http redirector?
What is jhttp web server?
Public serversocket(int port, int queuelength) throws ioexception, bindexception?
What are the advantages and disadvantages of sockets?
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.
how to know which version of java is running on ur computer using command prompt?