mirror of
https://github.com/yoboujon/tsock.git
synced 2025-06-08 14:00:50 +02:00
commit
5ddc3f3039
12 changed files with 152 additions and 141 deletions
|
@ -1,2 +1,6 @@
|
||||||
# tsock
|
# tsock
|
||||||
Using C's standard UDP/TCP Library, send and receive data through the Internet.
|
Using C's standard UDP/TCP Library, send and receive data through the Internet.
|
||||||
|
Pour compiler :
|
||||||
|
```
|
||||||
|
gcc -Wall src/*.c -o main
|
||||||
|
```
|
8
header/puit.h
Normal file
8
header/puit.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef HEADER_PUIT
|
||||||
|
#define HEADER_PUIT
|
||||||
|
|
||||||
|
#include "tsock.h"
|
||||||
|
int launchPuit(int nombreMessage);
|
||||||
|
int receiveMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct);
|
||||||
|
|
||||||
|
#endif
|
8
header/source.h
Normal file
8
header/source.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef HEADER_SOURCE
|
||||||
|
#define HEADER_SOURCE
|
||||||
|
#include "tsock.h"
|
||||||
|
|
||||||
|
int launchSource(int nombreMessage);
|
||||||
|
int sendMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct);
|
||||||
|
|
||||||
|
#endif
|
|
@ -13,5 +13,8 @@
|
||||||
|
|
||||||
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 initStructSocket(struct sockaddr_in *socketServerTemp, int source);
|
||||||
|
|
||||||
|
#define PORT_NUM 9000
|
||||||
|
|
||||||
#endif
|
#endif
|
73
puit.c
73
puit.c
|
@ -1,73 +0,0 @@
|
||||||
#include <stdlib.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
|
|
||||||
#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;
|
|
||||||
creationSocket(&sock,&socketClient);
|
|
||||||
initSocketAddr(&socketClient,0);
|
|
||||||
|
|
||||||
if (bind(sock, (const struct sockaddr *)&socketClient, sizeof(socketClient)) < 0 )
|
|
||||||
{
|
|
||||||
perror("bind failed");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
int n;
|
|
||||||
unsigned int lg_socketServer = sizeof(socketClient);
|
|
||||||
|
|
||||||
n = recvfrom(sock, (char *)buffer, 1024, 0, (struct sockaddr*) &socketClient,&lg_socketServer);
|
|
||||||
buffer[n] = '\0';
|
|
||||||
printf("Server : %s\n", buffer);
|
|
||||||
close(sock);
|
|
||||||
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));
|
|
||||||
|
|
||||||
}
|
|
BIN
source
BIN
source
Binary file not shown.
45
source.c
45
source.c
|
@ -1,45 +0,0 @@
|
||||||
#include <stdlib.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
|
|
||||||
|
|
||||||
void initSocket();
|
|
||||||
|
|
||||||
int sock;
|
|
||||||
int lg_emis;
|
|
||||||
struct hostent *hp;
|
|
||||||
struct sockaddr_in socketClient;
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
if((sock=socket(AF_INET,SOCK_DGRAM,0)) == -1)
|
|
||||||
{
|
|
||||||
printf("échec création du socket\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
memset(&socketClient, 0, sizeof(socketClient));
|
|
||||||
initSocket();
|
|
||||||
lg_emis=sendto(sock, "aaaaa", 6, 0, (struct sockaddr*)&socketClient, sizeof(socketClient));
|
|
||||||
printf("lgemis = %d\n",lg_emis);
|
|
||||||
close(sock);
|
|
||||||
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);
|
|
||||||
}
|
|
|
@ -1,4 +1,6 @@
|
||||||
#include "tsock.h"
|
#include "../header/tsock.h"
|
||||||
|
#include "../header/puit.h"
|
||||||
|
#include "../header/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(nb_message);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Puit : %d\n",nb_message);
|
printf("Puit : %d\n",nb_message);
|
||||||
|
launchPuit(nb_message);
|
||||||
}
|
}
|
||||||
}
|
}
|
48
src/puit.c
Normal file
48
src/puit.c
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
#include "../header/puit.h"
|
||||||
|
|
||||||
|
int launchPuit(int nombreMessage)
|
||||||
|
{
|
||||||
|
int sock;
|
||||||
|
struct sockaddr_in socketPuit;
|
||||||
|
|
||||||
|
if((sock=socket(AF_INET,SOCK_DGRAM,0)) == -1)
|
||||||
|
{
|
||||||
|
perror("[tsock] : fonction socket() : echec creation du socket\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
initStructSocket(&socketPuit,0);
|
||||||
|
if (bind(sock, (const struct sockaddr *)&socketPuit, sizeof(socketPuit)) < 0 )
|
||||||
|
{
|
||||||
|
perror("[tsock] : fonction bind() : echec du lien avec socket serveur.\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
receiveMultipleData(nombreMessage,30,sock,socketPuit);
|
||||||
|
close(sock);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int receiveMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct)
|
||||||
|
{
|
||||||
|
char messageRecu[tailleMessage+1];
|
||||||
|
int n, longueurRecu = sizeof(socketStruct);
|
||||||
|
if(nombreMessages < 0)
|
||||||
|
{
|
||||||
|
int i=1;
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
n = recvfrom(sock, (char *)messageRecu, tailleMessage, 0, (struct sockaddr*) &socketStruct,(socklen_t *__restrict__)&longueurRecu);
|
||||||
|
messageRecu[n] = '\0';
|
||||||
|
printf("Puit\tReception n°%d (%d) :\t[%s]\n",i,tailleMessage,messageRecu);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
for(int i=0;i<nombreMessages;i++)
|
||||||
|
{
|
||||||
|
n = recvfrom(sock, (char *)messageRecu, tailleMessage, 0, (struct sockaddr*) &socketStruct,(socklen_t *__restrict__)&longueurRecu);
|
||||||
|
messageRecu[n] = '\0';
|
||||||
|
printf("Puit\tReception n°%d (%d) :\t[%s]\n",i+1,tailleMessage,messageRecu);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
32
src/source.c
Normal file
32
src/source.c
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include "../header/source.h"
|
||||||
|
|
||||||
|
int launchSource(int nombreMessage)
|
||||||
|
{
|
||||||
|
int sock;
|
||||||
|
struct sockaddr_in socketSource;
|
||||||
|
|
||||||
|
if((sock=socket(AF_INET,SOCK_DGRAM,0)) == -1)
|
||||||
|
{
|
||||||
|
perror("[tsock] : fonction socket() : echec creation du socket\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
initStructSocket(&socketSource,1);
|
||||||
|
sendMultipleData(nombreMessage,30,sock,socketSource);
|
||||||
|
close(sock);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sendMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct)
|
||||||
|
{
|
||||||
|
int longueurEmis;
|
||||||
|
char messageChar='a';
|
||||||
|
char sendingMessage[tailleMessage];
|
||||||
|
for(int i=0;i<nombreMessages;i++)
|
||||||
|
{
|
||||||
|
memset(sendingMessage, messageChar, tailleMessage*sizeof(char));
|
||||||
|
longueurEmis=sendto(sock, sendingMessage, tailleMessage, 0, (struct sockaddr*)&socketStruct, sizeof(socketStruct));
|
||||||
|
printf("Source\tEnvoi n°%d (%d) :\t[%s]\n",i+1,longueurEmis,sendingMessage);
|
||||||
|
messageChar++;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
44
src/tsock.c
Normal file
44
src/tsock.c
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
#include "../header/tsock.h"
|
||||||
|
|
||||||
|
void setNbMessage(int * nb, int source)
|
||||||
|
{
|
||||||
|
if((*nb == -1) && (source))
|
||||||
|
{
|
||||||
|
*nb = 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printInfo(int nb, int source)
|
||||||
|
{
|
||||||
|
if(source)
|
||||||
|
{
|
||||||
|
printf("tsock lance en mode source, nombre de tampons à envoyer : %d\n", nb);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("tsock lance en mode puit, nombre de tampons à recevoir :");
|
||||||
|
nb != -1 ? printf("%d\n", nb) : printf("infini\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void initStructSocket(struct sockaddr_in *socketTempStruct, int source)
|
||||||
|
{
|
||||||
|
memset(socketTempStruct, 0, sizeof(*socketTempStruct));
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
22
tsock.c
22
tsock.c
|
@ -1,22 +0,0 @@
|
||||||
#include "tsock.h"
|
|
||||||
|
|
||||||
void setNbMessage(int * nb, int source)
|
|
||||||
{
|
|
||||||
if((*nb == -1) && (source))
|
|
||||||
{
|
|
||||||
*nb = 10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void printInfo(int nb, int source)
|
|
||||||
{
|
|
||||||
if(source)
|
|
||||||
{
|
|
||||||
printf("tsock lance en mode source, nombre de tampons à envoyer : %d\n", nb);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("tsock lance en mode puit, nombre de tampons à recevoir :");
|
|
||||||
nb != -1 ? printf("%d\n", nb) : printf("infini\n");
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue