How To: Create a Socket in the C Programming Language under Unix

Create a Socket in the C Programming Language under Unix

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.

What is a socket?

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.

What libraries in C are needed in order to create a simple socket?

In order to create a simple socket all you need is the library...

#include <sys/socket.h> 

#include <perror>

...included in your declarations. This along with the standard I/O and library headers should be just fine.

What do the various data types in a socket mean?

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.

What is the call to create a socket?

createdSocket = socket(AF_UNIX, SOCK_STREAM, 0);

if(createdSocket == -1)

{

    Perror("socket");

    Exit(1);

}

Warnings

  • 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.

Tips

  • 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.


Just updated your iPhone to iOS 18? You'll find a ton of hot new features for some of your most-used Apple apps. Dive in and see for yourself:

1 Comment

gREAT BUT HOW EXACTLY DO I USE MFC CLASS?

Share Your Thoughts

  • Hot
  • Latest