Thread: socket_problem

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    socket_problem

    How can I make this server to send any type of file format apart from html?
    NOTE:SENDING() FUNCTION
    CODE :

    Code:
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet.h>
    #include<string.h>
    #define port 1234
    #define buffer_size 1024
    #define CLEARSCREEN "\x1B[2J"
    #define MAXCONNECTIONS 3
    void sending (int sd,char *f1, char *prt);
    struct sockaddr cli,ser;
    main(){
    
    int d,t=0,r,q,sd,b,i,j,con=1,id;
    char buf[buffer_size+1],*method,*path,*prt;
    char line[buffer_size];
    struct sockaddr_in cli,ser;
    int sw;
    printf(CLEARSCREEN);
    
    d=socket(AF_INET,SOCKSTREAM,0);
    if(d>0)
    	printf(("\n Socket created");
    else{
    	puts("socket not created");
    	exit(0);
    }
     ser.sin_family=AF_INET;
     ser,sin_port=htons(port);
     ser.sin_addr.s_addr=INADDR_ANY;
     r=sizeof(struct sockadrr);
    
     b=bind(d,(struct sockaddr *)&ser,r);
     if(b<0)
     exit(0);
     
     listen(d,MAXCONNECTIONS);
    
     do{
     q=accept(d,(struct sockaddr *)&cli,&r)
     id=fork()//unix multitasking
     if(id==0)
     printf("The client %S is connected", inet_ntoa(cli.sin_addr);
     sz=sizeof(buf);
     i=read(q,buf,sz);
     buf[i]=0;
     for(i=0;i<buffer_size && buf[i]!='\r'&&buf[i]='\n';i++)
    	{
     	line[i]=buf[i];
    	}
     line[i]=0;
     
     method=strtok(line," ");
     path=strtok(NULL, " ");
     prt=strtok(NULL," ");
    
     if(strcmp(method, "GET")!=0)
     exit(0);
     
     sending(q, path, prt);
     con++;
    }while (con<=MAXCONNECTIONS);
    close(d);
    }
    
    
    void sending(int sd,char *path, char *prt)
    {
     char ch, header[buffer_size + 1];
     int len;
     FILE *fp;
     path++;
     fp=fopen(path, "r");
     sprintf(header, "%s 200 ok\r\nContent_type :text\html\r\n\r\n", prt);
     len=strlen(header);
     write(sd,header, len);
     
     if(fp==0)
     fp=fopen(filenotfound.html,"r");
     do{
     ch=fgetc(fp);
     if(ch!=EOF)
     write(sd,&ch,1);
     }while(ch!=EOF)
     
     fclose(fp);
     shutdown(sd,1);
    
    }
    Please use [code][/code]Tags

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    Code:
    sprintf(header, "%s 200 ok\r\nContent_type :text\html\r\n\r\n", prt);
    Does this mean that I woulkd have to edit this line anytime I need to include other file types?
    How do I get the server to detect the file being requested automatically?
    And thanks for noticing some of those minor errors.
    Hope this is better:
    Code:
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet.h>
    #include<string.h>
    #define port 1234
    #define buffer_size 1024
    #define CLEARSCREEN "\x1B[2J"
    #define MAXCONNECTIONS 3
    void sending (int sd,char *f1, char *prt);
    struct sockaddr cli,ser;
    main(){
    
    int create_socket,t=0,new_socket,sd,binderror,no_of_char,j,connectxns=1,id;
    char buf[buffer_size+1],*method,*path,*protocol;
    char line[buffer_size];
    struct sockaddr_in cli,ser;
    int sw;
    printf(CLEARSCREEN);
    
    create_socket=socket(AF_INET,SOCKSTREAM,0);
    if(create_socket>0)
    	printf(("\n Socket created");
    else{
    	puts("socket not created");
    	exit(0);
    }
     ser.sin_family=AF_INET;
     ser,sin_port=htons(port);
     ser.sin_addr.s_addr=INADDR_ANY;
     
    
     binderror=bind(create_socket,(struct sockaddr *)&ser,sizeof(struct sockadrr));
     if(binderror<0)
     exit(0);
     
     listen(create_socket,MAXCONNECTIONS);
    
     do{
     new_socket=accept(create_socket,(struct sockaddr *)&cli,&sizeof(struct sockadrr))
     id=fork()//unix multitasking
     if(id==0)//CHILD_PROCESS
     printf("The client %S is connected", inet_ntoa(cli.sin_addr));
     sz=sizeof(buf);
     no_of_char=read(new_socket,buf,sz);
     buf[no_of_char]=0;
     for(no_of_char=0;no_of_char<buffer_size && buf[no_of_char]!='\r'&&buf[no_of_char]='\n';no_of_char++)
    	{
     	line[no_of_char]=buf[no_of_char];
    	}
     line[no_of_char]=0;
     
     method=strtok(line," ");
     path=strtok(NULL, " ");
     protocol=strtok(NULL," ");
    
     if(strcmp(method, "GET")!=0)
     exit(0);
     
     sending(new_socket, path, protocol);
     connectxns++;
    }while (connectxns<=MAXCONNECTIONS);
    close(create_socket);
    }
    
    
    void sending(int sd,char *path, char *prt)//sd->socket descriptor
    {
     char ch, header[buffer_size + 1];
     int len;
     FILE *fp;
     path++;
     fp=fopen(path, "r");
     sprintf(header, "%s 200 ok\r\nContent_type :text\html\r\n\r\n", prt);
     len=strlen(header);
     write(sd,header, len);
     
     if(fp==0)
     fp=fopen(filenotfound.html,"r");
     do{
     ch=fgetc(fp);
     if(ch!=EOF)
     write(sd,&ch,1);
     }while(ch!=EOF)
     
     fclose(fp);
     shutdown(sd,1);
    
    }

Popular pages Recent additions subscribe to a feed