i cant seem to send my html file through a socket, when vere i connect to the server via web browser http://localhost:7000 it just freezes. and the server stops

open_ptr = open("/home/user/www/home.html",'r'); 
  
         while((n = read(open_ptr,buffer,256)) > 0)
     {
  
          n = write( new_sock,buffer,256); 

     }
  
         close(open_ptr);   
         return 0; 
      }

i cant seem to send my html file through a socket, when vere i connect to the server via web browser http://localhost:7000 it just freezes. and the server stops

open_ptr = open("/home/user/www/home.html",'r'); 
  
         while((n = read(open_ptr,buffer,256)) > 0)
     {
  
          n = write( new_sock,buffer,256); 

     }
  
         close(open_ptr);   
         return 0; 
      }

Are you checking the results of the write function? Is it returning -1 and is the errno set?
Also, did you check to see if the file opened correctly?... What are you passing to open, how did this even compile?
Here's the definition of open.

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

Your using open like it was fopen.

FILE *fopen(const char *path, const char *mode);

Edited 5 Years Ago by gerard4143: n/a

sorry i missed out the f in fopen, i use if (open_ptr == NULL) THen error opening the file

somehow it did compile the server listens when i try to connect to it via my web browser it just disconnects

somehow it did compile the server listens when i try to connect to it via my web browser it just disconnects

You have to use open if your going to use write/read. The reason your program compiled with open was that 'r' is a character value that can be implicitly converted to int. Like I said in the first post, check for success or failure on calls to open and write.

Edited 5 Years Ago by gerard4143: n/a

open_ptr = fopen("/home/user/www/home.html","r");
	if(open_ptr == NULL){
	printf("ERROR OPENING FILE"); 
	}
	while((n = fread(buffer,1,sizeof buffer, open_ptr)) > 0)
	{
	
	fwrite(new_sock,buffer,n,open_ptr); 
	
	}

i do not know what i am missing ?

Edited 5 Years Ago by moroccanplaya: n/a

open_ptr = fopen("/home/user/www/home.html","r");
	if(open_ptr == NULL){
	printf("ERROR OPENING FILE"); 
	}
	while((n = fread(buffer,1,sizeof buffer, open_ptr)) > 0)
	{
	
	fwrite(new_sock,buffer,n,open_ptr); 
	
	}

i do not know what i am missing ?

Try changing line 8 back to what it was

n = write( new_sock,buffer,n);/*note only write the number of bytes read*/

Now check the value of n with each write.

Edited 5 Years Ago by gerard4143: n/a

so do i need to change fread to just read ?

No, its O.K. to keep the fread().

Edited 5 Years Ago by gerard4143: n/a

This article has been dead for over six months. Start a new discussion instead.