Networking - C Server-Client Kinds in Linux

in #network9 years ago

    Hello my friends! Today we will continue our talk about the Server-Client Model getting into the Kinds of Server and Clients that we can have. After talking about all that I will also get into an full-on example. So, without further do let's get started!


Quick Introduction:

    To get you in the topic of today's post I will first start off recaping and analysing what we said talked about until now. As I already said last time servers and clients communicate with each other using sockets. These sockets can be TCP or UDP that changes the reliability, having TCP be super reliable using packets in an "real" connection and UDP be faster in local networks, but unreliable with many datagram recasts. We also got into the code for an server and client of both protocols, but something is missing!

    What happens if 2 or more clients want to connect to the server? This question gets us into today's topic! It's not only about connecting, but a server (or even a client) needs to have some way of receiving and processing data from more than one sources. The kinds and types or servers and clients that come out of this question are a lot, but today I will try to talk about all of them or at least mention them.


So, lets get into Servers first!

Server Kinds:

    The server is the most essential part and we need to take a lot of parameters in mind to create the perfect server for each application. The 2 main categories are: Simultaneous (many requests at a time) and Repetitive (one request at a time) Server. A Simultaneous Server mostly uses Threads or Child/Slave Processes that process the requests. A Repetitive Server finishes a request before getting to the next one. We also split the server up in 2 other categories that are: stateless (server doesn't keep information about previous client connections) and stateful (server keeps information about client connections). I will not get more in depth into the stateless and stateful servers, but you should keep in mind that keeping information or not keeping information depends on the application. We also split up the Servers based on their protocol (TCP or UDP), but I will again don't talk about that, cause we already talked about it!

Let's now get into all those different Server Kinds and when they should be used!

The Server Kinds are:

  • Repetitive Server -> We process one request at a time and so this kind of server is working great when we have a small number of requests and the processing of a request doesn't take a lot of time. To do this we simply use a while loop where we accept connections and process the request.
  • Simulataneous Server using Threads -> We process each request in a separate thread and so can have many requests be processed at the same time. This is helpful when the processing takes a lot of time, time that is much more than the cost of creating a thread. So, the main thread that accepts connections will create a thread that "speaks" with the client using the created socket and then continue accepting more connections. We can also have a limit of Threads that we want to create.
  • Simulataneous Server using Slave Processes -> In the same way as using Threads, using Processes makes only sense when the processing time is much larger then the process creation cost. The main/master process will accept connections and then create slave processes that "talk" with the client using the socket created while accepting. We can again limit the number of processes.
  • Simulateneous Server using 1-Thread that manages multiple connections -> This type of server uses one process, but can handle many connections at the same time. We actually just accept many connections and put them in an array and then go through each of those requests one by one. So, this kind of server is practically efficient when the processing time is low, but we have a lot of requests coming inside of an repetitive server that causes a queue overflow. This implementation can be done using select and you can read about it here!

    All these Servers used only one protocol and one main application, but we can also have servers of multiple protocols and applications. Such a server has a socket for each of the protocols and applications and can then handle each of those in any way he likes! This kind of server is called a Super Server and the best example is inetd linux server daemon that you can check out here and lets us put any protocol/application into this server by simply adding it to the directory!


Client Kinds:

    The clients can follow almost the same fashion. A client mostly has many simulataneous connections with servers where each of those connections can be a multi or single-threaded connection. For example think about how a website is being loaded. Each element of the website is loaded by a separated thread and so the server creates many small threads that manage each one of those elements (text, pictures, video, audio etc.). So, a client can again use more than 1 protocols and applications. The most essential thing tho is that a client that connects to a stateless server mostly keeps data on his own! The cookies that we all know from the browsers work exactly in that way.


     Using all these things I talked about in the whole Networking series until now we can now create servers and clients keeping in mind many different parameters! I hope that you now have a better or a basic understanding on how the Internet works!

Example:

   To finish off this post I would like to put a full-on example using many things that we talked about creating multiple clients and a single-threaded multi-connection server using the select() function I talked about! This code is a task that I did for my university.

Task Description:

Suppose we have a buffer that can store 600 integer numbers. Create and synchronize 10 producers and 1 consumer having:

  • The Producers try writing to their corresponding places(the 1st from 1 to 60, the 2nd from 61 to 120, etc.) on the buffer that the Consumer stores. When a place has a number already present, it will be overwritten.
  • The Consumer will receive the numbers and the place where the number needs to be be put and then calculate the sum of those 10 regions (1 for each producer) at regular intervals. 

The communication with the consumer needs to be implemented with socekts. Use any protocol and type you want.

Me as a certified madman, to destroy the peace in my life, selected to create a TCP single-threaded server! :D


Code:


    As you can see I made pictures and thats what I will do from now on, cause steemit destroys my code and it gives me the creeps! :P


And this is actually and I hope you enjoyed this post! 

    We actually are now finished with what I wanted to show you in C Linux. Next time we will get into Sockets and Server-Clients in Java and then well we will see :)

Bye!

Sort:  

To point it out really quick...
This solution was actually really good and had a brilliant performance!
Cause, when a client connects he stays connected and becauses of the sleep/wait after a request, the server was not bombed with many requests at the same time. That way we saved in memory and processing sources, cause the application was semi-repetitive :)

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.078
BTC 65309.22
ETH 1721.00
USDT 1.00
SBD 0.41