Thread: read write help?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    read write help?

    i have created the socket, checked if the socket is ok, binded the socket, checked if the binding is ok, listen for connection, checked if the listning is ok, and accept connection and check if the accepting is ok.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    #define PEND 10
    
    
    int main(int argc, char *argv[])
    {
    	/* listen on listen_fd, new connection on new_fd */
    	int listen_fd, new_fd;
    	/* my address information, address where I run this program */
    	struct sockaddr_in my_addr;
    	/* remote address information */
    	struct sockaddr_in there_addr;
    	int size;
    	int port;
    	
    	
    	
    	if (argc < 2) 
    {
            	 printf(stderr,"ERROR, no port provided\n");
            	 exit(1)
    }
    
    
    	lisen_fd = socket(AF_INET, SOCK_STREAM, 0);
    	if(sockfd == -1)
    {
     		 perror("socket error!");
      		 exit(1);
    }
    	
    	else
      	printf("socket is OK\n");
      	
      	/* pass port through command line */
      	port = atoi(argv[1]);
      	/* host byte order */
    	my_addr.sin_family = AF_INET;
    	/* network byte order */
    	my_addr.sin_port = htons(port);
    	/* use my IP address */
    	my_addr.sin_addr.s_addr = INADDR_ANY;	
    	
    	memset(&(my_addr.sin_zero), 0, 8);
    	
    	if(bind(listen_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
    {
     	 perror("bind error!");
      	 exit(1);
    }
    
    	else
    	printf("bind is OK...\n");
    
    	if(listen(listen_fd, PEND) == -1)
    {
    
     	 perror("listen error!");
      	 exit(1);
    }
    
    	else
    	printf("listen is OK...\n");
    
    	size = sizeof(struct sockaddr_in);
    	new_fd = accept(listen_fd, (struct sockaddr *)&there_addr, &size);
    
    	if(new_fd == -1)
    	perror("accept error!");
    
    	else
      	printf("accept is OK...\n");
      
       	close(new_fd);
    	close(listen_fd);
    	return 0;
    }
    now i want to send my html file thats on my filesystem through the socket and i do not know how to go about that. i know i have to accept read then write but i do not know how to put that in practice.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Beej


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    i can send a message i just cant send a file

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have to send a file the same way you send a message. You open the file on the one end, send the name of the file over (when you're expecting the name), open the file for writing on the other end, read from the first end and write to the other, until you run out of file. Then send a message saying you're at the end of the file.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    would ut be something like this ?

    Code:
            FILE *open;
    	open = fopen("/home/index.html","w");
            int bytes_sent;
    while (( n=fread(buffer,1,sizeof buffer, open)) > 0)
    {
    		
     	while(n != bytes_sent)
    {
    		
    		bytes_sent = send(new_fd,open,0,255);
    } }
       	close(new_fd);
    	close(listen_fd);
    	return 0;
    }
    i can send a message

    Code:
    	char *msg = "GET %s HTTP/1.0\r\nHOST:%s \r\n\r\n";
    	int len; 
    	int bytes_sent;
    	len = strlen(msg);
    	
    	
    	
     	while(len != bytes_sent)
    {
    		
    		bytes_sent = send(new_fd,msg,len,0);
    } 
       	close(new_fd);
    	close(listen_fd);
    	return 0;
    }
    Last edited by moroccanplaya; 03-31-2011 at 04:22 AM.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    ?????

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You waited 12 hours just to bump your own thread without actually just trying it to see if it worked? If you really wrote your own client/server and understand what it's doing, what's keeping you from trying what I mentioned earlier?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  2. Write and read in random acces file
    By lio in forum C Programming
    Replies: 2
    Last Post: 11-27-2010, 04:46 PM
  3. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 05:31 PM
  4. write(), read()
    By RedRum in forum C++ Programming
    Replies: 5
    Last Post: 06-09-2002, 11:45 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM