{"id":28717150,"url":"https://github.com/shoryasethia/socket-programming","last_synced_at":"2025-06-15T03:13:51.193Z","repository":{"id":298589824,"uuid":"1000483829","full_name":"shoryasethia/socket-programming","owner":"shoryasethia","description":"Bidirectional chat app and simple file transfer using Socket Programming in C++ using the Winsock2 API on Windows.","archived":false,"fork":false,"pushed_at":"2025-06-11T21:35:51.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-11T22:38:28.995Z","etag":null,"topics":["chat-app","client-server","cpp","file-transfer-protocol","socket-programming","tcp-socket"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shoryasethia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-11T21:25:07.000Z","updated_at":"2025-06-11T21:49:19.000Z","dependencies_parsed_at":"2025-06-11T22:38:33.415Z","dependency_job_id":"7da56cb0-267e-45c4-9db2-f65fdccff237","html_url":"https://github.com/shoryasethia/socket-programming","commit_stats":null,"previous_names":["shoryasethia/socket-programming"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/shoryasethia/socket-programming","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shoryasethia%2Fsocket-programming","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shoryasethia%2Fsocket-programming/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shoryasethia%2Fsocket-programming/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shoryasethia%2Fsocket-programming/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shoryasethia","download_url":"https://codeload.github.com/shoryasethia/socket-programming/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shoryasethia%2Fsocket-programming/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259914931,"owners_count":22931334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chat-app","client-server","cpp","file-transfer-protocol","socket-programming","tcp-socket"],"created_at":"2025-06-15T03:13:24.169Z","updated_at":"2025-06-15T03:13:51.119Z","avatar_url":"https://github.com/shoryasethia.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Socket Programming in C++\r\n\r\nSocket programming in C++ using the **Winsock2 API** on Windows. It includes:\r\n* 📬 A **bidirectional chat app** where server and client can both send and receive messages.\r\n* 📂 A **simple file transfer protocol** where server and client can send files to each other.\r\n\r\n## 🧠 C++ Socket Functions Summary\r\n\r\n| Component       | Description                                             |\r\n| --------------- | ------------------------------------------------------- |\r\n| `socket()`      | Creates a socket descriptor.                            |\r\n| `bind()`        | Binds socket to an address and port.                    |\r\n| `listen()`      | Prepares the socket to accept incoming connections.     |\r\n| `accept()`      | Accepts a connection from a client.                     |\r\n| `recv()/send()` | Receives/Sends data over the connection.                |\r\n| `close()`       | Closes the socket (POSIX) or `closesocket()` (WinSock). |\r\n\r\n## 🔄 Differences Between POSIX and Windows\r\n\r\n| Feature        | POSIX (Linux)                      | Windows (WinSock)              |\r\n| -------------- | ---------------------------------- | ------------------------------ |\r\n| Header files   | `\u003csys/socket.h\u003e`, `\u003cnetinet/in.h\u003e` | `\u003cwinsock2.h\u003e`, `\u003cws2tcpip.h\u003e` |\r\n| Socket close   | `close()`                          | `closesocket()`                |\r\n| Initialization | No init needed                     | `WSAStartup()`                 |\r\n\r\n---\r\n\r\n## ✅ Requirements\r\n\r\n* Windows OS\r\n* MinGW-w64 with GCC \u003e= 11 (`g++ --version` to check)\r\n* `WSAStartup()` and `closesocket()` for WinSock API\r\n* Compile using `-lws2_32`\r\n\r\n---\r\n\r\n## 📁 Directory Structure\r\n\r\n```\r\nsocket-programming/\r\n│\r\n├── chat-app/\r\n│   ├── server.cpp           # Chat server source\r\n│   ├── client.cpp           # Chat client source\r\n│\r\n├── file-transfer/\r\n│   ├── server.cpp           # File transfer server\r\n│   ├── client.cpp           # File transfer client\r\n│   ├── client_files/        # Directory holding files to send (client side)\r\n│   ├── server_files/        # Directory holding files to send (server side)\r\n│   ├── client_received/     # Directory where files are stored (client side)\r\n│   ├── server_received/     # Directory where files are stored (server side)\r\n```\r\n\r\n---\r\n\r\n## 💬 Bidirectional Chat App\r\n\r\n### ✅ Features\r\n\r\n* Both server and client can send/receive simultaneously\r\n* Implemented using `std::thread`\r\n\r\nEach side (server and client) launches **two threads**:\r\n1. **`receiveMessages(socket)`** – Continuously reads messages from the socket and prints to console.\r\n2. **`sendMessages(socket)`** – Continuously reads user input from terminal and sends via socket.\r\n\r\nThese run concurrently so that a client can keep listening and sending at the same time.\r\n\r\n#### Server Command\r\nTerminal 1\r\n```bash\r\n# Compile\r\n$ g++ -std=c++11 server.cpp -o server.exe -lws2_32\r\n\r\n# Run\r\n$ ./server.exe\r\n```\r\n\r\n#### Client Command\r\nTerminal 2\r\n```bash\r\n# Compile\r\n$ g++ -std=c++11 client.cpp -o client.exe -lws2_32\r\n\r\n# Run\r\n$ ./client.exe\r\n```\r\n\r\n---\r\n\r\n## 📂 Simple Bidirectional File Transfer Protocol\r\n\r\n### ✅ Features\r\n\r\n* Send any binary/text file (e.g., `.jpg`, `.txt`, `.pdf`, etc.)\r\n* Transfers files using raw buffer chunks\r\n* Bidirectional: Client \u003c-\u003e Server\r\n\r\n### 🧠 How It Works (Step-by-step)\r\n\r\nEach side (server/client) performs these steps:\r\n\r\n1. **Open File**:\r\n   * File is opened in `binary` read mode on sender side.\r\n   * File is opened in `binary` write mode on receiver side.\r\n\r\n2. **Send Metadata**:\r\n   * First, the **file name** (e.g., `image.jpg`) is sent.\r\n   * Then, the **file size** (as a `long long`) is sent to pre-calculate read loops.\r\n\r\n3. **Transfer Data**:\r\n   * File data is read in fixed buffer chunks (e.g., 1024 bytes) and sent via `send()`.\r\n   * The receiver writes each chunk to the file until the complete file is received.\r\n\r\n4. **Directory**:\r\n   * Sent files are picked from `client_files/`\r\n   * Received files are stored in `server_received/` (or vice versa for reverse transfer)\r\n\r\n### 🗂️ Directory Setup\r\n```bash\r\nmkdir client_files client_received server_files server_received\r\n```\r\n\r\n### Server Command\r\nTerminal 1\r\n```bash\r\n$ g++ server.cpp -o server.exe -lws2_32\r\n$ ./server.exe\r\n```\r\n\r\n### Client Command\r\nTerminal 2\r\n```bash\r\n$ g++ client.cpp -o client.exe -lws2_32\r\n$ ./client.exe\r\n```\r\n---\r\n#### Basics of Socket Programming in C++\r\n[Uni-directional simple TCP-based Client-Server connection](https://github.com/shoryasethia/socket-programming/tree/main/greeter) \r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshoryasethia%2Fsocket-programming","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshoryasethia%2Fsocket-programming","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshoryasethia%2Fsocket-programming/lists"}