Coder Social home page Coder Social logo

tcp-ip-server's Introduction

Server

A tcp/ip server application that supports concurrent connections from clients enabling them to request file downloads.

Project structure

At present, all the source files are within a single directory called Src to keep things simple. Though at some point it'd be better to split the header and source files into two seperate folders e.g. include and src.

Server Configuration

  1. In file_transfer.h
#define FILE_TRANSFER_NAME_SIZE_MAX 32 + 1                      // Maximum size supported for the requested filename
#define FILE_TRANSFER_TABLE "/home/vinay_divakar/file_storage"  // files storage path
#define FILE_TRANSFER_PATH_NAME_SIZE_MAX 64                     // Maximum size supported for the file path
#define FILE_TRANSFER_BUFF_READ_SIZE 32                         //  Maximum size supported for buffer reads
  1. In server.h
#define SERVER_SOCKET_LISTEN_INDEX 0        // listening socket index in poll fds descriptor set
#define SERVER_SOCKET_LISTEN_PORT_NUM 12345 // listening socket port number to which clients request connection
#define SERVER_SOCKET_POLL_TIMEOUT -1       // poll timeout set to block indefinetly if not events occur
#define SERVER_CONNECTIONS_BACKLOG 5        // maximum connections to be queued to be serviced
#define SERVER_STATE_MACHINE_FDS_MAX 3 + 1  // maximum number of connections supported 

Building the project

  1. In the Makefile, update the SRCDIR variable to point the path project is located on your local machine.
SRCDIR = /home/vinay_divakar/tcp-ip-server/src
  1. Run make from within the project's root i.e.Server to build your project. This will generate an executable called server_app.

Running the application

  1. Run the server_app executable and the below shows the server application is running & has started listening for connections!
vinay_divakar@vinay-divakar-Linux:~/Server$ ./server_app 
server listening on port 12345 using socket fd 3
  1. You could use the client program to test the server OR tools such as telnet.
  2. For debug purposes or visiblity, you can enable/uncomment the below line in file_transfer.c within the function file_transfer(). This prints what's being sent over the socket.
// enable to see whats being sent out
// printf("content: %.*s\r\n", err, (char *)buffer);

How it works

To ensure the server supports concurrent and services concurrent connections, there are a few ways to about it. The most common one's are using the select() or poll() functions. For this application, I decided to use poll() considering a few advantages it has over select() and also simplifies the software design. There also seems to epoll which is believed to offer much better performace, however, for this case, poll should be good enough.

The application has five states(shown below) to manage all the operations and present in the server_state_machine.h.

enum server_state_t {
  SERVER_LISTEN_BEGIN,
  SERVER_POLL_FOR_EVENTS,
  SERVER_POLL_INCOMING_CONNECTIONS,
  SERVER_PROCESS_CONNECTION_EVENTS,
  SERVER_FATAL_ERROR
};
  1. SERVER_LISTEN_BEGIN: Initializes the variables, sets up a socket and starts listening for incoming connections. If no errors, transistion to state(2) else to state(5).
  2. SERVER_POLL_FOR_EVENTS: Polls for events on all the active sockets i.e. listening and any active connections. If no errors, transistion to state(3) else to state(5).
  3. SERVER_POLL_INCOMING_CONNECTIONS: Handles incoming connection request events from clients and adds them for poll to monitor for events. Check for events on listening socket and if POLLIN, accept the connection and on success, transisition to state(4). On connection acceptance errors, transisition to state(5).
  4. SERVER_PROCESS_CONNECTION_EVENTS: Processes events generated on all active connection such as socket read, socket write or socket errors. If all events are processed without errors, transition to state(2) else to state(5).
  5. SERVER_FATAL_ERROR: Handles unexpected errors and exits on a failure.

State transistion diagram

TBD

tcp-ip-server's People

Contributors

deeplyembeddedwp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.