Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Starting create monothread APP #2

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 89 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,92 @@
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "lib_socket/library.h"
typedef struct
{
char nom[20];
int age;
float poids;
} PERSONNE;

int main() {
printf("%d",ServerSocket(4296));
}
int main(int argc,char* argv[]) {
if (argc != 2)
{
printf("Erreur...\n");
printf("USAGE : ServeurTest portServeur\n");
exit(1);
}
int sServer;
if ((sServer = ServerSocket(atoi(argv[1]))) == -1)
{
perror("Erreur de ServeurSocket");
exit(1);
}

printf("Attente d'une connexion...\n");
int sService;
if ((sService = Accept(sServer,NULL)) == -1)
{
perror("Erreur de Accept");
close(sServer);
exit(1);
}
printf("Connexion acceptee !\n");

// ***** Reception texte pur **************************************
char buffer[100];
int nbLus;
if ((nbLus = Receive(sService,buffer)) < 0)
{
perror("Erreur de Receive");
close(sService);
close(sServer);
exit(1);
}
printf("NbLus = %d\n",nbLus);
buffer[nbLus] = 0;
printf("Lu = --%s--\n",buffer);
// ***** Envoi de texte pur ***************************************
char texte[80];
sprintf(texte,"Je vais bien merci ;) !");
int nbEcrits;
if ((nbEcrits = Send(sService,texte,strlen(texte))) < 0)
{
perror("Erreur de Send");
close(sService);
close(sServer);
exit(1);
}
printf("NbEcrits = %d\n",nbEcrits);
printf("Ecrit = --%s--\n",texte);
// ***** Reception d'une structure ********************************
PERSONNE p;

if ((nbLus = Receive(sService,(char*)&p)) < 0)
{
perror("Erreur de Receive");
close(sService);
close(sServer);
exit(1);
}
printf("NbLus = %d\n",nbLus);
printf("Lu = --%s--%d--%f--\n",p.nom,p.age,p.poids);
// ***** Envoi d'une structure *************************************
strcpy(p.nom,"charlet");
p.age = 54;
p.poids = 71.98f;
if ((nbEcrits = Send(sService,(char*)&p,sizeof(PERSONNE))) < 0)
{
perror("Erreur de Send");
close(sService);
close(sServer);
exit(1);
}
printf("NbEcrits = %d\n",nbEcrits);
printf("Ecrit = --%s--%d--%f--\n",p.nom,p.age,p.poids);
close(sService);
close(sServer);
exit(0);
}