Socket programming in Unix allows multiple computer programs to "talk" to each other in the form of open "pipes". This is used in many popular programs for the Linux system (see any distribution). This how-to is created to understand the simple creation of a socket and what the various parts of the call mean.
A socket is an integer variable representing an area in memory. At most, this value returns the pointer of a memory location. A socket is created so that data can be read to and from other sockets open on a Unix machine. This opening allows for the programs to send messages (data) back and forth to implement faster processing.
In order to create a simple socket all you need is the library...
...included in your declarations. This along with the standard I/O and library headers should be just fine.
To declare a socket it is a simple call to the operating system within the program itself.
int createdSocket = socket(AF_UNIX, SOCK_STREAM, 0); //file descriptor created
- The socket we created is called createdSocket. It is an integer in value and is a file descriptor to the socket stored in the Unix machine. This socket is usually created in the directory where the program is being run unless otherwise stated.
- Socket takes 3 values. Once the socket is created the value is a pointer to the socket file:
- AF_UNIX - This allows UNIX to understand that this socket is part of the Unix family. This call is included in the <sys/socket.h> declaration:
- SOCK_STREAM - This is a call to create a stream type of datagram. This datagram is popular with the TCP protocol stack and is far more reliable than a simple DGRAM call.
- The 0 value is arbitrary.
createdSocket = socket(AF_UNIX, SOCK_STREAM, 0);
if(createdSocket == -1)
- Never leave a socket connection open because it can open your computer up to maleware. Always close (createdSocket) so no more data can traverse the system.
- When creating a socket, it is a good idea to call Perror function to be returned to the user to allow them to know that the socket failed to create successsfully.
- Sockets are like any other data type (a struct) and can therefore be passed in and out of functions.
- Knowing about how a socket is called allows for you to create more complex programs.
- Sockets are the forerunner to harder code such as the TCP protocol stack programming.
Want to master Microsoft Excel and take your work-from-home job prospects to the next level? Jump-start your career with our Premium A-to-Z Microsoft Excel Training Bundle from the new Gadget Hacks Shop and get lifetime access to more than 40 hours of Basic to Advanced instruction on functions, formula, tools, and more.
Other worthwhile deals to check out:
- 97% off The Ultimate 2021 White Hat Hacker Certification Bundle
- 98% off The 2021 Accounting Mastery Bootcamp Bundle
- 99% off The 2021 All-in-One Data Scientist Mega Bundle
- 59% off XSplit VCam: Lifetime Subscription (Windows)
- 98% off The 2021 Premium Learn To Code Certification Bundle
- 62% off MindMaster Mind Mapping Software: Perpetual License
- 41% off NetSpot Home Wi-Fi Analyzer: Lifetime Upgrades