Hello,
Please I would create a client and a server in C that communicate frequently. The client sends "hello message" to the server, the server waits a few minutes and sends a "hello message" to the client, the client sends again "hello server ".. etc up to 15 minutes
Can you guide me please?
Thank you for help
Yes it's a homework
I need to change the following code:
client.c
int main (int argc, char *argv ) {
int socketclient,rc;
int port, retour;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
char adresseserveurip[32];
char buffer[BUFFER_TAILLE];
printf("Entrez l'adresse IP ou le nom du serveur :");
scanf("%s", adresseserveurip);
printf(" port :");
scanf("%d", &port);
h = gethostbyname(adresseserveurip);
if(h==NULL) {
printf("%s: serveur inconnu '%s'\n",argv[0],adresseserveurip);
exit(1);
}
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
servAddr.sin_port = htons(port);
socketclient = socket(AF_INET, SOCK_STREAM, 0);
if(socketclient<0) {
perror("Probleme de creation de socket\n ");
exit(1);
}
//Connexion au serveur
rc = connect(socketclient, (struct sockaddr *) &servAddr, sizeof(servAddr));
if(rc < 0)
{
printf("Probleme de connexion\n");
exit(1);
}
retour = read (socketclient, buffer, 32);
buffer[32] = '\0';
printf("\nMessage :%s. Nombre de caracteres lus = %d\n", buffer, retour);
close (socketclient); // Fermeture de la socket
return 0;
}
server.c
int creer_socket_serveur(int port)
{
int listen_fd;
struct sockaddr_in sin;
if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) exit(1);
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = INADDR_ANY;
if (bind(listen_fd, (struct sockaddr *)&sin, sizeof (sin)) == -1) exit(1);
listen(listen_fd, 5);
return (listen_fd);
}
main (int argc, char *argv )
{
int port, so, client_sin_len, nb, socketserveur;
struct sockaddr_in client_sin;
char buffer[BUFFER_TAILLE]="Ceci est un exemple de socket!!!";
port=1111;
socketserveur = creer_socket_serveur(port); //Creation du serveur et socket
printf("Serveur pret ... En attente de requetes ...\n");
client_sin_len = sizeof (client_sin);
so = accept(socketserveur, (struct sockaddr *)&client_sin, &client_sin_len);
if (so == -1)
{
printf ("Probleme de creation d'une socket de traitement\n");
exit (1);
}
nb = write(so, buffer, sizeof (buffer));
printf("Transmission effectuee. Nombre de caracteres transmis = %d\n", nb);
close (so);
close(socketserveur);
exit (0);
}
Thank you so much
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules .
Please review the rules , which you agreed to when you registered, if you have not already done so.
More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.
If you did post homework in the main forums, please review the guidelines for posting homework and repost.
Thank You.
The UNIX and Linux Forums.