1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(param) close(param)
#define PORT 8003
static const unsigned char base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int base64_encode(const unsigned char *src, size_t len, char* outStr);
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
void main(){
system("fuser -k 8003/tcp");
usleep(100000);
struct sockaddr_in
{
short sin_family;
unsigned short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
SOCKET sock;
SOCKADDR_IN sin;
SOCKET csock;
SOCKADDR_IN csin;
socklen_t recsize = sizeof(csin);
int sock_err;
char buffer[5000];
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock != INVALID_SOCKET)
{
printf("Le socket %d est maintenant ouvert en mode TCP/IP\n", sock);fflush(stdout);
/* Configuration */
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sock_err = bind(sock, (SOCKADDR*)&sin, sizeof(sin));
if(sock_err == SOCKET_ERROR){printf("erreur socket");}
if(sock_err != SOCKET_ERROR)
{
sock_err = listen(sock, 5);
printf("Listage du port %d...\n", PORT);fflush(stdout);
if(sock_err != SOCKET_ERROR)
{
printf("Attente de connexion sur le port %d...\n", PORT);fflush(stdout);
csock = accept(sock, (SOCKADDR*)&csin, &recsize);
printf("Un client se connecte avec le socket %d de %s:%d\n", csock, inet_ntoa(csin.sin_addr), htons(csin.sin_port));
fflush(stdout);
recv(csock,buffer,sizeof(buffer),0);
printf("Réponse du client : %s\n",buffer);fflush(stdout);
closesocket(sock);
//--------------------- Entête wav (44 octects)-----------------------------------
char buff[100000];
unsigned long nsample=48000; // 1s avec échantillonage à 48000 Hz
//sprintf(buff,"RIFF%luWAVEfmt %lu%u%u%u%lu%u%udata%lu",36+nsample*2,16L,1,1,48000,4*48000L,2,16,nsample*2);
FILE* ent=fopen("entete","rb");
fread(buff,44,1,ent);
fclose(ent);
//------------------------------------
sprintf(buffer,"HTTP/1.1 200 OK\r\n"
"Content-length:100000000\r\n"
"Content-Type: text/html\r\n\r\n"
);
send(csock,buffer,strlen(buffer), 0);
printf("Envoyé:\n%s\n\n",buff);fflush(stdout);
bzero(buffer,5000);
recv(csock,buffer,sizeof(buffer),MSG_DONTWAIT);
usleep(100000);
printf("Réponse du client : %s\n",buffer);fflush(stdout);
bzero(buffer,5000);
int i=0;
int len=0;
FILE* js=fopen("ecouter2.php","r");
while (fread(&buffer[i],1,1,js)>0){i++;}
fclose(js);
send(csock,buffer,i,0);
FILE* f=fopen("../test/dat.wav","r");
int tot=0;
int num=0;
char out[400000];
char out2[100];
while(1) //condition d'arrêt non encore implémentée
{
fread(&buff[44],96000,1,f);
len=base64_encode(buff,96044,out);
sprintf(out2,"<script type='text/javascript'>tab[%d]='data:audio/wav;base64,",num);num++;
send(csock,out2,strlen(out2),0);
send(csock,out,len,0);
sprintf(out2,"';</script>\n");
send(csock,out2,strlen(out2),0);
usleep(900000);
}
}
}
}
}
int base64_encode(const unsigned char *src, size_t len, char* outStr)
{
unsigned char *out, *pos;
const unsigned char *end, *in;
int olen;
olen = 4*((len + 2) / 3); /* 3-byte blocks to 4-byte */
out = (unsigned char*)&outStr[0];
end = src + len;
in = src;
pos = out;
while (end - in >= 3) {
*pos++ = base64_table[in[0] >> 2];
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
*pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
*pos++ = base64_table[in[2] & 0x3f];
in += 3;
}
if (end - in) {
*pos++ = base64_table[in[0] >> 2];
if (end - in == 1) {
*pos++ = base64_table[(in[0] & 0x03) << 4];
*pos++ = '=';
}
else {
*pos++ = base64_table[((in[0] & 0x03) << 4) |
(in[1] >> 4)];
*pos++ = base64_table[(in[1] & 0x0f) << 2];
}
*pos++ = '=';
}
outStr[olen]='\0';
return olen;
} |