Faire un don ! | | style | statistiques | contactez-nous | plan | lettre d'information

Programmation.c : Problem avec send()

Posté par cecile3147 () le 07 mars 2007
Je dois realiser un programme qui permettrait d'envoyer des messages d'un server a un client.

Apparement ma fonction send() renvoie toujours -1 je n'arrive pas a savoir pour quoi, si vous pouviez m'aider. Voici le code du coté client.







#include <sys/types.h>

#include <sys/socket.h>

#include <sys/time.h>

#include <netinet/in.h>

#include <netdb.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>



#define bufLength 1024



void makeLocalSocketAdress(struct sockaddr_in *sa){

sa->sin_family =AF_INET;

sa->sin_port=htons(0); // any port or port 2222 ????? htons(atoi(argv[1]))

sa->sin_addr.s_addr = htonl(INADDR_ANY); // on local host

}



void makeRemoteSocketAdress(struct sockaddr_in *sa, char * hostname, int port) {

struct hostent *host;



if ((host= gethostbyname(hostname))==NULL){

printf("Unknown host name \n");exit(-1);

}

sa->sin_family = AF_INET ;

sa->sin_addr= *(struct in_addr *)(host->h_addr); //memcpy((char*)&(sa->sin_addr.s_addr), host->h_addr,host->h_length);

sa->sin_port = htons(port);



}



int main(int argc, char *argv[]){



int sock,port;

struct sockaddr_in serverAdress;

struct sockaddr_in clientAdress;

int addrLength=sizeof(struct sockaddr_in);

char msg[bufLength]; //message to send/receive

char * hostname=malloc(30); // server adress



extern char *optarg;

extern int optind;

int semantic=0;

int c=0;



if (argc <3){

printf("Please provide server and port number \n");exit(-1);

}

while ((c = getopt(argc, argv, "lm")) != -1) {

switch (c) {

case 'l':

semantic = 0; //0 at-least-once semantic

break;

case 'm':

semantic = 1; //1 at-most-once semantic

break;

default: printf("usage: cmd [-l][-m][adress server][port number] \n");

}

}



strcpy(hostname,argv[argc-2]); // Copy the server adress given by the client in the variable hostname

port = atoi(argv[argc-1]); // Copy the port number given by the client in the variable port

port=htons(port);



if ((sock = socket(AF_INET,SOCK_DGRAM,0))== -1){ // creation socket for internet communication domain over UDP

printf("Error creation socket\n");exit(-1);

}

makeLocalSocketAdress(&clientAdress);

if((bind(sock,(struct sockaddr*)&clientAdress,addrLength))==-1){ // bind socket

printf("Error bind socket\n");exit(-1);

}



makeRemoteSocketAdress(&serverAdress,hostname,port);

strcpy(msg,"message envoye et bien recu");

printf("message : %s \n",msg);



if (sendto(sock,msg,strlen(msg),0,(struct sockaddr *)&serverAdress,addrLength) !=0 ) {

perror("Receive 1") ;

printf("Error send to\n");}

return 0;

}

> Lire le message (5 commentaires, moyenne: 1).  

Vous avez demandé le commentaire #810520.

bind -> connect

Posté par kesako () le 07/03/2007 à 14:57. (lien). Évalué à 0.

Bind () c'est pour les serveur .

pour un client, il faut faire connect()

  • [^]Re: bind -> connect

    Posté par Vincent ORDY () le 07/03/2007 à 15:08. (lien). Évalué à 2.

    hum hum

    sock = socket(AF_INET,SOCK_DGRAM,0))== -1){
    // creation socket for internet communication domain over UDP

    //...

    if (sendto(sock,msg,strlen(msg),0,(struct sockaddr *)&serverAdress,addrLength) !=0 ) {


    C'est de l'UDP, pas du TCP !! Donc bind() pour le serveur ET le client, et jamais de connect().

    Voir les schémas de http://www.spi.ens.fr/beig/systeme/sockets.html par exemple