:PROPERTIES:
:ID:       efe0360d-8d81-4372-833d-ad58e67d17c6
:END:
#+title: socket_programming_in_c
#+filetags: :networking:coding:notes:

From: [[https://www.linuxhowtos.org/C_C++/socket.htm][Web Link]]
* Steps: 
The steps involved in establishing a socket on the client side are as follows:

1. Create a socket with the socket() system call
2. Connect the socket to the address of the server using the connect() system call
3. Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.

The steps involved in establishing a socket on the server side are as follows:

1. Create a socket with the socket() system call
2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
3. Listen for connections with the listen() system call
4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
5. Send and receive data
* Socket types:
When you create a socket, you must specify the *address domain* and the *socket type*, the client and server can only communicate if they are of the same type and in the same domain.

The two most common address domains:
- unix domain (they share common file system)
- internet domain

The two most common socket types:
- stream sockets (continuous stream of characters) - uses tcp
- datagram sockets (reads the entire messages at once) - uses dp 
