Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. change C code from persistent to none persistent in HTML

change C code from persistent to none persistent in HTML

Scheduled Pinned Locked Moved C / C++ / MFC
htmlsysadminhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Roozbeh Amiressami
    wrote on last edited by
    #1

    I wanna change this C code from persistent to none persistent in HTML please help me i don't know about that

    int main(int argc, char *argv[])
    {
    struct sockaddr_in server_addr, client_addr;
    socklen_t sin_len = sizeof(client_addr);
    int fd_server, fd_client;
    char buf[2048];
    int fdimg;
    //int on = 1;

            fd\_server = socket(AF\_INET, SOCK\_STREAM, 0);
            if(fd\_server < 0)
            {
                perror("socket");
                exit(1);
            }
            
            //setsockopt(fd\_server, SOL\_SOCKET, SO\_REUSEADDR, &on, sizeof(int));
            
            server\_addr.sin\_family = AF\_INET;
            server\_addr.sin\_addr.s\_addr = INADDR\_ANY;
            server\_addr.sin\_port = htons(8080);
            
            if(bind(fd\_server, (struct sockaddr \*) &server\_addr, sizeof(server\_addr)) == -1)
            {
                perror("bind");
                close(fd\_server);
                exit(1);
            }
            
            if(listen(fd\_server,10) == -1)
            {
                perror("listen");
                close(fd\_server);
                exit(1);
            }
            
            while(1)
            {
                fd\_client = accept(fd\_server, (struct sockaddr \*) &client\_addr, &sin\_len);
                if(fd\_client == -1)
                {
                    perror("Connection Failed! Can't Conneect to Client .... \\n");
                    continue;
                }
                printf("Accepted the Client Connection ..... \\n");
                
                if(!fork())
                {
                    /\* Child Process \*/
                    close(fd\_server);
                    memset(buf, 0, 2047);
                    read(fd\_client, buf, 2047);
                    
                    printf("%s\\n", buf);
                    
                    if(!strncmp(buf, "GET /testicon.ico", 16))
                    {
                        fdimg = open("testicon.ico", O\_RDONLY);
                        sendfile(fd\_client, fdimg, NULL, 200000);
                        close(fdimg);
                    }
                    else if(!strncmp(buf, "GET /testpic.jpg", 16))
                    {
                        fdimg = open("testpic.jpg", O\_RDONLY);
                        sendfile(fd\_client, fdimg, NULL, 60000);
                        close(fdimg);
    
    L L 2 Replies Last reply
    0
    • R Roozbeh Amiressami

      I wanna change this C code from persistent to none persistent in HTML please help me i don't know about that

      int main(int argc, char *argv[])
      {
      struct sockaddr_in server_addr, client_addr;
      socklen_t sin_len = sizeof(client_addr);
      int fd_server, fd_client;
      char buf[2048];
      int fdimg;
      //int on = 1;

              fd\_server = socket(AF\_INET, SOCK\_STREAM, 0);
              if(fd\_server < 0)
              {
                  perror("socket");
                  exit(1);
              }
              
              //setsockopt(fd\_server, SOL\_SOCKET, SO\_REUSEADDR, &on, sizeof(int));
              
              server\_addr.sin\_family = AF\_INET;
              server\_addr.sin\_addr.s\_addr = INADDR\_ANY;
              server\_addr.sin\_port = htons(8080);
              
              if(bind(fd\_server, (struct sockaddr \*) &server\_addr, sizeof(server\_addr)) == -1)
              {
                  perror("bind");
                  close(fd\_server);
                  exit(1);
              }
              
              if(listen(fd\_server,10) == -1)
              {
                  perror("listen");
                  close(fd\_server);
                  exit(1);
              }
              
              while(1)
              {
                  fd\_client = accept(fd\_server, (struct sockaddr \*) &client\_addr, &sin\_len);
                  if(fd\_client == -1)
                  {
                      perror("Connection Failed! Can't Conneect to Client .... \\n");
                      continue;
                  }
                  printf("Accepted the Client Connection ..... \\n");
                  
                  if(!fork())
                  {
                      /\* Child Process \*/
                      close(fd\_server);
                      memset(buf, 0, 2047);
                      read(fd\_client, buf, 2047);
                      
                      printf("%s\\n", buf);
                      
                      if(!strncmp(buf, "GET /testicon.ico", 16))
                      {
                          fdimg = open("testicon.ico", O\_RDONLY);
                          sendfile(fd\_client, fdimg, NULL, 200000);
                          close(fdimg);
                      }
                      else if(!strncmp(buf, "GET /testpic.jpg", 16))
                      {
                          fdimg = open("testpic.jpg", O\_RDONLY);
                          sendfile(fd\_client, fdimg, NULL, 60000);
                          close(fdimg);
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What exactly do you mean by "change this C code from persistent to none persistent in HTML ". Your code looks to be a simple HTTP message handler.

      1 Reply Last reply
      0
      • R Roozbeh Amiressami

        I wanna change this C code from persistent to none persistent in HTML please help me i don't know about that

        int main(int argc, char *argv[])
        {
        struct sockaddr_in server_addr, client_addr;
        socklen_t sin_len = sizeof(client_addr);
        int fd_server, fd_client;
        char buf[2048];
        int fdimg;
        //int on = 1;

                fd\_server = socket(AF\_INET, SOCK\_STREAM, 0);
                if(fd\_server < 0)
                {
                    perror("socket");
                    exit(1);
                }
                
                //setsockopt(fd\_server, SOL\_SOCKET, SO\_REUSEADDR, &on, sizeof(int));
                
                server\_addr.sin\_family = AF\_INET;
                server\_addr.sin\_addr.s\_addr = INADDR\_ANY;
                server\_addr.sin\_port = htons(8080);
                
                if(bind(fd\_server, (struct sockaddr \*) &server\_addr, sizeof(server\_addr)) == -1)
                {
                    perror("bind");
                    close(fd\_server);
                    exit(1);
                }
                
                if(listen(fd\_server,10) == -1)
                {
                    perror("listen");
                    close(fd\_server);
                    exit(1);
                }
                
                while(1)
                {
                    fd\_client = accept(fd\_server, (struct sockaddr \*) &client\_addr, &sin\_len);
                    if(fd\_client == -1)
                    {
                        perror("Connection Failed! Can't Conneect to Client .... \\n");
                        continue;
                    }
                    printf("Accepted the Client Connection ..... \\n");
                    
                    if(!fork())
                    {
                        /\* Child Process \*/
                        close(fd\_server);
                        memset(buf, 0, 2047);
                        read(fd\_client, buf, 2047);
                        
                        printf("%s\\n", buf);
                        
                        if(!strncmp(buf, "GET /testicon.ico", 16))
                        {
                            fdimg = open("testicon.ico", O\_RDONLY);
                            sendfile(fd\_client, fdimg, NULL, 200000);
                            close(fdimg);
                        }
                        else if(!strncmp(buf, "GET /testpic.jpg", 16))
                        {
                            fdimg = open("testpic.jpg", O\_RDONLY);
                            sendfile(fd\_client, fdimg, NULL, 60000);
                            close(fdimg);
        
        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        Your server code doesn't even appear to be sending a header at the moment, so client will assume you are http 1.0 server and default of close connection. Look at the rosseta code for a Web server .. goto the C section .. you should recognize the code Hello world/Web server - Rosetta Code[^] The moment it connects to the client it sends a header to the client

        write(client_fd, response, sizeof(response) - 1); /*-1:'\0'*/

        The header is at top of code and looks like this

        char response[] = "HTTP/1.1 200 OK\r\n"
        "Content-Type: text/html; charset=UTF-8\r\n\r\n"
        "Bye-bye baby bye-bye"
        "body { background-color: #111 }"
        "h1 { font-size:4cm; text-align: center; color: black;"
        " text-shadow: 0 0 2mm red}"
        "

        Goodbye, world!

        \r\n";

        So they are sending a header telling client the server is HTML 1.1 and will have persistent connections. The alternative is to send a 1.0 header with the keep-alive tag Then you simply don't close the connection until it times out. It's designed as a start point you need to flesh out the rest from there.

        In vino veritas

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups