The first time I write sockets in a normal language and these are the questions I have. Previously, I created a server and for each client I created a copy of server logic, which allowed me to communicate directly with each client separately. But it was not code, but graphics, i.e. I made the necessary functionality from cubes, and the code itself remained hidden for me. The process of creating a copy of server logic for me was not fully disclosed.

It is possible to explain in detail with ordinary words what follows what. For example, I created a socket on the n port, the first client connected, how to "move" it to its copy of the logic? How to communicate with each client in a separate thread?

  • Well, I also created something, connected, how to get the data now, without driving logic through some kind of while who are constantly looking at whether the data came - hitman249
  • 2
    We need some kind of event framework for the socket server. An interesting article is also from [Habra] ( habrahabr.ru/post/128772 ), it is of course about the very idea of ​​creating such frameworks, but also in it you can find the names of already finished products for Java. - ReinRaus
  • Is it worth doing this for the sake of ~ 70 clients from which the text will simply twitch periodically? I mean Netty - hitman249
  • No, you can make a server in 15 minutes that will do it without any springs, servlets, applets ...... - Gorets
  • one
    Gorets, very informative, and most importantly, reasoned! - hitman249

2 answers 2

In the simplest case, the servlet listens to the port with a non-blocking socket. When a client connects, the servlet starts processing this socket in a separate thread, and starts listening again.
I recommend to study work on the example of a simple code with Habra .

    Somewhere I came across a good article, but I can’t find it right away. Here is an example, just do not call the class with a small letter;)

    Thread Pro Sockets Article

    • one
      voo another thing, a short code and the case, but it is very annoying to shovel a ton ..... for the sake of 3 lines of code hidden somewhere in the middle. - hitman249
    • Link is rotten. - Eugene Krivenja