TCP Network Communication [Code Design]

I have bothered with TCP / IP Communication many times over the past few days (using Java and C #). I understand how this works, and I can use it. My question is rather a question of code design, how to make it the best and easiest way to make a real connection.

For example, ive Created my own multi-user chat server. I want my message to be able to decide whether to send its Auth request or a new chat message to get the current list of users, etc. Etc.

Ive implemented several methods myself, but I'm not very happy about this, since I think this is a more standard and beautiful way to do this.

My first thought was String with delimiters that are separated, here is an example of my implementation of my post in Java:

//The Object-types im Using clientSocket = new Socket(host, port_number); _toServer = new PrintStream(clientSocket.getOutputStream()); _fromServer = new DataInputStream(clientSocket.getInputStream()); //Example Commands my Client sends to the server _toServer.println("STATUS|"); //Gets the Status if server is online or closed (closed can occur when server runs but chat is disabled) _toServer.println("AUTH|user|pw"); //Sends an auth Request to Server with username and Password _toServer.println("MESSAGE|Hello World|ALL"); //Sends hello World in the Normal Chat to all Users _toServer.println("MESSAGE|Hello World|PRIVATE|foo"); //Sends hello World only to the user "foo" _toServer.println("USERS|GET"); //Request a list of all Connected Users //Example In the Recieved Message Method where all The Server Messages Get Analyzed serverMessage = _fromServer.readLine(); //Reads the Server Messages String action = serverMessage.split("|")[0]; if (action.equals("USERS")) { //Example "USERS|2|foo;bar" String users[] = serverMessage.split("|")[2].split(";"); } if (action.equals("MESSAGE")) { //Example "MESSAGE|Hello World|PRIVATE|foo" if(serverMessage.split("|")[2].equals("ALL") { //Code and else for private.... } } if (serverMessage.equals("STATUS|ONLINE")) { // Code // I leave out //Code and } for the next If statements } if (serverMessage.equals("STATUS|OFFLINE")) { if (serverMessage.equals("AUTH|ACCEPTED")) { if (serverMessage.equals("AUTH|REJECT")) { 

Is this the way it is usually done? Ad You See I need to send status codes and objects corresponding to the code. Ive thought about writing data in bytes and implementing a "decoder for each object," for example:

 int action = _fromServer.readInt(); //opcodes is just an Enum Holding the corresponding int switch(action) { case(opcodes.MESSAGE): break; case(opcodes.AUTH): break; } 

Please note that this is a more general design. The question is not only for this chat server example, I think they need a small network console game just for practice.

Is there a better way to do this, or even an API / Framework?

Thanks in advance!

+4
source share
4 answers

In essence, you are developing a protocol. There are several communication protocols that can handle this, the main one being the IRC skill. I am sure you can do a web search for tips on how to implement the protocol.

As for expanding something like this for a console game, I would start by implementing IRC and use this to find out how the real communication protocols are written. Once you do this, you can build it to add your own teams to your infrastructure.

+2
source

If you are developing a protocol for interlanguage communication, I would suggest not using formatted strings as a means of communication, but status bytes. If you look at, for example, the design of TCP / IP itself, the messages consist of a fixed-format header and a payload variable. Thus, you always know that (for example) the third byte of the message contains the type of message, the fifth indicates an error, etc. This simplifies processing.

If you developed your protocol, you might consider working with explicit MessageObjects on the java side, in which case you would use a factory with sorting and disassembling methods for these objects, converting objects from and to messages in your protocol.

If you are all java, you can even get rid of these efforts and use ObjectInputStreams and ObjectOutputStreams on the client and server. If you do not, you can take a look at the Google protocol buffers: http://code.google.com/intl/de-DE/apis/protocolbuffers/ , which are essentially the same for interlanguage communications.

+2
source

If your project is growing, you can take a look at Netty - it is an environment for working with communication code. If your code is simple, you would be better off doing something manually.

As for the protocol design, it depends on what is most important to you: performance, extensibility, readability, ease of debugging, etc. These criteria may to some extent oppose each other, for example, high performance may mean preference for binary protocols, but they adversely affect ease of debugging and sometimes extensibility. This is usually a good idea not to reinvent the wheel. Get inspired from existing protocols. If you decide to go in binary mode, don't start from scratch, if you really don't need to, start with Protocol Buffers . If your application is simple and not aimed at very high performance, use a readable protocol that will simplify your life (debugging and testing are possible using standard shell tools such as strace and nc ).

+2
source

I think Apache MINA will help you. http://mina.apache.org/

Building a Java C / S application is really complicated, you need to deal with programming TCP, UDP and multiple threads; MINA can help you with this.

I think the other part you need is your private chat protocol, but what about an instant messaging service like Jabber? :)

+1
source

Source: https://habr.com/ru/post/1398585/


All Articles