-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
632 lines (499 loc) · 22.6 KB
/
client.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*
sudo apt-get install libgtk2.0-dev
gcc client.c -o c -pthread `pkg-config --cflags --libs gtk+-2.0`
sudo ./c 0.0.0.0 5432 wlp2s0
sudo kill -9 $(sudo lsof -t -i:5464)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <time.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>
#include <gtk/gtk.h>
struct args
{
int port;
char* ip;
char* name;
};
struct args ptr;
//ptr=(struct args*)(malloc(sizeof(struct args)));
//#define MC_PORT 1723 // 1723 , 5499
#define BUF_SIZE 4096
int MC_PORT = 41054;
int INFO_PORT = 1723;
int true;
int choice;
int dava2 = 0;
int currentStationNumber;
pthread_t thread_id_song,thread_id_song_info;
pthread_t thread_id_vlc;
int flag = 1;
int fffff=0;
int s; /* socket descriptor */
int sInfo;
int change;
int songInfoWindowGTK=1;
GtkWidget *labelGtkSongInfo;
//char buf[BUF_SIZE];
void *myThreadFun(void *vargp);
void *startStream(void *vargp);
void *tcp(void *vargp);
void *infoThread(void *vargp);
struct ip_mreq mcast_req;
struct sockaddr_in mcast_saddr;
socklen_t mcast_saddr_len;
int myflag=1;
GtkWidget *windowStationSelect;
struct station_info{
uint8_t station_number;
uint8_t station_name_size;
char station_name[256];
uint32_t multicast_address;
uint16_t data_port;
uint16_t info_port;
uint32_t bit_rate;
};
typedef struct {
uint8_t type;
uint8_t site_name_size;
char site_name[256];
uint8_t site_desc_size;
char site_desc[256];
uint8_t station_count;
struct station_info station_list[10] ;
}site_info;
typedef struct {
uint8_t type;
uint8_t song_name_size ;
char song_name[ 256 ];
uint16_t remaining_time_in_sec;
uint8_t next_song_name_size;
char next_song_name[ 256 ];
}song_info;
song_info current_song_info;
void *stationInfo(void *vargp);
static void oneListener (){
choice = 1;
if(flag==1){
pthread_create(&thread_id_song, NULL, startStream,NULL);
sleep(1);
pthread_create(&thread_id_song_info, NULL, infoThread,NULL);
}
else
dava2 = 3 ;
gtk_widget_destroy(windowStationSelect);
}
static void twoListener (){
choice = 2;
if(flag==1){
pthread_create(&thread_id_song, NULL, startStream,NULL);
sleep(1);
pthread_create(&thread_id_song_info, NULL, infoThread,NULL);
}
else
dava2 = 3;
gtk_widget_destroy(windowStationSelect);
}
static void threeListener (){
choice = 3;
if(flag==1){
pthread_create(&thread_id_song, NULL, startStream,NULL);
sleep(1);
pthread_create(&thread_id_song_info, NULL, infoThread,NULL);
}else
dava2 = 3;
gtk_widget_destroy(windowStationSelect);
}
static void fourListener (){
choice = 4;
if(flag==1){
pthread_create(&thread_id_song, NULL, startStream,NULL);
sleep(1);
pthread_create(&thread_id_song_info, NULL, infoThread,NULL);
}else
dava2 = 3;
gtk_widget_destroy(windowStationSelect);
}
static void fiveListener (){
choice = 5;
if(flag==1){
pthread_create(&thread_id_song, NULL, startStream,NULL);
sleep(1);
pthread_create(&thread_id_song_info, NULL, infoThread,NULL);
}else
dava2 = 3;
gtk_widget_destroy(windowStationSelect);
}
static void selectStationListener (GtkWidget *widget,gpointer *data)
{
GtkWidget *vbox;
GtkWidget *one;
GtkWidget *two;
GtkWidget *three;
GtkWidget *four;
GtkWidget *five;
GtkWidget *label;
void print_s (){};
void print_a (){};
void print_l (){};
void print_c (){};
void print_d (){};
windowStationSelect = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(windowStationSelect), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(windowStationSelect), 230, 250);
gtk_window_set_title(GTK_WINDOW(windowStationSelect), "Select Station");
gtk_container_set_border_width(GTK_CONTAINER(windowStationSelect), 5);
vbox = gtk_vbox_new(TRUE, 1);
gtk_container_add(GTK_CONTAINER(windowStationSelect), vbox);
one = gtk_button_new_with_label("1");
two = gtk_button_new_with_label("2");
three = gtk_button_new_with_label("3");
four = gtk_button_new_with_label("4");
five = gtk_button_new_with_label("5");
gtk_box_pack_start(GTK_BOX(vbox), one, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), two, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), three, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), four, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), five, TRUE, TRUE, 0);
g_signal_connect (one, "clicked", G_CALLBACK (oneListener), NULL);
g_signal_connect (two, "clicked", G_CALLBACK (twoListener), NULL);
g_signal_connect (three, "clicked", G_CALLBACK (threeListener), NULL);
g_signal_connect (four, "clicked", G_CALLBACK (fourListener), NULL);
g_signal_connect (five, "clicked", G_CALLBACK (fiveListener), NULL);
gtk_widget_show_all(windowStationSelect);
}
static void stationInfoListener (){
pthread_create(&thread_id_song,NULL,tcp,NULL);
sleep(2);
}
static void pauseListener (){
dava2 = 2;
}
static void restartListener (){
dava2 = 1;
}
static void exit00Listener (){
system("pidof ffplay | xargs -I{} kill {}");
exit(0);
}
void *startStream(void *vargp){
////////////////////////////////////////////////////////////////////////////////////////////////////
char buf[BUF_SIZE];
struct ip_mreq mcast_req;
struct sockaddr_in mcast_saddr;
socklen_t mcast_saddr_len;
char *if_name;
if_name = ptr.name;
if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
perror("receiver: socket");
exit(1);
}
struct sockaddr_in sin;
memset((char *)&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(MC_PORT);
if ((bind(s, (struct sockaddr *) &sin, sizeof(sin))) < 0) {
perror("receiver: bind()");
exit(1);
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name , if_name, sizeof(if_name)-1);
if ((setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr,sizeof(ifr))) < 0){
perror("receiver: setsockopt() error");
close(s);
exit(1);
}
int iSetOption = 1;
setsockopt(s , SOL_SOCKET , SO_REUSEADDR ,(char*)&iSetOption , sizeof(iSetOption));
//////////////////////////////////////////////////////////////////////////////////////////////////////
currentStationNumber = choice;
char addressnew[]="239.192.xx.11";
addressnew[8]=(currentStationNumber/10)+'0';
addressnew[9]=(currentStationNumber%10)+'0';
char *mcast_addr;
mcast_addr= (char *)&addressnew;
mcast_req.imr_multiaddr.s_addr = inet_addr(mcast_addr);
mcast_req.imr_interface.s_addr = htonl(INADDR_ANY);
if ((setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mcast_req, sizeof(mcast_req))) < 0) {
printf("%s Not able to join \n",mcast_addr);
perror("mcast join receive: setsockopt()");
exit(1);
}else{
printf("%s Multicast Group Joined \n",mcast_addr);
}
int temp = choice;
FILE *fp;
fp=fopen("recv.mp3","wb");
fclose(fp);
int cnt=0;
int count=1;
while(1) {
if(dava2 > 0){
// dava2 == 3 --> station change
// dava2 == 1 --> restart
// dava2 == 2 --> pause
if(dava2 == 3 || dava2 == 2){
if ((setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void*) &mcast_req, sizeof(mcast_req))) < 0){
printf("%s Not able to drop \n",mcast_addr);
perror("mcast join receive: setsockopt()");
exit(1);
}else{
printf("%s Droped Successfully \n",mcast_addr);
fp=fopen("recv.mp3","wb");
fclose(fp);
system("pidof ffplay | xargs -I{} kill {}");
}
if(dava2 == 2){
while( dava2 != 1 && dava2 != 3);
}
count = 0;
/* if(dava2 == 2)
dava2 = 0;
*/
}
if(dava2 == 3 || dava2 == 1){
currentStationNumber = choice;
char addressnew[]="239.192.xx.11";
addressnew[8]=(currentStationNumber/10)+'0';
addressnew[9]=(currentStationNumber%10)+'0';
char *mcast_addr;
mcast_addr= (char *) &addressnew;
mcast_req.imr_multiaddr.s_addr = inet_addr(mcast_addr);
mcast_req.imr_interface.s_addr = htonl(INADDR_ANY);
if ((setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mcast_req, sizeof(mcast_req))) < 0){
printf("%s Not able to join \n",mcast_addr);
perror("mcast join receive: setsockopt()");
exit(1);
}else{
printf("%s Multicast Group Joined \n",mcast_addr);
fp=fopen("recv.mp3","wb");
// count = 0;
fclose(fp);
}
dava2 = 0;
}
}
memset(&mcast_saddr, 0, sizeof(mcast_saddr));
mcast_saddr_len = sizeof(mcast_saddr);
memset(buf, 0, sizeof(buf));
int len;
if ((len = recvfrom(s, buf, BUF_SIZE, 0, (struct sockaddr*)&mcast_saddr, &mcast_saddr_len)) < 0) {
perror("receiver: recvfrom()");
exit(1);
}
fp=fopen("recv.mp3","a+b");
fwrite(buf,sizeof(buf),1,fp);
fclose(fp);
if(count==3 ){
pthread_create(&thread_id_vlc, NULL, myThreadFun, NULL);
flag = 0;
}
//myflag=0;
cnt++;
count++;
memset(buf, 0, sizeof(buf));
}
close(s);
}
void *infoThread(void *vargp){
////////////////////////////////////////////////////////////////////////////////////////////////////
int s;
char *if_name;
if_name = ptr.name; //take input from command line
if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
perror("receiver: socket");
exit(1);
}
struct sockaddr_in sin;
memset((char *)&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(INFO_PORT);
if ((bind(s, (struct sockaddr *) &sin, sizeof(sin))) < 0) {
perror("receiver: bind()");
exit(1);
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name , if_name, sizeof(if_name)-1);
if ((setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr,sizeof(ifr))) < 0){
perror("receiver: setsockopt() error");
close(s);
exit(1);
}
int iSetOption = 1;
setsockopt(s , SOL_SOCKET , SO_REUSEADDR ,(char*)&iSetOption , sizeof(iSetOption));
//////////////////////////////////////////////////////////////////////////////////////////////////////
GtkWidget *window_p;
GtkWidget *label;
window_p = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window_p), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window_p), "Song Information");
gtk_container_set_border_width(GTK_CONTAINER(window_p), 15);
label = gtk_label_new("BHAI BHAI BHAI BHAI BHAI BHAI BHAI BHAI BHAI BHAI");
gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
gtk_container_add(GTK_CONTAINER(window_p), label);
int firstTime = 0;
while(1) {
char buf[BUF_SIZE];
memset(buf, 0, sizeof(buf));
int len;
if ((len = recvfrom(s, ¤t_song_info, sizeof(current_song_info) , 0, (struct sockaddr*)&mcast_saddr, &mcast_saddr_len)) < 0) {
perror("receiver: recvfrom()");
exit(1);
}else{
printf("Current Song = %s\n",current_song_info.song_name);
printf("Next Song = %s\n",current_song_info.next_song_name);
printf("Left Time = %d\n",current_song_info.remaining_time_in_sec);
if(firstTime){
gtk_widget_show_all(window_p);
printf("Oh Oh\n");
}
firstTime = 0;
/* char stationInfoDD[1024];
sprintf(stationInfoDD, "-----------------------------------------------------------------------------------\n\nCurrent Song : %s\n\n-----------------------------------------------------------------------------------\n\nNext Song : %s\n\n-----------------------------------------------------------------------------------",current_song_info.song_name,current_song_info.next_song_name);
labelGtkSongInfo = gtk_label_new(stationInfoDD);
*/
}
memset(buf, 0, sizeof(buf));
}
close(s);
}
void *tcp(void *vargp){
FILE *fp;
struct hostent *hp;
struct sockaddr_in sin;
char *host;
char buf[256];
int s;
int len;
host = ptr.ip;
hp = gethostbyname(host);
if (!hp){
fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
exit(1);
}
else{
//printf("Client's remote host:");
}
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
sin.sin_port = htons(ptr.port);
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0){
perror("simplex-talk: socket");
exit(1);
}
else{
//printf("Client created socket.\n");
}
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0){
perror("simplex-talk: connect");
close(s);
exit(1);
}
else{
//printf("Client connected.\n");
}
while(1){
site_info mysite;
recv(s,&mysite,sizeof(mysite),0);
int stationCount = mysite.station_count;
char stationInfoDD[1024];
int i=0,j=1,k=2,l=3,m=4;
sprintf(stationInfoDD, "-----------------------------------------------------------------------------------\n\nStation Number : %d\nStation Name : %s\nMulticast Address : %d\nData Port : %d\nInfo Port : %d\nBitrate : %d\n\n-----------------------------------------------------------------------------------\n\nStation Number : %d\nStation Name : %s\nMulticast Address : %d\nData Port : %d\nInfo Port : %d\nBitrate : %d\n\n-----------------------------------------------------------------------------------\n\nStation Number : %d\nStation Name : %s\nMulticast Address : %d\nData Port : %d\nInfo Port : %d\nBitrate : %d\n\n-----------------------------------------------------------------------------------\n\nStation Number : %d\nStation Name : %s\nMulticast Address : %d\nData Port : %d\nInfo Port : %d\nBitrate : %d\n\n-----------------------------------------------------------------------------------",
mysite.station_list[i].station_number,mysite.station_list[i].station_name,mysite.station_list[i].multicast_address,mysite.station_list[i].data_port,mysite.station_list[i].info_port,mysite.station_list[i].bit_rate,
mysite.station_list[j].station_number,mysite.station_list[j].station_name,mysite.station_list[j].multicast_address,mysite.station_list[j].data_port,mysite.station_list[j].info_port,mysite.station_list[j].bit_rate,
mysite.station_list[k].station_number,mysite.station_list[k].station_name,mysite.station_list[k].multicast_address,mysite.station_list[k].data_port,mysite.station_list[k].info_port,mysite.station_list[k].bit_rate,
mysite.station_list[l].station_number,mysite.station_list[l].station_name,mysite.station_list[l].multicast_address,mysite.station_list[l].data_port,mysite.station_list[l].info_port,mysite.station_list[l].bit_rate
);
GtkWidget *window_p;
GtkWidget *label;
window_p = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window_p), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window_p), "Station Information");
gtk_container_set_border_width(GTK_CONTAINER(window_p), 15);
label = gtk_label_new(stationInfoDD);
gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
gtk_container_add(GTK_CONTAINER(window_p), label);
//g_signal_connect(window_p, "destroy",
// G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window_p);
break;
}
close(s);
}
void *myThreadFun(void *vargp){
// system(ffmpeg -i input -f mpegts udp://0.0.0.0:port?pkt_size=188&buffer_size=65535)
char command[] = "";
// system("cat recv.mp3 | ffplay pipe:");
// system("cat recv.mp3 | vlc-wrapper -");
// system("ffplay -nodisp -i recv.mp3");
system("pidof ffplay | xargs -I{} kill {}");
// system("ffplay -nodisp -i recv.mp3 -hide_banner -loglevel panic");
system("ffplay -i recv.mp3 -hide_banner -loglevel panic");
return NULL;
}
int main(int argc, char * argv[]){
// system("sudo kill -9 $(sudo lsof -t -i:5432)");
system("sudo kill -9 $(sudo lsof -t -i:1723)");
system("sudo kill -9 $(sudo lsof -t -i:41054)");
system("sudo kill -9 $(sudo lsof -t -i:5499)");
if(argc==4)
{
ptr.ip=argv[1];
ptr.port=atoi(argv[2]);
printf("%d\n",ptr.port);
ptr.name=argv[3];
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *stationInfo;
GtkWidget *selectStation;
GtkWidget *pause;
GtkWidget *restart;
GtkWidget *exit00;
GtkWidget *label;
int temp = 1;
char **temp2;
gtk_init(&temp, &temp2);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 230, 250);
gtk_window_set_title(GTK_WINDOW(window), "Menu");
gtk_container_set_border_width(GTK_CONTAINER(window), 5);
vbox = gtk_vbox_new(TRUE, 1);
gtk_container_add(GTK_CONTAINER(window), vbox);
stationInfo = gtk_button_new_with_label("Station Info");
selectStation = gtk_button_new_with_label("Select Station");
pause = gtk_button_new_with_label("Pause");
restart = gtk_button_new_with_label("Restart");
exit00 = gtk_button_new_with_label("Exit");
gtk_box_pack_start(GTK_BOX(vbox), stationInfo, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), selectStation, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), pause, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), restart, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), exit00, TRUE, TRUE, 0);
g_signal_connect (stationInfo, "clicked", G_CALLBACK (stationInfoListener), NULL);
g_signal_connect (selectStation, "clicked", G_CALLBACK (selectStationListener), NULL);
g_signal_connect (pause, "clicked", G_CALLBACK (pauseListener), NULL);
g_signal_connect (restart, "clicked", G_CALLBACK (restartListener), NULL);
g_signal_connect (exit00, "clicked", G_CALLBACK (exit00Listener), NULL);
gtk_widget_show_all(window);
gtk_main();
while(1);
}
else
printf("command line argument is missing\n");
return 0;
}