Envoyez une chaîne du client au serveur, puis enregistrez le fichier

J’ai ces deux codes C ci-dessous composés d’un client et d’un serveur. Le code est simple:
1- Serveur écoute et attend une connexion
2- Le client envoie une chaîne HTTP Post au serveur
3- Le serveur reçoit la requête, publie une réponse
4- Le client reçoit une réponse

Mais ce que je veux, c’est que le serveur sauvegarde la chaîne sur le disque. Par exemple, si le client (périphérique) envoie “Hello World”, je souhaite que le serveur (PC) puisse l’enregistrer dans un fichier texte.

Voici le code du serveur

/*server code*/ #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #define PORT "3434" // the port users will be connecting to #define BACKLOG 10 // how many pending connections queue will hold void sigchld_handler(int s) { while(waitpid(-1, NULL, WNOHANG) > 0); } // get sockaddr, IPv4 or IPv6: void *get_in_addr(struct sockaddr *sa) { if (sa->sa_family == AF_INET) { return &(((struct sockaddr_in*)sa)->sin_addr); } return &(((struct sockaddr_in6*)sa)->sin6_addr); } int main(void) { int sockfd, new_fd; // listen on sock_fd, new connection on new_fd struct addrinfo hints, *servinfo, *p; struct sockaddr_storage their_addr; // connector's address information socklen_t sin_size; struct sigaction sa; int yes=1; char s[INET6_ADDRSTRLEN]; int rv; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // use my IP if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // loop through all the results and bind to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("server: socket"); continue; } if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("server: bind"); continue; } break; } if (p == NULL) { fprintf(stderr, "server: failed to bind\n"); return 2; } freeaddrinfo(servinfo); // all done with this structure if (listen(sockfd, BACKLOG) == -1) { perror("listen"); exit(1); } sa.sa_handler = sigchld_handler; // reap all dead processes sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) { perror("sigaction"); exit(1); } printf("server: waiting for connections...\n"); while(1) { // main accept() loop sin_size = sizeof their_addr; new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if (new_fd == -1) { perror("accept"); continue; } inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s); printf("server: got connection from %s\n", s); if (!fork()) { // this is the child process close(sockfd); // child doesn't need the listener if (send(new_fd, "Server : I received your request \n", 23, 0) == -1) perror("send"); close(new_fd); exit(0); } close(new_fd); // parent doesn't need this } return 0; } 

Voici le code client

 #include  /* printf, sprintf */ #include  /* read, write, close */ #include  /* memcpy, memset */ #include  /* socket, connect */ #include  /* struct sockaddr_in, struct sockaddr */ #include  /* struct hostent, gethostbyname */ #include  void error(const char *msg) { perror(msg); exit(0); } int main(int argc,char *argv[]) { /* what and where are we going to send */ int portno = 3434; /* 3490 or 8080 */ char *host = "192.168.1.65"; char *message_fmt = "POST HTTP/1.0\n"; struct hostent *server; struct sockaddr_in serv_addr; int sockfd, bytes, sent, received, total; char message[1024],response[4096]; /* fill in the parameters */ sprintf(message,message_fmt,argv[1],argv[2]); printf("Request:\n%s\n",message); /* create the socket */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd h_addr,server->h_length); /* connect the socket */ if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); /* send the request */ printf("Sending request\n\n"); total = strlen(message); sent = 0; do { bytes = write(sockfd,message+sent,total-sent); if (bytes < 0) error("ERROR writing message to socket"); if (bytes == 0) break; sent+=bytes; } while (sent < 0); //while (sent < total); printf("I am a string post \n"); //receive the response printf("Receiving response \n"); memset(response,0,sizeof(response)); total = sizeof(response)-1; received = 0; do { bytes = read(sockfd,response-received,total-received); if (bytes < 0) error("ERROR reading response from socket"); if (bytes == 0) break; received+=bytes; } while(received < 0); //while (received < total); printf("Response received\n"); if (received == total) error("ERROR storing complete response from socket"); /* close the socket */ close(sockfd); /* process response */ printf("\nServer Response:\n%s\n\n",response); return 0; } 

Server stdout

 server: waiting for connections... server: got connection from 192.168.1.65 

Stdout client

 Request: POST HTTP/1.0 ip address: 192.168.1.65 Sending request I am a ssortingng post Receiving response Response received Server Response: I received your request 

La partie que vous n’avez pas incluse dans le code de votre serveur:

 3- Server gets the request, posts back a response 

Vous avez besoin de quelque chose comme:

 char buf[N]; ssize_t len; len = recv(sock, buf, sizeof(buf) - 1, 0); if (len > 0) { buf[len] = '\0'; /* save to file */ } 

après accept()