Java - Simple Network

After reading a few web pages about the network, trying to understand basic network technologies (I had never worked on the network before), I made several classes that are designed to create a simple chat. The following classes will be placed below:

ChatServer: http://paste.strictfp.com/32591 ((Recently edited) Creating an actual server that waits for people to connect to it, in this case, through port 9045)

ChatSession: http://paste.strictfp.com/32583 (When a client is found, from the server code above, it creates a new session, which simply basically reads messages sent by clients)

ChatClient: http://paste.strictfp.com/32584 (Allows the client to write to the server)

ServerRunner: http://paste.strictfp.com/32585 (Basic server startup method)

ClientRunner: http://paste.strictfp.com/32586 (The main method for starting a client that connects to the server)

I know that the code above is not the best, given the fact that I did not add any checks to check if the socket / client was disconnected or something was interrupted. But then again, it was just a practice to help me try to understand the concept of networks.

So these 5 classes work together fine, but I have a question / concern that would be very helpful if he answered:

How to send a message from the server to the client?

The reason I ask is because I want to create a simple tic-tac-toe multiplayer game that will have a server and 2 clients (both representing players) and basically what I had in mind was that whenever clients click on the buttons, I send a message to the server. And then I will send a message to both clients to change both games. And I'm a little confused about how this works, due to my lack of network knowledge. I would appreciate it if you did not redirect me to another URL, if not very, very simple, because I prefer people to explain this to me in simple words. If someone can help me, he will be very grateful.

+4
source share
2 answers

There are two meanings for the word server that can be confusing:

  • The server is what the client does not have (i.e. it is a central hub to which each client / user connects)
  • A server is one that receives requests. Servers never initiate conversations; they always respond to incoming messages.

So what you are trying to do is turn your server into a client (and ChatClient will become a server).

This explains why this seemingly simple task is so complex: servers are not designed to talk with clients. An example in the real world: a clerk from your bank suddenly appears on your doorstep and tries to sell you something at home. Odd, right?

Solutions:

  • You can send a message to the client on the server every few minutes. The message will be "Is there something new?". This is called a "survey."
  • You can send a message to the client and wait for a response. The server does not need to immediately respond to messages. However, there will be time required for processing. But the client can send the message "Is there anything new?" and then wait for an answer. If you move this to a stream, the client can do something else while it waits for a response. This is a survey, but it is passive.
  • You can use a bidirectional protocol like WebSocket .
  • You can turn your client into a real server: Open the server socket, let the central hub know the port number and let it connect to it to update your client. Although this works, he needs two connections, and they are a little expensive. This works well for small customers, but as the number grows, it eventually becomes a problem.
+1
source

Before moving on, you need to decide how to structure the protocol at the application level over regular TCP sockets. It is about which side initiates the conversation, which messages you want to send back and forth, etc.

Then the next often sticky dot determines the layout of the messages themselves - whether they should be text or binary, and how the receiving party finds out if it received the full message. The usual confusion here is not to realize that a TCP connection gives you a bi-directional stream of bytes, and you need to figure out where the messages start and end. Traditional options:

  • Delimited messages - best suited for text protocols. The simplest form is a line-to-message designation, where your line is a new line.
  • Message with a fixed length. Best for binary files.
  • Messages with a length prefix - you have a fixed-length header that tells you how many bytes will follow.
  • Self-describing messages such as s-expressions, XML, etc. Usually text.

Hope this helps a bit.

0
source

All Articles