I have a really simple exercise to do: make a socket where when a client is connected, the server display its information (address and port number).
I wrote this:
client.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
// check arguments
if (argc != 3)
{
fprintf(stderr, "Erreur argument\n");
exit(EXIT_FAILURE);
}
char *nom_machine = argv[1];
int port = atoi(argv[2]);
int sock;
struct sockaddr_in addr;
// création socket
sock = socket(AF_INET, SOCK_STREAM, 0);
// remplissage struct de l'adresse
addr.sin_family = AF_INET;
addr.sin_port = htons(port); // code les octets dans l'ordre réseau
inet_aton(nom_machine, &addr.sin_addr);
// connnexion à 1.2.3.4:4242
connect(sock, (struct sockaddr *)&addr, sizeof addr);
// display informations
fprintf (stderr,
"---------\nAdresse: %s\nPort: %hd\n---------\n",
inet_ntoa(addr.sin_addr),
ntohs(addr.sin_port));
// envoie message
int n = 7;
int r = write(sock, &n, sizeof(int));
if (r != sizeof(int)) {
fprintf(stderr, "Echec de l'envoi de n\n");
exit(EXIT_FAILURE);
}
// arrêt
close(sock);
exit(EXIT_SUCCESS);
}
Server.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Erreur argument\n");
exit(EXIT_FAILURE);
}
int port = atoi(argv[1]);
int sock;
struct sockaddr_in addr;
// création socket
sock = socket(AF_INET, SOCK_STREAM, 0);
// remplissage struct de l'adresse
addr.sin_family = AF_INET;
addr.sin_port = htons(port); // code les octets dans l'ordre réseau
addr.sin_addr.s_addr = INADDR_ANY;
// création connnexion
bind(sock, (struct sockaddr *)&addr, sizeof addr);
// écoute
listen(sock, 2);
// attend une connexion entrante
struct sockaddr_in client_addr;
socklen_t size;
int lect = accept(sock, (struct sockaddr *)&client_addr, &size); // (2) structure entring connection
// display client informations
fprintf (stderr,
"---------\nAdresse: %s\nPort: %d\n---------\n",
inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port));
// lecture message
int n;
int r = read(lect, &n, sizeof(int));
if (r <= 0) {
fprintf(stderr, "Le serveur n'a pas recu le message\n");
close(sock);
exit(EXIT_FAILURE);
}
// Affichage
fprintf(stderr, "%d\n", n);
// arrêt
close(sock);
exit(EXIT_SUCCESS);
}
Unfortunately, when I test my code, the client's address and port number given by the server seems to be totally random (except that sometimes it gives the right address, but I never saw the correct port number).
Here's an example:
client console
thomas@thomasCoding:~/Code/2020/Reseau/TP_4$ gcc -g -Wall -Wextra -Og -o client client.c
thomas@thomasCoding:~/Code/2020/Reseau/TP_4$ ./client 127.0.0.1 3333
---------
Adresse: 127.0.0.1
Port: 3333
---------
thomas@thomasCoding:~/Code/2020/Reseau/TP_4$ ./client 127.0.0.1 3333
---------
Adresse: 127.0.0.1
Port: 3333
---------
server console
thomas@thomasCoding:~/Code/2020/Reseau/TP_4$ gcc -g -Wall -Wextra -Og -o serveur serveur.c
thomas@thomasCoding:~/Code/2020/Reseau/TP_4$ ./serveur 3333
---------
Adresse: 6.127.0.0
Port: 16578
---------
Le serveur n'a pas recu le message
thomas@thomasCoding:~/Code/2020/Reseau/TP_4$ ./serveur 3333
---------
Adresse: 127.0.0.1
Port: 55098
---------
7
If someone can help to figure my errors out. Thanks :)