简体   繁体   中英

Java how to marshall code on a network message

This is a chat program that has a server and client side. The user sends a message to the server dictating what it is that the user wants to do. I am asked to marshall and unmarshall code for each of the message identifiers. I currently have an enumator containing every message along with the id value in the Server and Client classes. I also have an interface that is being implemented by every message identifier class. There are 7 message identifiers: REGISTRATION_MESSAGE, REGISTRATION_RESPONSE, DEREGISTRATION_MESSAGE, REQUEST_REGISTERED_USERS, RESPONSE_REGISTERED_USERS, BROADCAST_MESSAGE, and WHISPER_MESSAGE. All in all, I believe that if I can figure out the first message, I can do the other ones pretty easily. I am struggling to understand the concept, along with the know-hows for marshalling and unmarshalling. Currently, this is the snippet of code I am currently struggling on. code snippet

I've never messed with bytes so that alone kind of confuses me. Oh, and also I suppose it might would be useful to show my interface, interface code snippet , along with my enumator declartion (in both Server and Client classes, but for the case of showing you I'll show just one since they are the same) enumator code snippet

Of the Socket.InputStream you create a DataInputStream, which lets you read short, int, long etc. Use short or readUTF to read the ordinal/String for the message type, then switch on it, let each of the message type implementation handle its own I/O from there (best done in individual methods).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM