mildred | Voila, j'essaie de faire un petit serveur mais je ny arrive pas. J'utilise les sockets unix.
Jai des problèmes pour lire les données du socket. Voici mon code:
Code :
- send(t, "Bienvenue\n", sizeof("Bienvenue\n" ), 0);
- quit = 0;
- fprintf(stderr, "Listening ...\n" );
- while(!quit)
- {
- char buffer[BUFSIZE];
- if(recv(t, buffer, BUFSIZE, 0) > 0)
- {
- if(buffer[0] == "q" ) quit = 1;
- else printf("unknown request: %s", buffer);
- }
- }
- close(t);
- fprintf(stderr, "Connexion closed ...\n" );
|
Lorsque je me connexte avec telnet, je n'ai pas le message de bienvenue alors que sur la console du serveur, ile me marque bien "Listening ..."
Après, quoi que je fasse sur mon telnet, rien ne se passe sur mon serveur ...
Pouvez vous maider ...
Merci
Voici mon fichier complet pour ceux que ca intéresse:
config.c
Code :
- /***************************************************************************
- * Copyright (C) 2004 by mildred *
- * mildred@louve *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program; if not, write to the *
- * Free Software Foundation, Inc., *
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
- ***************************************************************************/
- #define true 1
- #define false ""
- #define YES 1
- #define NO 0
- #define PORT 59300
- #define BACKLOG 100
- #define BUFSIZE 250
- #define SOCKET_ERROR -1
|
serv.c
Code :
- /***************************************************************************
- * Copyright (C) 2004 by mildred *
- * mildred@louve *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program; if not, write to the *
- * Free Software Foundation, Inc., *
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
- ***************************************************************************/
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>*
- #include "config.c"
- int main(int argc, char *argv[])
- {
- int sockfd, newfd, size, count_req, t, quit;
- pid_t pid;
- struct sockaddr_in local;
- struct sockaddr_in remote;
- bzero(&local, sizeof(local));
- local.sin_family = AF_INET;
- local.sin_port = htons( PORT );
- local.sin_addr.s_addr = INADDR_ANY;
- bzero(&(local.sin_zero), 8);
- if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR)
- {
- fprintf(stderr, "Socket creation error\n" );
- return EXIT_FAILURE;
- }
- if(bind(sockfd, (struct sockaddr *)&local, sizeof(struct sockaddr)) == SOCKET_ERROR)
- {
- fprintf(stderr, "Can't link socket\n" );
- return EXIT_FAILURE;
- }
- if(listen(sockfd, BACKLOG) == SOCKET_ERROR)
- {
- fprintf(stderr, "Can't listen\n" );
- return EXIT_FAILURE;
- }
- size = sizeof(struct sockaddr_in);
- while(t = accept(sockfd, (struct sockaddr *)&remote, &size) || true)
- {
- if(t == SOCKET_ERROR)
- {
- fprintf(stderr, "Can't accept connexion\n" );
- return EXIT_FAILURE;
- continue;
- }
- else pid = fork ();
- if (pid > 0)
- {
- printf("Forking. PID: %d\n", pid);
- }
- else if (pid == 0)
- {
- send(t, "Bienvenue\n", sizeof("Bienvenue\n" ), 0);
- quit = 0;
- fprintf(stderr, "Listening ...\n" );
- while(!quit)
- {
- char buffer[BUFSIZE];
- if(recv(t, buffer, BUFSIZE, 0) > 0)
- {
- if(buffer[0] == "q" ) quit = 1;
- else printf("unknown request: %s", buffer);
- }
- }
- close(t);
- fprintf(stderr, "Connexion closed ...\n" );
- }
- else
- {
- fprintf(stderr, "Can't fork(): Unknown error: %d\n", pid);
- fprintf(stderr, "Type `man 2 pid` for more details\n" );
- return EXIT_FAILURE;
- }
- }
- close(newfd);
- return EXIT_SUCCESS;
- }
|
|