05-20-2008
1,
0
Join Date: May 2008
Last Activity: 20 May 2008, 7:18 PM EDT
Posts: 1
Thanks Given: 0
Thanked 0 Times in 0 Posts
Unix: socket & Co
Hello,
I need help to replace the ................. of client.c that request the server implemented by server.c
------------------
Listing 1 - server.c
/* Inclusion des différentes librairies nécessaires */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet.in.h>
#include <arpa.inet.h>
int main(int argc, char *argv[])
{
int res;
/* Operandes */
int a, b,buf;
/* socket mise en place côté serveur */
int serv_sock;
/* socket mise en place côté client */
int cli_sock;
/* adresse de la socket distante */
struct sockaddr_in localaddr;
/* longueur de l'adresse */
socklen_t localaddr_len;
/* longueur de l'adresse */
socklen_t distaddr_len;
localaddr_len = sizeof(localaddr);
/* Mise en place de la socket */
serv_sock = socket(AF_INET.SOCK_STREAM, 0);
if (serv_sock = socket(AF_INET.SOCK_STREAM, 0);
if (serv_sock == -1) {
perror(''Impossible de creer la socket'')
exit(-1);
}
/* Mise en place de la structure décrivant l'adresse
* et le port qui seront associés à la socket */
bzero((char*)&localaddr, localaddr_len);
/* Protocole internet */
localaddr.sin_family = AF.INET;
/* On écoute sur l'adresse de boucle locale */
localaddr.sin_addr.s_addr = inet_addr(''127.0.0.1'');
/* On écoute sur le port 1234 */
localaddr.sin_port=htons(1234);
/* On attache la socket au port 1234 sur
* l'adresse locale 127.0.0.1 */
if (bind(serv_sock,
(struct sockaddr *) &localaddr,
localaddr_len) == -1) {
perror(''Impossible d'attacher la socket'');
exit(-1);
}
/* Mise en place de l'écoute sur la socket */
if (listen(serv_sock, 1) == -1) {
perror(''Impossible d'écouter sur la socket'');
exit(-1);
}
/* On accepte les connexions entrantes */
cli_sock = accept(serv_sock,
(struct sockaddr *) &distaddr,
&distaddr_len);
if (cli_sock == -1) {
perror(''Erreur de connexion du client'');
exit(-1);
}
/* on recoit la première operande */
res = recv(cli_sock, &buf; sizeof(int), 0);
if (res != sizeof(int)) {
perror(''Donnees recues incorrectes'');
close(serv_sock);
exit(-1);
}
a = ntohl(buf);
printf(''Recu: %d/n'', a);
/* On recoit la deuxieme operande */
res = recv(cli_sock, &buf, sizeof(in), 0);
if (res != sizeof(int)) {
perror(''Donnees recues incorrectes'');
close(serv_sock);
exit(-1);
}
b = ntohl(buf);
printf(''Recu: %d\n'', b);
buf=htonl(a+b);
printf(''Envoi: %d\n'', a+b);
/* On envoie le resultat */
res=send(cli_sock, &buf, sizeof(int), 0);
if (res != sizeof(int)) {
perror(''Erreur de transmission de donnees'');
close(serv_sock);
exit(-1)
}
close(serv_sock);
close(cli_sock);
return ();
}
-----------------
Listing 2 - client.c
// Inclusion des différentes librairies necessaires
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int res;
/* Operandes */
int a, b, buf;
/* socket mise en place cote serveur */
int serv_sock;
/* socket mise en place cote client */
int cli_sock;
/* adresse de la socket distante */
struct sockaddr_in distaddr;
/* longueur de l'adresse */
socklen_t distaddr_len;
distaddr_len = sizeof(distaddr);
a=htonl(123);
b=btonl(124);
/* Creation de la socket */
cli.sock = .............................
if (cli_sock == -1) {
perror(''Impossible de creer la socket'');
exit(-1);
}
/* Mise en place de la structure decrivant
* l'adresse et le port qui seront associes
* a la socket */
bzero((char*)&distaddr, distaddr_len);
/* Protocole internet */
distaddr.sin_family = .......................
/* On se connecte sur l'adresse de boucle locale */
distaddr.sin_addr.s_addr = inet_addr(''127.0.0.1");
/* On se connecte sur le port 1234 */
distaddr.sin.port = .............................
/* Connexion au serveur */
serv_sock = ....................................
if (serv_sock == -1) {
perror(''Impossible de se connecter'');
}
/* On envoi la premiere operande */
res = .....................................
if (res != sizeof(int)) {
perror(''Impossible d'envoyer les donnees'');
close(cli_sock);
exit(-1);
}
/* On envoie la seconde operande */
res = send(cli_sock, &b, sizeof(int), 0);
if (res != sizeof(int)) {
perror(''Impossible d'envoyer les donnees'');
close(cli_sock);
exit(-1);
}
/* On recupere le resultat */
res = ....................................
if (res != sizeof(int)) {
perror(''Impossible de lire les donnees'');
close(cli_sock);
exit(-1);
}
printf(''Resultat:%d\n'', ntohl(buf));
.............................
close(serv_sock);
return 0;
}