Creer un serveur ftp [C] - Programmation
Marsh Posté le 19-03-2001 à 19:31:35
Il faut utiliser la fonction "accept" qui crée une nouvelle socket quand une connexion a lieu.
Marsh Posté le 19-03-2001 à 19:32:56
Comment est-ce que je sais qu'une nouvelle connection a lieu?
Marsh Posté le 19-03-2001 à 19:38:08
extrait de "man accept": If no pending connections are present on the queue, and
the socket is not marked as non-blocking, accept blocks
the caller until a connection is present. If the socket
is marked non-blocking and no pending connections are pre-
sent on the queue, accept returns EAGAIN.
In order to be notified of incoming connections on a
socket, you can use select(2) or poll(2). A readable
event will be delivered when a new connection is attempted
and you may then call accept to get a socket for that con-
nection. Alternatively, you can set the socket to deliver
SIGIO when activity occurs on a socket; see socket(7) for
details.
Marsh Posté le 19-03-2001 à 19:02:35
Une fois que j'ecoute sur le port passer en parametre, comment est-ce que je sais qu'un client veut se connecter?
voici ou j'en suis :
void start_server(int port)
{
struct sockaddr_in sin;
int sin_len;
if ((gbl->listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
fprintf(stderr, "Error creating socket.\n" );
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = INADDR_ANY;
if (bind(gbl->listen_fd, (struct sockaddr *)&sin, sizeof (sin)) == -1)
fprintf(stdout, "Error binding socket.\n" );
listen(gbl->listen_fd, 5);
sin_len = sizeof (sin);
getsockname(gbl->listen_fd, (struct sockaddr *)&sin, (socklen_t *) &sin_len);
fprintf(stdout, "Listening on port %d (requested port was %d).\n", ntohs(sin.sin_port), port);
}
Merci d'avance