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; 
      }

Recommended Answers

All 9 Replies

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);

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.

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 ?

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.

so do i need to change fread to just read ?

so do i need to change fread to just read ?

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

so if i add

send(new_sock,buffer, strlen(buffer),0);

would this send the file ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.