Thread: Server with browser

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Server with browser

    I have coded a server program which interacts with my client, but it doesnt interact with a browser, what could be the problem?
    here is the
    server.c:
    Code:
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    
    main(int argc, char *argv[])
    {
            int                     sockfd, newsockfd ; /* Socket descriptors */
            int                     clilen;
            struct sockaddr_in      cli_addr, serv_addr;
            int port;
    
            int i, readsocks;
            char buf[100];          /* We will use this buffer for communication */
    
            if(argc != 2)   {
                    printf("\nEnter port number only in the command line argument\n");
                    exit(0);
            }
            if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
                    printf("Cannot create socket\n");
                    exit(0);
            }
            port = atoi(argv[1]);
            printf("\nport is %d\n", port);
            serv_addr.sin_family            = AF_INET;
            serv_addr.sin_addr.s_addr       = INADDR_ANY;
            serv_addr.sin_port              = port;
            if (bind(sockfd, (struct sockaddr *) &serv_addr,
                                            sizeof(serv_addr)) < 0) {
                    printf("Unable to bind local address\n");
                    exit(0);
            }
    
            listen(sockfd, 5);
            clilen = sizeof(cli_addr);
            newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr,
                                            &clilen);
            printf("wow Some client is connected\n\n");
            close(newsockfd);
    }
    I am using "http://127.0.0.1:8080/" in the address bar of the mozilla firefox.
    am running the server program on the same machine on the port 8080.
    actually the server should print:
    Code:
    wow Some client is connected
    but it does nothing..
    Last edited by kapil1089thekin; 02-01-2011 at 07:35 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You've got a connection, but you don't send anything down it - what were you expecting to happen?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    134
    if i get a connection.. the server shoul print the line.. it is not printing that means the server has not get any connection..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    System Calls or Bust
    This suggests you need htons() on the port value.

    Also, get Wireshark · Go deep. so you can see what is really happening "on the wire".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Port 8080 is a secure SSL port. The browser is probably trying to make a secured connection and gives up when it doesn't get the correct acknowledgements. Try it on port 80 which is the general HTTP port.

    OR... use a different port altogether...

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Port 8080 is not "a secure SSL port". 443 is the standard HTTPS port, but any port can actually serve as a secure port if the server is so configured.

    To the OP: printf writes to standard output, not to the connected socket. You need to use send(), not printf().

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    134
    I have changed the code to :

    Code:
            serv_addr.sin_addr.s_addr       = htonl(INADDR_ANY);
            serv_addr.sin_port              = htons(port);
    still not working.
    The client program now also is unable to connect to the server:
    here is the client program.
    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    
    main(int argc, char *argv[])
    {
            int                     sockfd ;
            struct sockaddr_in      serv_addr;
    
            if(argc != 3)   {
                    printf("\nEnter SERVER_IP & PORT_NUMBER only in order as command line argument\n");
                    exit(0);
            }
    
    
            if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
                    printf("Unable to create socket\n");
                    exit(0);
            }
            serv_addr.sin_family            = AF_INET;
            serv_addr.sin_addr.s_addr       = inet_addr(argv[1]);
            serv_addr.sin_port              = atoi(argv[2]);
            if ((connect(sockfd, (struct sockaddr *) &serv_addr,
                                                    sizeof(serv_addr))) < 0) {
                    printf("Unable to connect to server\n");
                    exit(0);
            }
    }
    I think any port number can be used if it can be bind by the server program.

    I want to connect this client program too, and make the server work with browser too.

    I am not using send but write just to check weather there is any connection has been established or not as accept() call blocks the process till there is any connection made.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. INT 15 to return memory size
    By pole1080 in forum C Programming
    Replies: 49
    Last Post: 06-23-2010, 09:28 AM
  2. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 11:21 PM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM