Code:
#include "server.h"
int main(int argc, char** argv)
{
FILE* myfile;
int sock,newsock,serverlen, i;
pthread_t *tids;
struct sockaddr_in client;
struct sockaddr* serverptr, *clientptr;
orisma or;
char buf[50];
int myfd;
ssize_t bytesread;
songlist.songcnt = 0;
port = atoi(argv[2]);
strcpy(musicdir, argv[4]);
getsonglist(musicdir);
if((sock=socket(PF_INET, SOCK_STREAM, 0)) < 0 )
{
perror("socket");
exit(1);
}
server.sin_family=PF_INET;
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port=htons(port);
serverptr=(struct sockaddr*) &server;
serverlen=sizeof(server);
if(bind(sock, serverptr, serverlen)<0)
{
perror("bind");
exit(1);
}
if((tids=malloc(THREADS*sizeof(pthread_t)))==NULL)
{
perror("malloc");
exit(1);
}
if(listen(sock, THREADS)< 0)
{
perror("bind");
exit(1);
}
printf("\nListening for connections to port %d\n",port);
while(1)
{
int clientlen,err;
clientptr=(struct sockaddr*) &or.client;
clientlen=sizeof(or.client);
if((or.newsock=accept(sock, clientptr, &clientlen))<0)
{
perror("accept");
exit(1);
}
/*
if(err=pthread_create(tids+i, NULL, thread_f, (void*)&or))
{
perror2("pthread_create", err); +
exit(1);
}
*/
tempfun(or);
printf("\nEnd");
}
}
void tempfun(orisma or)
{
int i,j, addrlen, myfd;
ssize_t bytesread;
char buf1[MAXPATH], buf2[MAXPATH];
char tempbuf[MAXPATH], songpath[MAXPATH], ip[MAXIPSIZE];
char url[MAXPATH];
char* playlist[MAXPATH];
FILE* filelist;
char filename[MAXPATH];
i = 0;
while(read(or.newsock, tempbuf, 1) > 0 && tempbuf[0]!= '\n')
buf1[i++] = tempbuf[0];
buf1[i] = '\0';
strncpy(tempbuf, strstr(buf1, " HTTP/")-3, 4);
tempbuf[3] = '\0';
if(strcmp(tempbuf, "mp3") == 0)
{
for(i = 4, j = 0; buf1[i] != ' '; i++, j++)
tempbuf[j] = buf1[i];
tempbuf[j] = '\0';
url_decode(tempbuf, songpath, strlen(tempbuf)+1, 0);
strcpy(tempbuf, musicdir);
strcat(tempbuf, songpath);
if(song_exists(tempbuf))
{
strcpy(buf1,"HTTP/1.0 200 OK\r\n");
write(or.newsock, buf1, strlen(buf1));
strcpy(buf1,"Server: musicstreamer v1.0\r\n");
write(or.newsock, buf1, strlen(buf1));
strcpy(buf1,"Connection: close\r\n");
write(or.newsock, buf1, strlen(buf1));
strcpy(buf1,"Content-Type: audio/mpeg\r\n\r\n");
write(or.newsock, buf1, strlen(buf1));
myfd = open(tempbuf, O_RDONLY);
bytesread = read(myfd, buf1, sizeof(buf1));
while(bytesread > 0)
{
write(or.newsock, buf1, bytesread);
bytesread = read(myfd, buf1, sizeof(buf1));
}
}
}else if(strcmp(tempbuf, "m3u") == 0)
{
i = 0;
while(read(or.newsock, tempbuf, 1) > 0 && tempbuf[0]!= '\n')
buf2[i++] = tempbuf[0];
buf2[--i] = '\0';
if(strstr(buf2, "Host:") != NULL)
{
for(i = strstr(buf2, "Host: ")+6-buf2, j = 0; buf2[i] != '\0'; i++, j++)
tempbuf[j] = buf2[i];
tempbuf[j] = '\0';
strcpy(ip, tempbuf);
}
else
{
addrlen = sizeof(or.client);
if (getsockname(or.newsock, &or.client, &addrlen) == -1)
{
perror("getsockname() failed");
return -1;
}
strcpy(ip, inet_ntoa(or.client.sin_addr));
printf("Local IP address is: %s\n", inet_ntoa(or.client.sin_addr));
printf("Local port is: %d\n", (int) ntohs(or.client.sin_port));
}
for(i = 5, j = 0; buf1[i] != ' '; i++, j++)
tempbuf[j] = buf1[i];
tempbuf[j] = '\0';
url_decode(tempbuf, songpath, strlen(tempbuf)+1, 0);
if(strstr(tempbuf, "songsearch") != NULL)
{
for(i = strstr(tempbuf, "songsearch")+strlen("songsearch") + 1 -tempbuf, j = 0; tempbuf[i] != '\0'; i++, j++)
songpath[j] = tempbuf[i];
songpath[j-4] = '\0';
if((j = create_m3ulist(songpath, ip, filename)) > 0)
{
if((filelist = fopen(filename, "r")) == NULL)
{
perror("fopen");
exit(1);
}
while(fgets(tempbuf, sizeof(tempbuf), filelist) != NULL)
write(or.newsock, tempbuf, strlen(tempbuf));
fclose(filelist);
}
}
}
}
int create_m3ulist(char* word, char* ip, char filename[MAXPATH])
{
FILE* file;
int i, cnt = 0;
char* playlist[MAXPATH];
//char filename[MAXPATH];
char buf[MAXPATH],tempbuf[MAXPATH];
strcpy(filename, word);
strcat(filename, ".m3u");
if((cnt = findsongs(word, playlist)) > 0)
{
if((file = fopen(filename, "w")) == NULL)
{
perror("fopen");
return -1;
}
strcpy(buf,"HTTP/1.0 200 OK\r\n");
fwrite(buf, 1, strlen(buf), file);
strcpy(buf,"Server: musicstreamer v1.0\r\n");
fwrite(buf, 1, strlen(buf), file);
strcpy(buf,"Connection: close\r\n");
fwrite(buf, 1, strlen(buf), file);
strcpy(buf,"Content-Type: audio/x-mpegurl\r\n\r\n");
fwrite(buf, 1, strlen(buf), file);
for(i = 0; i < cnt; i++)
{
strcpy(buf, "http://");
strcat(buf, ip);
//strcat(buf, "/");
url_encode(playlist[i], tempbuf, sizeof(tempbuf), 1);
strcat(buf, tempbuf);
strcat(buf, "\n");
fwrite(buf, 1, strlen(buf), file);
}
fclose(file);
}
return cnt;
}
Thanks in advance