aide pour socket TCP IP en C

aide pour socket TCP IP en C - C - Programmation

Marsh Posté le 16-05-2010 à 14:52:01    

:heink:  
Salut à tous,
voila j'ai 2 programme en C, concernant les sockets.
Mon probleme c'est que le client se connecte au serveur, mais je veux que le serveur renvoie un message (saisie au clavier) pour notifier au client qu'il est bien connecté. Voici mes deux programmes :
Client :
/****** comclient.c ******/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
/* Our server port as in the previous program */
#define SERVER_PORT 12345
 
main (int argc, char *argv[])
{
 int len, rc;
 int sockfd;
 char send_buf[100];
 char recv_buf[100];
 struct sockaddr_in addr;
 
 if(argc !=2)
 {
  printf("Usage: %s <Server_name or Server_IP_address>\n", argv[0]);
  exit (-1);
 }
 /* Create an AF_INET stream socket */
 sockfd = socket(AF_INET, SOCK_STREAM, 0);
 if(sockfd < 0)
 {
  perror("client - socket() error" );
  exit(-1);
 }
 else
  printf("client - socket() is OK.\n" );
  /* Initialize the socket address structure */
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(SERVER_PORT);
 
  /* Connect to the server */
  rc = connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
  if(rc < 0)
  {
   perror("client - connect() error" );
   close(sockfd);
   exit(-1);
  }
  else
  {
   printf("client - connect() is OK.\n" );
   printf("connect() completed successfully.\n" );
   printf("Connection with %s using port %d established!\n", argv[1], SERVER_PORT);
  }
 
  /* Enter data buffer that is to be sent */
   
 
  /* Send data buffer to the worker job */
  //len = send(sockfd, send_buf, strlen(send_buf) + 1, 0);
/*  if(len != strlen(send_buf) + 1)
  {
   perror("client - send() error" );
   close(sockfd);
   exit(-1);
  }
  else*/
  while(1){
 
   /* Receive data buffer from the worker job */
   len = recv(sockfd, recv_buf, sizeof(recv_buf), 0);
   recv_buf[len] = '\0';
   if(len != strlen(send_buf) + 1)
   {
    perror("client - recv() error" );
    close(sockfd);
    exit(-1);
   }
   else
   {
    printf("client - recv() is OK.\n" );
    printf("The sent message: \"%s\" successfully received by server and echoed back to client!\n", recv_buf);
    printf("%d bytes received.\n", len);
    printf("Enter message to be sent to server:\n" );
    fgets(send_buf, sizeof send_buf, stdin);
    printf("client - send() is OK.\n" );
    printf("%d bytes sent.\n", len);
   }
  }
   /* Close the socket */
   close(sockfd);
   return 0;
}
 
Server:
 
/**** iserver.c ****/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#define SERVER_PORT 12345
 
/* Run with a number of incoming connection as argument */
int main(int argc, char *argv[])
{
 int i, len, num, rc;
 int listen_sd, accept_sd;
 /* Buffer for data */
 char buffer[100];
 struct sockaddr_in addr;
 
 /* If an argument was specified, use it to */
 /* control the number of incoming connections */
 if(argc >= 2)
  num = atoi(argv[1]);
  /* Prompt some message */
 else
 {
  printf("Usage: %s <The_number_of_client_connection else 1 will be used>\n", argv[0]);
  num = 1;
 }
   
 /* Create an AF_INET stream socket to receive */
 /* incoming connections on */
 listen_sd = socket(AF_INET, SOCK_STREAM, 0);
 if(listen_sd < 0)
 {
  perror("Iserver - socket() error" );
  exit(-1);
 }
 else
  // commentaire - printf("Iserver - socket() is OK\n" );
   
  // commentaire - printf("Binding the socket...\n" );
  /* Bind the socket */
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(SERVER_PORT);
  rc = bind(listen_sd, (struct sockaddr *)&addr, sizeof(addr));
  if(rc < 0)
  {
   perror("Iserver - bind() error" );
   close(listen_sd);
   exit(-1);
  }
  else
   // commentaire - printf("Iserver - bind() is OK\n" );
     
   while(1){
   /* Set the listen backlog */
   rc = listen(listen_sd, 5);
   if(rc < 0)
   {
    perror("Iserver - listen() error" );
    close(listen_sd);
    exit(-1);
   }
   else
   
    // commentaire - printf("Iserver - listen() is OK\n" );
     
    /* Inform the user that the server is ready */
    // commentaire - printf("The Iserver is ready!\n" );
    /* Go through the loop once for each connection */
    for(i=0; i < num; i++)
    {
     /* Wait for an incoming connection */
     // commentaire - printf("Iteration: #%d\n", i+1);
     // commentaire - printf(" waiting on accept()\n" );
     accept_sd = accept(listen_sd, NULL, NULL);
     if(accept_sd < 0)
     {
      perror("Iserver - accept() error" );
      close(listen_sd);
      exit(-1);
     }
     else
      // commentaire - printf("accept() is OK and completed successfully!\n" );
       
      /* Receive a message from the client */
      // commentaire - printf("I am waiting client(s) to send message(s) to me...\n" );
      rc = recv(accept_sd, buffer, sizeof(buffer), 0);
      if(rc <= 0)
      {
       perror("Iserver - recv() error" );
       close(listen_sd);
       close(accept_sd);
       exit(-1);
      }
      else
       printf("The message from client: \"%s\"", buffer);
       /* Echo the data back to the client */
       // commentaire - printf("Echoing it back to client...\n" );
 
       if(strcmp(buffer,"toto" )==1){
         printf("LE CLIENT A BIEN REPONDU TOTO\n" );
         memset(buffer,'\0',101);  
       }
       else{
        printf("PAS LA BONNE REPONSE\n" );
        memset(buffer,'\0',101);  
       }
       len = rc;
       rc = send(accept_sd, buffer, len, 0);
       if(rc <= 0)
       {
        perror("Iserver - send() error" );
        close(listen_sd);
        close(accept_sd);
        exit(-1);
       }
       else
        printf("Iserver - send() is OK.\n" );
        /* Close the incoming connection */
        close(accept_sd);
       }
       /* Close the listen socket */
   }
close(listen_sd);
return 0;
}
 
Merci de me donner un pti' coup de main.

Reply

Marsh Posté le 16-05-2010 à 14:52:01   

Reply

Marsh Posté le 16-05-2010 à 15:30:10    

nino37d12 a écrit :

:heink:  
Mon probleme c'est que le client se connecte au serveur, mais je veux que le serveur renvoie un message (saisie au clavier) pour notifier au client qu'il est bien connecté. Voici mes deux programmes :


Tu es sûr qu'ils sont bien de toi?
 
C'est curieux, autant de lignes de code, tout un client/serveur en C avec réception du message du client et son affichage et tu cales juste sur le fait de renvoyer le message au client? À ce stade, c'est presque trivial.
 
En plus, tous ces commentaires en anglais et du 'toto' en français à la fin.
 
Tu n'aurais pas plutôt piqué un code existant, sans rien comprendre, avant de nous demander un "coup de main" pour le compléter?


---------------
Now Playing: {SYNTAX ERROR AT LINE 1210}
Reply

Marsh Posté le 16-05-2010 à 17:19:27    

http://www.tenouk.com/Module41b.html
 
Ca ressemble quand même vachement à son code


---------------
"I can cry like Roger. It's just a shame I can't play like him" - Andy Murray, 2010
Reply

Marsh Posté le 16-05-2010 à 17:53:30    

Yaisse... J'avais ça sous le coude :)


---------------
Now Playing: {SYNTAX ERROR AT LINE 1210}
Reply

Marsh Posté le 17-05-2010 à 06:58:55    

sircam a écrit :

Yaisse... J'avais ça sous le coude :)


 
Salut les gars, merci pour les réponses,  
je suis autodidacte, c'est sûr qu'il a fallu que je m'inspire de quelque chose : www.tenouk.com,
ce que je ne comprend pas, c'est pourquoi la boucle "while(1)" ne fonctionne pas. C'est tout, j'ai jamais prétendu être l'auteur de ce code. "Le web pour tous" Tim lee barnes.

Reply

Marsh Posté le 17-05-2010 à 10:47:34    

Code :
  1. addr.sin_addr.s_addr = htonl(INADDR_ANY);


là tu te connecte à n'importe quel serveur.........

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed