The accept() system call waits a connection from a client.

For the example above, we are going to take the nc tool (you can use telnet, it is the same).

If we reach the server through the nc connection, the server will shutdown.
But that’s what we want to do.

Let’s see it in this accept() tutorial.

Using accept()

server.c

/* server.c */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "h.h"
void my\_socket(t\_s *s)
{
s->name = "TCP";
s->domain = AF\_INET;
s->type = SOCK\_STREAM;
s->pe = getprotobyname(s->name);
s->fd = socket(s->domain, s->type, s->pe->p\_proto);
check\_error(s->fd, -1);
}
void my\_bind(t\_s *s, int port)
{
s->sockfd = s->fd;
s->addr.sin\_family = s->domain;
s->addr.sin\_addr.s\_addr = INADDR\_ANY;
s->addr.sin\_port = htons(port);
s->addrlen = sizeof(s->addr);
s->bindValue = bind(s->fd, (const struct sockaddr *)&s->addr, s->addrlen);
check\_error(s->bindValue, -1);
}
void my\_listen(t\_s *s, int backlog)
{
s->listenValue = listen(s->sockfd, backlog);
check\_error(s->listenValue, -1);
}
void my\_accept(t\_s *s)
{
s->addrlenAccept = sizeof(s->addrAccept);
s->acceptValue = accept(s->sockfd, (struct sockaddr *)&s->addrAccept, &s->addrlenAccept);
check\_error(s->acceptValue, -1);
}
int main(int ac, char *av[])
{
t\_s s;
check\_arg(ac, ARG\_SIZE);
my\_socket(&s);
my\_bind(&s, atoi(av[1]));
my\_listen(&s, BACKLOG);
my\_accept(&s);
debug(&s);
check\_error(close(s.acceptValue), -1);
check\_error(close(s.fd), -1);
return 0;
}

debug.c

/* debug.c */
#include 
#include 
#include 
#include 
#include 
#include "h.h"
void debug(t\_s *s)
{
printf("\n===== socket() =====\n");
printf("domain = %d\n", s->domain);
printf("type = %d\n", s->type);
printf("fd = %d\n", s->fd);
printf("name = %s\n", s->name);
printf("p\_proto = %d\n", s->pe->p\_proto);
printf("\n===== bind() =====\n");
printf("sockfd = %d\n", s->sockfd);
printf("sin\_family = %d\n", s->addr.sin\_family);
printf("sin\_addr.s\_addr = %d\n", s->addr.sin\_addr.s\_addr);
printf("sin\_port = %d\n", s->addr.sin\_port);
printf("addrlen = %d\n", s->addrlen);
printf("bindValue = %d\n", s->bindValue);
printf("\n===== listen() =====\n");
printf("listenValue = %d\n", s->listenValue);
printf("\n===== accept() =====\n");
printf("addrlenAccept = %d\n", s->addrlenAccept);
printf("acceptValue = %d\n\n", s->acceptValue);
}
void check\_error(int test, int error)
{
if (test == error)
{
fprintf(stderr, "ERROR: %s\n", strerror(errno));
exit(EXIT\_FAILURE);
}
}
void check\_arg(int ac, int number)
{
if (ac < number)
{
printf("Usage: ./server [PORT]\n");
exit(EXIT\_FAILURE);
}
}

h.h

#ifndef H\_H\_
#define H\_H\_
#include 
/**
* Define
*/
#define ARG\_SIZE 2
#define BACKLOG 10
#define BUF\_SIZE 255
/**
* Structure
*/
typedef struct mystruct
{
/* socket */
int domain;
int type;
int fd;
char *name;
struct protoent *pe;
/* bind */
int sockfd;
socklen\_t addrlen;
struct sockaddr\_in addr;
int bindValue;
/* listen */
int listenValue;
/* accept */
int acceptValue;
socklen\_t addrlenAccept;
struct sockaddr\_in addrAccept;
} t\_s;
/**
* Prototype
*/
void debug(t\_s *);
void check\_error(int, int);
void check\_arg(int, int);
#endif /* H\_H\_ */

Compiling

$ gcc server.c debug.c -o server ; ./server 5000

Now that the server is launched, it’s waiting a connection from a client on the port 5000.

For that we need to start another shell (it will be our client side).
We have to specified the address and the port.
As we are in local (on our system), we can use the localhost alias or if it doen’t work, its real value : 127.0.0.1.

So let’s type with nc:

$ nc localhost 5000

Result

The server side is quitting and it displays:

===== socket() =====
domain = 2
type = 1
fd = 3
name = TCP
p\_proto = 6
===== bind() =====
sockfd = 3
sin\_family = 2
sin\_addr.s\_addr = 0
sin\_port = 4647
addrlen = 16
bindValue = 0
===== listen() =====
listenValue = 0
===== accept() =====
addrlenAccept = 16
acceptValue = 4

It’s getting good! 😳