Skip to content

CristianoSword/Server-C-Multi_thread-sockets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 

Repository files navigation

Multi-threaded C Socket Server System

A multi-threaded TCP server system in C demonstrating advanced concepts of concurrent programming and network communication.

Features

  • Thread Pool – Limit of 10 simultaneous threads controlled by a semaphore
  • LRU Cache – Thread-safe cache system with a Least Recently Used policy
  • Asynchronous Logging – Dedicated thread with a circular buffer of 1000 messages
  • Load Balancer – Round-robin distribution across up to 5 servers
  • Plugin System – Dynamic loading of DLLs (Windows) or .so files (Linux)
  • TCP Socket – Basic HTTP server on port 9090

Requirements

Windows: MinGW-w64 or MSVC, Winsock2 Linux: GCC/Clang, pthread, libdl

Compilation

# Windows
gcc server.c -o server.exe -lws2_32

# Linux
gcc server.c -o server -lpthread -ldl -Wall -O2

Execution

# Windows
server.exe

# Linux
./server

Test with: curl http://localhost:9090

Configuration

Edit the constants in the code:

#define MAX_THREADS 10          // Simultaneous threads
#define CACHE_CAPACITY 100      // Cache entries
#define SERVER_PORT 9090        // Server port
#define MAX_PLUGINS 10          // Maximum plugins

Structure

main()
  └─> socket_server()
       └─> connection_manager()
            └─> distributed_request_processing()
                 ├─> LRU Cache
                 ├─> Asynchronous Logger
                 ├─> Load Balancer
                 └─> Plugins

Creating Plugins

Linux (.so)

void plugin_init(void* context) { }
void plugin_process(const char* data, void* context) { }

Compile: gcc -shared -fPIC my_plugin.c -o my_plugin.so

Windows (.dll)

__declspec(dllexport) void plugin_init(void* context) { }
__declspec(dllexport) void plugin_process(const char* data, void* context) { }

Compile: gcc -shared my_plugin.c -o my_plugin.dll

Place the plugins in the ./plugins/ folder

Troubleshooting

Port in use (error 10013/EADDRINUSE):

# Windows
netstat -ano | findstr :9090
taskkill /PID <number> /F

# Linux
sudo lsof -i :9090
sudo kill <PID>

Permission denied: Run as administrator or switch to a port > 1024

Concepts Demonstrated

Concurrency: Threads, semaphores, mutexes, condition variables Networking: TCP sockets, basic HTTP protocol Architecture: LRU cache, producer-consumer, plugin system, load balancing Systems: Dynamic loading, signal handling, asynchronous file I/O

Windows vs Linux Differences

Feature Windows Linux
Threads CreateThread pthread_create
Mutex CRITICAL_SECTION pthread_mutex_t
Semaphore CreateSemaphore sem_t
Sockets Winsock2 POSIX sockets
Plugins LoadLibrary (.dll) dlopen (.so)

Performance

  • Requests/second: ~5000–10000
  • Average latency: <1ms
  • Simultaneous connections: 10 (MAX_THREADS)
  • Memory usage: ~5–10 MB base

Notes

Educational project demonstrating advanced techniques. For production use, add robust error handling, HTTPS, authentication, and rate limiting.

License

MIT License – Free for use and modification.

About

Server C Multi_thread with sockets

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages