I want to create a chat application, so I need to create a specific packet format .
for example)
lets consider that pkt
is the structure
pkt {
type_no;
msg;
dest_ID;
.
.
.
.
}
Is it a good way to create a packet class and send the whole object? like:
public class Packet implements Serializable {
private static final long serialVersionUID = 1L;
private int type;
private String data1;
private String data2;
private String clientID;
private String destID;
private boolean hasFile=false;
private boolean hasList;
private byte[] file ;
private HashMap <String,String> onlineList = new HashMap<>();
.
.
.
.
.
Or there is a better way to handle this?
Thanx
Method for sending class via socket
for example:
public class packet implements Serializable {
String msg;
int no;
int Id;
String info;
}
create new object
packet pkt = new packet();
pkt.msg="HI";
pkt.Id=2;
.
.
.
For sending use
// simple code
ObjectOutputStream outputStream;
outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.writeObject(pkt);
socket.close();
For receiving use
// simple code
ObjectInputStream inStream;
inStream = new ObjectInputStream(socket.getInputStream());
packet rcvPkt = (packet)inStream.readObject();
socket.close();
you will have the information in rcvPkt. I hope that this answer helps.