#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Angry Creating Proxy server program on C running and compile in Linux


    Hi Everyone,

    Just want to ask help. I am now doing my school project building proxy server program that will handle HTTP request, making remote servers, and returning data to client, establishing socket connection for listening connection.

    Once it sees a valid HTTP request it will parse the request which need host, port and requested path.

    I made a program already but it seems while running the program, I can't see anything from the browser.

    Any help please,

    Best regards,
    Dud
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    We don't know much about your program or the various systems on which you're testing.

    How about inserting a lot of output statements to a log file for diagnostics?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0
    Can I post my program here?

    Best regards,

    Dudz
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Fine by me. Please enclose it in [code ] [/code] tags (with out the space between e and ] )
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0
    [code ]#include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <unistd.h>

    #define MAX_MSG 100
    #define END_LINE 0x0A

    #define SUCCESS 0
    #define ERROR 1

    #define BUFFER_SIZE 10000

    #define MAX_STR_LEN 512
    #define BUFFER_SIZE 10000


    parse_URL(char *url, char *hostname, int *port, char *identifier)
    {
    char protocol[MAX_STR_LEN], scratch[MAX_STR_LEN], *ptr=0, *nptr=0;

    strcpy(scratch, url);
    ptr = (char *)strchr(scratch, ':');
    if (!ptr)
    {
    fprintf(stderr, "Wrong url: no protocol specified\n");
    exit(ERROR);
    }
    strcpy(ptr, "\0");
    strcpy(protocol, scratch);
    if (strcmp(protocol, "http"))
    {
    fprintf(stderr, "Wrong protocol: %s\n", protocol);
    exit(ERROR);
    }

    strcpy(scratch, url);
    ptr = (char *)strstr(scratch, "//");
    if (!ptr)
    {
    fprintf(stderr, "Wrong url: no server specified\n");
    exit(ERROR);
    }
    ptr += 2;

    strcpy(hostname, ptr);
    nptr = (char *)strchr(ptr, ':');
    if (!nptr)
    {
    *port = 80;
    nptr = (char *)strchr(hostname, '/');
    }
    else
    {
    sscanf(nptr, ":%d", port);
    nptr = (char *)strchr(hostname, ':');
    }

    if (nptr)
    *nptr = '\0';

    nptr = (char *)strchr(ptr, '/');

    if (!nptr)
    {
    fprintf(stderr, "Wrong url: no file specified\n");
    exit(ERROR);
    }

    strcpy(identifier, nptr);

    }

    //parse_TIME(char *time, int *day, int *hour, int *min)
    //{
    // char scratch[MAX_STR_LEN], *ptr=0, *oldptr = 0;
    //char TEMP[MAX_STR_LEN];

    //strcpy(scratch, time);
    //ptr = (char *)strchr(scratch, ':');
    //if (!ptr)
    //{
    // printf("Wrong time format, code 1\n");
    // exit(ERROR);
    //}
    //strcpy(ptr, "\0");
    //strcpy(TEMP, scratch);
    //*day = atoi(TEMP);

    //oldptr = ptr;
    //ptr = (char *)strchr(ptr+1, ':');
    //if (!ptr)
    //{
    // printf("Wrong time format, code 2\n");
    // exit(ERROR);
    //}
    //strcpy(ptr, "\0");
    //strcpy(TEMP, oldptr+1);
    //*hour = atoi(TEMP);

    // strcpy(TEMP, ptr+1);
    // *min = atoi(TEMP);
    //}




    main( int argc, char *argv[ ])
    {


    char line[MAX_MSG];
    char response[] = "HTTP/1.0 501 Not Implemented \r\n\r\n";
    int len, bytes_sent;

    // open a socket, bind the server address (use port number 9798) to it,
    int sd,newSd, cliLen;

    struct sockaddr_in cliAddr, servAddr;
    char msg[MAX_MSG];
    char buffer[BUFFER_SIZE];

    if(2!=argc){

    fprintf(stderr, "Usage: %s <port>\n", argv[0]);

    exit(1);

    }


    int port = atoi(argv[1]);



    printf("Welcome to Web Proxy Server! \nUsage: server %d\n\n", port);
    if (argc == 2)
    port = atoi(argv[1]);


    // create socket
    sd = socket(AF_INET, SOCK_STREAM, 0);
    if(sd<0) {
    perror("cannot open socket ");
    return ERROR;
    }

    // bind server port
    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servAddr.sin_port = htons(port);

    if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0)
    {
    perror("cannot bind port ");
    return ERROR;
    }



    // listen to incoming connection requests.
    listen(sd,5);

    // If there is a connection request, then the server
    // accepts the connection, reads the request from socket, displays the request
    // on standard output and sends back the default response.

    // server infinite loop
    while(1) {
    printf(" Waiting for data on port %u\n",port);
    cliLen = sizeof(cliAddr);

    newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
    if(newSd<0) {
    perror("cannot accept connection ");
    return ERROR;
    }

    memset(buffer,0x0,BUFFER_SIZE); // init line

    // receive request
    read(newSd, buffer,BUFFER_SIZE);
    printf(" Received request from %s:\n\n%s\n", inet_ntoa(cliAddr.sin_addr),buffer);



    // send default response
    printf(" Sending default response...\n");
    len = strlen(response);
    bytes_sent = send(newSd, response,len,0);
    // bytes_sent = write(newSd, response, len);
    if (len != bytes_sent )
    {
    printf("error during send response, total: %d, sent:%d\n", len, bytes_sent);
    exit(ERROR);
    }
    close(newSd);
    printf("\n-- End of a session --\n\n");

    } // while (1)
    close(sd);
    }
    [code/]
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    please edit your code to remove the space character between

    code
    and
    ]

    code]


    like that...
    thanks.


    oh, but I'm not likely to know the answer to your questions.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo