Skip to content

Sourabh-Harapanahalli/Socket_Programming

Repository files navigation

Socket Programming Project

Overview

This project demonstrates network socket programming in C by implementing a multi-threaded server and a client application. The server listens for incoming connections, handles multiple clients simultaneously using multi-threading, and provides an interface for users to manage an address book database. Clients can log in, search, add, update, or delete records, and even retrieve a list of active users. The system also supports administrative commands, including server shutdown.

The primary objective of this project is to explore:

  • Networking concepts like socket creation, connection handling, and message passing over TCP.
  • Multi-threading for concurrent handling of multiple clients.
  • User authentication and session management.
  • File I/O operations to persistently store and retrieve data from an address book.
  • Inter-process communication (IPC) through sockets for efficient data transfer.

This project is useful for understanding real-world client-server architectures used in networking applications, database services, and distributed systems.

Features

  • Multi-threaded server that handles multiple clients simultaneously.
  • Client-server communication over TCP sockets.
  • User authentication with login and logout functionality.
  • Address book management (lookup, add, delete, update records).
  • Graceful client disconnection and server shutdown support.

Files Included

Socket_Programming/
│── multiThreadServer.c  # Multi-threaded server implementation
│── sclient.c            # Client program to connect to the server
│── addressbook.txt      # Sample data file storing address book records
│── Makefile             # Makefile to compile the project
│── README.md            # Project documentation

Compilation

To compile the server and client programs, use the provided Makefile:

make

This will generate the following executables:

  • multiThreadServer
  • sclient

Alternatively, compile manually using:

gcc -pthread -o multiThreadServer multiThreadServer.c
gcc -o sclient sclient.c

Usage

Start the Server

Run the server on a machine:

./multiThreadServer

The server listens on port 8080.

Connect a Client

On another terminal or machine, connect a client to the server:

./sclient <server_ip>

Example:

./sclient 127.0.0.1

Client Commands

Once connected, clients can send commands:

  • LOGIN <user_id> <password> - Authenticate a user.
  • LOGOUT - Log out from the session.
  • LIST - View all address book entries.
  • LOOK <type> <query> - Search records by first name, last name, or phone number.
  • ADD <first_name> <last_name> <phone_number> - Add a new record.
  • DELETE <record_id> - Remove an entry.
  • UPDATE <record_id> <field> <new_value> - Update a record.
  • WHO - List active users.
  • QUIT - Disconnect the client.
  • SHUTDOWN - Shut down the server (Admin only).

Example Interaction

Client Side

Enter command: LOGIN john john06
Server: 200 OK

Enter command: LIST
Server: 200 OK
1001 John Doe 123-456-789

Enter command: QUIT

Server Side

Server started on port 8080
New connection from 127.0.0.1 on socket 4
Received from socket 4: LOGIN john john06
User 'john' logged in.
Received from socket 4: LIST
Sent address book data to client.
Received from socket 4: QUIT
Client on socket 4 disconnected.

Cleanup

To remove compiled binaries:

make clean

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published