Stream Server

This is a discussion on Stream Server within the Networking/Device Communication forums, part of the General Programming Boards category; One more question : when should i close socketFd and newsockectFd if my server handles clients using threads ?...

  1. #16
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    One more question : when should i close socketFd and newsockectFd if my server handles clients using threads ?

  2. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by ch4 View Post
    One more question : when should i close socketFd and newsockectFd if my server handles clients using threads ?
    newsocketFd, being the client connection, should be closed after the session has terminated. Either when the client suddenly disconnects, or when both sides agree to stop talking to each other. In other words, the handler thread is probably the one which closes the socket.

    socketFd, the listening socket, should remain open for however long you want to continue accepting new clients. In other words, none of the handler threads touch this socket.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #18
    Registered User
    Join Date
    Jun 2009
    Posts
    1
    DO NOT READ THIS, I SOLVED THE PROMBLEM (thanks)

    I'm trying to make a stream server like ch4's, but I have a problem with the m3u list. My code working when I ask for an mp3 song but It's not working with m3u list. With code like this
    Code:
    strcpy(buffer, "http://localhost:9999/home/username/music/test1.mp3\n");
    write(fdsocket, buffer, strlen(buffer));
    strcpy(buffer, "http://localhost:9999/home/username/music/test2.mp3\n");
    write(fdsocket, buffer, strlen(buffer));
    ...
    and having the Content-Type as audio/x-mpegurl, I can read the list in the playlist of the client (vlc player), but I can not hear the mp3 files (the vcl is just reading the list and nothing else). The player just stops when I send the list. What am I suppose to do, to load the songs of the m3u list from my player;
    if it helps, all the code is this
    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

    PS: Please forgive me for my English an my huge post


    edit: I found my fault. Thw only thing I needed it was to close the fd that was returning from accept. So many hours for that stupid mistake
    Last edited by kasmi; 06-22-2009 at 01:56 PM.

  4. #19
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Quote Originally Posted by kasmi View Post
    I'm trying to make a stream server like ch4's
    Trying ??
    This is almost a copy of my schedule.

    2 Cases :
    Same Teacher
    Wrong way to learn from me (because I'm still learning)
    Last edited by ch4; 06-29-2009 at 03:12 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 10:21 PM
  2. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 07:04 PM
  3. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21