mirror of
https://github.com/yoboujon/tsock.git
synced 2025-06-08 14:00:50 +02:00
Arrangement dans un unique fichier main avec arguments. Création de tsock pour gérer le puit/source
This commit is contained in:
parent
9c9541fe09
commit
8f52358f0e
7 changed files with 73 additions and 91 deletions
4
main.c
4
main.c
|
@ -1,4 +1,6 @@
|
||||||
#include "tsock.h"
|
#include "tsock.h"
|
||||||
|
#include "puit.h"
|
||||||
|
#include "source.h"
|
||||||
|
|
||||||
int main (int argc, char **argv)
|
int main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
@ -43,9 +45,11 @@ int main (int argc, char **argv)
|
||||||
if(source)
|
if(source)
|
||||||
{
|
{
|
||||||
printf("Source : %d\n",nb_message);
|
printf("Source : %d\n",nb_message);
|
||||||
|
launchSource();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Puit : %d\n",nb_message);
|
printf("Puit : %d\n",nb_message);
|
||||||
|
launchPuit();
|
||||||
}
|
}
|
||||||
}
|
}
|
67
puit.c
67
puit.c
|
@ -1,73 +1,26 @@
|
||||||
#include <stdlib.h>
|
#include "puit.h"
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/un.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#define _OE_SOCKETS
|
int launchPuit(void)
|
||||||
#define PORT_NUM 9000
|
|
||||||
|
|
||||||
void initSocketAddr(struct sockaddr_in *socketServerTemp, int source);
|
|
||||||
void creationSocket(int *socketTemp, struct sockaddr_in *socketTempStruct);
|
|
||||||
|
|
||||||
|
|
||||||
char buffer[1024];
|
|
||||||
struct sockaddr_in socketClient;
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
{
|
||||||
int sock;
|
int sock;
|
||||||
creationSocket(&sock,&socketClient);
|
char buffer[1024];
|
||||||
initSocketAddr(&socketClient,0);
|
struct sockaddr_in socketPuit;
|
||||||
|
|
||||||
if (bind(sock, (const struct sockaddr *)&socketClient, sizeof(socketClient)) < 0 )
|
creationSocket(&sock,&socketPuit);
|
||||||
|
initSocketAddr(&socketPuit,0);
|
||||||
|
|
||||||
|
if (bind(sock, (const struct sockaddr *)&socketPuit, sizeof(socketPuit)) < 0 )
|
||||||
{
|
{
|
||||||
perror("bind failed");
|
perror("bind failed");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int n;
|
int n;
|
||||||
unsigned int lg_socketServer = sizeof(socketClient);
|
unsigned int lg_socketPuit = sizeof(socketPuit);
|
||||||
|
|
||||||
n = recvfrom(sock, (char *)buffer, 1024, 0, (struct sockaddr*) &socketClient,&lg_socketServer);
|
n = recvfrom(sock, (char *)buffer, 1024, 0, (struct sockaddr*) &socketPuit,&lg_socketPuit);
|
||||||
buffer[n] = '\0';
|
buffer[n] = '\0';
|
||||||
printf("Server : %s\n", buffer);
|
printf("Server : %s\n", buffer);
|
||||||
close(sock);
|
close(sock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void initSocketAddr(struct sockaddr_in *socketTempStruct, int source)
|
|
||||||
{
|
|
||||||
socketTempStruct->sin_family=AF_INET;
|
|
||||||
socketTempStruct->sin_port=htons(PORT_NUM);
|
|
||||||
if(source)
|
|
||||||
{
|
|
||||||
struct hostent *hp;
|
|
||||||
if((hp = gethostbyname("localhost")) == NULL)
|
|
||||||
{
|
|
||||||
printf("erreur gethostbyname\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
memcpy((char*)&(socketTempStruct->sin_addr.s_addr),hp->h_addr_list[0],hp->h_length);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
socketTempStruct->sin_addr.s_addr=INADDR_ANY;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void creationSocket(int *socketTemp, struct sockaddr_in *socketTempStruct){
|
|
||||||
|
|
||||||
if((*socketTemp=socket(AF_INET,SOCK_DGRAM,0)) == -1)
|
|
||||||
{
|
|
||||||
printf("échec création du socket\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
memset(socketTempStruct, 0, sizeof(*socketTempStruct));
|
|
||||||
|
|
||||||
}
|
|
7
puit.h
Normal file
7
puit.h
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef HEADER_PUIT
|
||||||
|
#define HEADER_PUIT
|
||||||
|
|
||||||
|
#include "tsock.h"
|
||||||
|
int launchPuit(void);
|
||||||
|
|
||||||
|
#endif
|
43
source.c
43
source.c
|
@ -1,45 +1,20 @@
|
||||||
#include <stdlib.h>
|
#include "source.h"
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/un.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#define PORT_NUM 9000
|
int launchSource(void)
|
||||||
|
|
||||||
void initSocket();
|
|
||||||
|
|
||||||
int sock;
|
|
||||||
int lg_emis;
|
|
||||||
struct hostent *hp;
|
|
||||||
struct sockaddr_in socketClient;
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
{
|
||||||
|
int sock;
|
||||||
|
int lg_emis;
|
||||||
|
struct hostent *hp;
|
||||||
|
struct sockaddr_in socketSource;
|
||||||
if((sock=socket(AF_INET,SOCK_DGRAM,0)) == -1)
|
if((sock=socket(AF_INET,SOCK_DGRAM,0)) == -1)
|
||||||
{
|
{
|
||||||
printf("échec création du socket\n");
|
printf("échec création du socket\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
memset(&socketClient, 0, sizeof(socketClient));
|
memset(&socketSource, 0, sizeof(socketSource));
|
||||||
initSocket();
|
initSocketAddr(&socketSource,1);
|
||||||
lg_emis=sendto(sock, "aaaaa", 6, 0, (struct sockaddr*)&socketClient, sizeof(socketClient));
|
lg_emis=sendto(sock, "aaaaa", 6, 0, (struct sockaddr*)&socketSource, sizeof(socketSource));
|
||||||
printf("lgemis = %d\n",lg_emis);
|
printf("lgemis = %d\n",lg_emis);
|
||||||
close(sock);
|
close(sock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void initSocket()
|
|
||||||
{
|
|
||||||
socketClient.sin_family=AF_INET;
|
|
||||||
socketClient.sin_port=htons(PORT_NUM);
|
|
||||||
if((hp = gethostbyname("localhost")) == NULL)
|
|
||||||
{
|
|
||||||
printf("erreur gethostbyname\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
memcpy((char*)&(socketClient.sin_addr.s_addr),hp->h_addr_list[0],hp->h_length);
|
|
||||||
}
|
|
7
source.h
Normal file
7
source.h
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef HEADER_SOURCE
|
||||||
|
#define HEADER_SOURCE
|
||||||
|
|
||||||
|
#include "tsock.h"
|
||||||
|
int launchSource(void);
|
||||||
|
|
||||||
|
#endif
|
31
tsock.c
31
tsock.c
|
@ -20,3 +20,34 @@ void printInfo(int nb, int source)
|
||||||
nb != -1 ? printf("%d\n", nb) : printf("infini\n");
|
nb != -1 ? printf("%d\n", nb) : printf("infini\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void initSocketAddr(struct sockaddr_in *socketTempStruct, int source)
|
||||||
|
{
|
||||||
|
socketTempStruct->sin_family=AF_INET;
|
||||||
|
socketTempStruct->sin_port=htons(PORT_NUM);
|
||||||
|
if(source)
|
||||||
|
{
|
||||||
|
struct hostent *hp;
|
||||||
|
if((hp = gethostbyname("localhost")) == NULL)
|
||||||
|
{
|
||||||
|
printf("erreur gethostbyname\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
memcpy((char*)&(socketTempStruct->sin_addr.s_addr),hp->h_addr_list[0],hp->h_length);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
socketTempStruct->sin_addr.s_addr=INADDR_ANY;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void creationSocket(int *socketTemp, struct sockaddr_in *socketTempStruct)
|
||||||
|
{
|
||||||
|
if((*socketTemp=socket(AF_INET,SOCK_DGRAM,0)) == -1)
|
||||||
|
{
|
||||||
|
printf("échec création du socket\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
memset(socketTempStruct, 0, sizeof(*socketTempStruct));
|
||||||
|
}
|
5
tsock.h
5
tsock.h
|
@ -13,5 +13,10 @@
|
||||||
|
|
||||||
void setNbMessage(int * nb, int source);
|
void setNbMessage(int * nb, int source);
|
||||||
void printInfo(int nb, int source);
|
void printInfo(int nb, int source);
|
||||||
|
void initSocketAddr(struct sockaddr_in *socketServerTemp, int source);
|
||||||
|
void creationSocket(int *socketTemp, struct sockaddr_in *socketTempStruct);
|
||||||
|
|
||||||
|
#define _OE_SOCKETS
|
||||||
|
#define PORT_NUM 9000
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Add table
Reference in a new issue