-
Notifications
You must be signed in to change notification settings - Fork 0
/
livestreaming.c
125 lines (99 loc) · 3.52 KB
/
livestreaming.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<fcntl.h>
#include<signal.h>
#include<unistd.h>
int main(){
struct sockaddr_in sockserv,sockclient;
int socketfd,clientfd;
socklen_t clientsocklen;
int filefd;
int cnt;
int finalcnt = 0;
int chancecnt;
char buff[BUFSIZ],buff2[BUFSIZ];
signal(SIGPIPE,SIG_IGN);
printf("Signal handler for SIGPIPE over ridden : %s\n",strerror(errno));
socketfd = socket(AF_INET,SOCK_STREAM,0);
printf("Socket Creation: %s\n",strerror(errno));
filefd = open("live_stream2.ogg",O_RDONLY);
printf("File open: %s\n",strerror(errno));
bzero(&sockserv,sizeof(sockserv));
sockserv.sin_family = AF_INET;
sockserv.sin_addr.s_addr = INADDR_ANY;
sockserv.sin_port = htons(8500);
/*sockintermediate.sin_family = AF_INET;
inet_pton(AF_INET,"127.0.0.1",(void*)&sockintermediate.sin_addr.s_addr);
sockintermediate.sin_port = htons(81);
*/
bind(socketfd,(struct sockaddr *)&sockserv,sizeof(sockserv));
printf("Socket Bind: %s\n",strerror(errno));
listen(socketfd,10);
printf("Socket Listen: %s\n",strerror(errno));
clientfd = accept(socketfd,(struct sockaddr*)&sockclient,&clientsocklen);
//chrome sends two requests while firefox 4 only 1
printf("1st Request accepted\n");
read(clientfd,buff,BUFSIZ);
printf("%s\n\n",buff);
strcpy(buff,"HTTP/1.0 200 OK\r\nDate: Tue, 01 Mar 2011 06:14:58 GMT\r\nConnection: close\r\nCache-control: private\r\nContent-type: video/ogg\r\nServer: lighttpd/1.4.26\r\n\r\n");
cnt = send(clientfd,buff,strlen(buff),0);
printf("Sent this reply : %s \nwith size = %d : %s\n",buff,cnt,strerror(errno));
int xcnt = 0;
while(1){
cnt = read(filefd,buff,1025);
printf("Read %d bytes 1 from the file : %s\n",cnt,strerror(errno));
if(xcnt==7)
break;
else if(xcnt > 0 && cnt != 0)
xcnt = 0;
else if(xcnt >=0 && cnt == 0){
sleep(1);
xcnt++;
continue;
}
finalcnt = send(clientfd,buff,cnt,0);
printf("Data written 1 = %d bytes: %s\n",finalcnt,strerror(errno));
if(finalcnt <= 0){
memcpy(buff2,buff,cnt);
finalcnt = cnt;
close(clientfd);
printf("%s\n",strerror(errno));
break;
}
}
if(xcnt == 7)
return 0;
clientfd = accept(socketfd,(struct sockaddr*)&sockclient,&clientsocklen);
printf("2nd Request accepted\n");
read(clientfd,buff,BUFSIZ);
printf("%s\n\n",buff);
strcpy(buff,"HTTP/1.0 200 OK\r\nDate: Tue, 01 Mar 2011 06:14:58 GMT\r\nConnection: close\r\nCache-control: private\r\nContent-type: video/ogg\r\nServer: lighttpd/1.4.26\r\n\r\n");
cnt = send(clientfd,buff,strlen(buff),0);
printf("Sent this reply : %s \nwith size = %d : %s\n",buff,cnt,strerror(errno));
lseek(filefd,0,0);
while(1){
cnt = read(filefd,buff,1025);
printf("Read %d bytes 2 from the file : %s\n",cnt,strerror(errno));
if(xcnt==7)
break;
else if(xcnt > 0 && cnt != 0)
xcnt = 0;
else if(xcnt >=0 && cnt == 0){
sleep(1);
xcnt++;
continue;
}
finalcnt = send(clientfd,buff,cnt,0);
printf("Data written = %d bytes 2 : %s\n",finalcnt,strerror(errno));
if(finalcnt <= 0){
break;
}
}
close(clientfd);
close(socketfd);
return 0;
}