|
1 | 1 | # SocketProgramming |
2 | | -Sockets are a fascinating concept as communication endpoints between 2 processes running on the same host using the same filesystem or on totally different hosts and filesystem miles apart but of course connected by a network |
3 | | -When I was new to socket programming it was very confusing for me to understand the concept with the code on the internet which was either too complex, lacked annotations or didn't cover enough |
4 | | -This prodded me to create this repo with some socket programming examples in C with annotations to demonstrate how sockets work |
| 2 | +Sockets are a fascinating concept as communication endpoints between 2 or more entities running on the same host using the same filesystem or on totally different hosts and filesystem miles apart but of course connected by a network |
| 3 | +When I was new to socket programming, it was very difficult for me to understand various types of sockets and how they work with the resources on the internet which were scattered, lacked annotations or didn't cover enough. |
| 4 | +This prodded me to create a unified repo containing some examples in C with copious annotations for different types of sockets. I also created some real world applications like a network file server/sender to demonstrate their possible applications |
5 | 5 |
|
6 | | -There are primarily 2 socket domains (Internet and Unix) |
7 | | -In this project, I have laid down examples with elaborate comments for 3 most commonly used types of sockets: |
| 6 | +In this project, I have created examples and sample apps for following type of sockets: |
8 | 7 | - **TCP** (Stream socket used for secure reliable connection between 2 processes running on the same or different hosts on the same network) |
9 | 8 | - **UDP** (Datagram socket used for fast data exchange between 2 processes running on the same or different hosts on the same network) |
10 | 9 | - **Unix** (Stream/Datagram socket residing on the same filesystem as the 2 processes using it to communicate) |
| 10 | +- **CAN** (Raw sockets used to transmit and receive CAN messages using a can interface created by a CAN bus controller) |
| 11 | +- **Netlink** (Raw sockets used mostly for kernel-userspace communication and also for interprocess communication) |
| 12 | +- **Virtio** (Virtio sockets used for communication between host and virtual machine using the virtio-vsock driver and vhost-vsock backend) |
| 13 | + |
| 14 | +**Note:** If you're working with SocketCAN and do not have a real CAN bus to work with, you can create a virtual CAN bus interface using can-utils package. A script `vcan-setup.sh` has been created for the same under *Misc* directory of this repo https://github.com/amoldhamale1105/CodingPlayground |
11 | 15 |
|
12 | 16 | ## Build Instructions |
13 | | -This project is setup with a hierarchical cmake structure. The top level cmake adds downstream directories which in turn have their own cmake to build the sources enclosed |
14 | | -By default it will generate binaries for all sources within this repo under the `bin` directory |
| 17 | +This project is setup with a hierarchical cmake structure. The top level cmake adds downstream directories which in turn have their own cmake to build the sources enclosed. By default it will generate binaries for all sources within this repo under the `bin` directory |
| 18 | + |
15 | 19 | If you working with one particular type of socket, for instance `TCP`, comment out the `add_subdirectory()` calls which are not required. The top-level cmake in our example would look something like this: |
16 | 20 | ``` |
17 | 21 | add_subdirectory(TCP) |
18 | 22 | #add_subdirectory(UDP) |
19 | 23 | #add_subdirectory(Unix) |
20 | 24 | ``` |
21 | | -This will save build time and unncessary build instructions when you're not intending to execute the output binaries |
22 | | -Once the target directories are set in cmake, run `Clean Reconfigure All` or `Clean Rebuild All` in case you've loaded the project using VSCode |
23 | | -Enter the following commands at the project root if you work with the command line |
| 25 | +This will save you some time if you're dedicatedly working on a specific type of socket |
| 26 | + |
| 27 | +Once the target directories are set in cmake, run the build script with or without options depending on your build configuration. Defaults for each option will appear between `{}` in the usage instruction. Print the script usage by running the following command |
| 28 | +``` |
| 29 | +./build.sh -h |
| 30 | +``` |
| 31 | +As an example, if you want to use the script to build for release mode with `Unix Makefiles` cmake generator, it can be executed as follows |
24 | 32 | ``` |
25 | | -mkdir build |
26 | | -cd build |
27 | | -cmake .. |
28 | | -make |
| 33 | +./build.sh -a -r -g "Unix Makefiles" |
29 | 34 | ``` |
30 | 35 | On a successful build, required binaries will be generated under the `bin` directory at the root of the project |
31 | 36 |
|
32 | 37 | ## Contribution |
33 | | -This is a repository catering to new programmers and experienced developers alike who like to tinker with sockets. I'd love people to contribute by adding to this project any fun experiments they conducted using raw socket programming |
34 | | -Also, any other version of code or in a different language (for instance, python) which explains socket programming in a much better manner would be great. |
35 | | -Contribution in any way is appreciable. Send me an email ( [email protected]) if you happen to face issues creating branches, issues or raising pull requests |
| 38 | +This is a repository catering to new programmers and experienced developers alike who like to tinker with sockets. I'd love to see you contribute by adding to this project any fun experiments you conducted using raw socket programming. Be sure to share this with people who are new to socket programming and wish to have working examples with explanation for all types of sockets in one place. |
| 39 | +Contribution in any way is welcome. Get in touch with me in case of any questions or suggestions [email protected] |
0 commit comments