[C – UNIX] Network – TCP, UDP Sockets

The basic building block of C network programming is a socket. Let’s find out how to create a socket.

 

1. Socket Descriptor

The socket() function is defined in <sys/socket.h>.

int socket (int family, int type, int protocol);
  • family : AF_INET for TCP or UDP 
  • type : SOCK_STREAM (connected stream like TCP), SOCK_DGRAM (connectionless stream like UDP), SOCK_SEQPACKET (sequnced packet), or SOCK_RAW (low level raw data)
  • protocol : IPPROTO_TCP or IPPROTO_UDP. But usually set to 0. Zero protocol lets the kernel choose the correct protocol based on the type.
  • returns a socket descriptor or -1 on error.

 

2. Wrapper Function for socket()

The following code is a helper function to create a socket.

int Socket (int family, int type, int protocol)
{
  int n=-1;
  if ( (n=socket(family, type, protocol)) < 0 )
  {
    err_sys("Socket Error");
  }
  return n;
}

 

3. Binding

Once you have a socket, you might have to associate that socket with an address and a port on your local machine. The port number is used by the kernel to match an incoming packet to a certain process’s socket descriptor. If you’re going to only be doing a connect(), this may be unnecessary. This function is defined in <sys/socket.h>.

int bind (int sockfd, struct sockaddr *my_addr, int addrlen);
  • sockfd : the socket file descriptor returned by socket()
  • my_addr : a pointer to a struct sockaddr that contains information about the port and the IP address
  • addrlen : sizeof(struct sockaddr)
  • returns -1 on error and sets errno to the error’s value.

 

4. Connecting

The connect() function is used when a client wants to connect to a remote host. It is defined in <sys/socket.h>.

int connect (int sockfd, const struct sockaddr *serv_addr, int addrlen);
  • sockfd : the socket file descriptor returned by socket()
  • my_addr : a pointer to the struct sockaddr (or sockaddr_in) of the server
  • addrlen : sizeof(struct sockaddr)
  • return -1 on error and set the variable errno

 

5. Listening

The listen() function makes a server to wait for the clients’ request. This function should be called after the bind() and before the accept(). It is defined in <sys/socket.h>.

int listen(int sockfd, int backlog);
  • sockfd : the socket file descriptor returned by socket() 
  • backlog : the number of connections allowed on the incoming queue
  • returns -1 and sets errno on error

 

6. Aaccepting

The accpet() function is called by a server to create a new socket to the client which requests the connection using the connect(). It is defined in <sys/socket.h>.

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
  • sockfd : the socket file descriptor returned by socket()
  • addr : a pointer to the struct sockaddr(or sockaddr_in) of the client
  • addrlen : length of the *addr, sizeof(struct sockaddr)
  • return a new socket descripter or -1 on error

 

7. Closing

The close() function is used to close a socket and terminate a TCP/UDP connection.

int close(int sockfd);

Just in case you want a little more control over how the socket closes, you can use the shutdown() function. It allows you to cut off communication in a certain direction, or both ways (just like close() does.)

int shutdown(int sockfd, int how);
  • sockfd : the socket file descriptor you want to shutdown
  • how : the way of closing sockets
    • 0 : Further receives are disallowed
    • 1 : Further sends are disallowed
    • 2 : Further sends and receives are disallowed (like close())

 

8. Creating a Socket for a Client

...
socket();
...
connect();
...
/* send data */
...
close();

 

9. Creating a Socket for a Server

...
socket();
bind();
listen();

accept();
// receive or send data
...
close();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s