Thread: adress ip server

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    13

    adress ip server

    Hello,

    Please, I have a little problem, I hope you propose a solution. Thank you very much.
    I developed a client task in C and a server task in C. The client needs the server IP address to connect to the server.
    With Freepastry , I don't know in advance which machine will run the client and which machine will run the server so I can know the ip address of the server only at runtime
    My question: how at run time the client will know the ip address of the server?

    Thank you.
    Cordially.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The client needs the server IP address to connect to the server.
    You mean like a web browser connecting to a web server

    > My question: how at run time the client will know the ip address of the server?
    Well for a browser, you type the address (of the server) into the address bar.
    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
    Jun 2012
    Posts
    13
    Hello,

    Thank you very much for the help :
    here is the client code:
    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    
    ///   CLIENT   
    
    
    int main(int argc, char *argv[])
    {
        printf("This is the client program\n");
    
        int sockfd;
        int len, rc ;
        struct sockaddr_in address;
        int result;
        char ch = 'A';
    
       //Create socket for client.
        sockfd = socket(PF_INET, SOCK_STREAM, 0);
        if (sockfd == -1) { 
            perror("Socket create failed.\n") ; 
            return -1 ; 
        } 
        
        //Name the socket as agreed with server.
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = inet_addr("192.168.1.3");
        address.sin_port = htons(1111);
        len = sizeof(address);
    
        result = connect(sockfd, (struct sockaddr *)&address, len);
        if(result == -1)
        {
            perror("Error has occurred");
            exit(-1);
        }
    char lettre;
        while ( ch < 'Y') {
    
            //Read and write via sockfd
                   // printf("donner lettre:");
                   // scanf("%c",&ch);
                    
            rc = write(sockfd, &ch, 1);
            //printf("write rc = &#37;d\n", rc ) ; 
            if (rc == -1) break ;
            read(sockfd, &ch, 1);
            printf("Char from server = %c\n", ch);
            //if (ch == 'A') sleep(5) ;  // pause 5 seconds 
            sleep(10);
            ch++;
        } 
        close(sockfd);
    
        exit(0);
    }
    here is the server code:
    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    ///   SERVER   
    
    int main(int argc, char *argv[])
    {
        //Declaring process variables.
        int server_sockfd, client_sockfd;
        int server_len ; 
        int rc ; 
        unsigned client_len;
        struct sockaddr_in server_address;
        struct sockaddr_in client_address;
    
        //Remove any old socket and create an unnamed socket for the server.
        server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
        server_address.sin_family = AF_INET;
        server_address.sin_addr.s_addr = htons(INADDR_ANY);
        server_address.sin_port = htons(1111) ; 
        server_len = sizeof(server_address);
    
        rc = bind(server_sockfd, (struct sockaddr *) &server_address, server_len);
        printf("RC from bind = %d\n", rc ) ; 
        
        //Create a connection queue and wait for clients
        rc = listen(server_sockfd, 5);
        printf("RC from listen = %d\n", rc ) ; 
    
        client_len = sizeof(client_address);
        client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
        printf("after accept()... client_sockfd = %d\n", client_sockfd) ; 
    
        while(1)
        {
            char ch;
            printf("server waiting\n");
    
            //Accept a connection
            //client_len = sizeof(client_address);
            //client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
            //printf("after accept()... client_sockfd = %d\n", client_sockfd) ; 
            //Read write to client on client_sockfd
    
            rc = read(client_sockfd, &ch, 1);
                    printf("Char from client = %c\n", ch);
            //printf("RC from read = %d\n", rc ) ;         
            if (ch=='X') break ; 
                    sleep(10);
            ch++;
                    
                    
                    //printf("Donner une lettre (serveur):");
                    
                   // scanf("%c",&ch);
            write(client_sockfd, &ch, 1);
        }
    
        printf("server exiting\n");
    
        //close(client_sockfd);
        close(client_sockfd);
        return 0;
    }
    The client needs a server IP address (like 192.168.1.3) to connect to the server, the problem is that I do not know the ip address of the server before execution....

    Thank you so much

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How big is the network you want to search?

    Trying each one of 192.168.1.1 to 192.168.1.254 is easy enough to do.
    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
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    I wonder if it is possible that the client sends a message broadcat "who is the server" to get the ip address of the server ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Sure, you could do that as well.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Please can you guide me to modify my code to do this? Do you know a function that do this?
    I searched on google, I have not found a tutorial explaining


    Thank you so much for help

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Really!?
    Slightly Advanced Techniques

    Then read the rest of the Beej tutorial, it has most of the stuff you're likely to need on a day to day basis.
    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.

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Thank you so much for much for help
    I have used the client code that makes the broadcacast:
    Code:
    //Socket Client
      
    #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>
    #include <stdlib.h>
    #include <string.h>
      
    #define BUFFER_TAILLE 10 
      
    int main (int argc, char *argv[]) {  
    /*******************************************/
    
    char buffer[BUFFER_TAILLE]="ip address";
    int sock;
    int on = 1;
    struct sockaddr_in sin;
    
    if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
      perror("socket");
      exit(1);
    }
    if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
      &on, sizeof(on)) < 0) {
      perror("setsockopt");
      exit(1);
    }
    sin.sin_family = PF_INET;
    sin.sin_addr.s_addr = INADDR_BROADCAST;
    sin.sin_port = htons(1111);
    if (bind(sock, (struct sockaddr *)&sin,
      sizeof(sin)) < 0) {
      perror("bind");
      exit(1);
    }
    
    write(sock,buffer, 10);
    close(sock);
    /*************************************************************/
      return 0;  
    }
    But I do not know how I check the reception of this broadcast, do I need to do a client or a server that is listening on port 1111 ?

    Thank you so much for help
    Best Regards.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why not just use DNS?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Quote Originally Posted by Elysia View Post
    Why not just use DNS?
    Please can you explain more your idea?

    Thanks a lot

  12. #12
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Why does your post have better spelling, meaning and grammar than the title? First impressions are everything.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by chercheur View Post
    Please can you explain more your idea?

    Thanks a lot
    Run some software that reports the ip to a server on your server. Ther are free sevices for this.
    Then, connect to that DNS.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Quote Originally Posted by Elysia View Post
    Run some software that reports the ip to a server on your server.
    Can you name one?
    I thought that is for me to develop the code that makes the broadcast

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NUMA Virtual Adress Space / Physical Adress Space
    By NUMA Orly in forum Windows Programming
    Replies: 0
    Last Post: 03-14-2011, 03:19 AM
  2. Server Client return IP Adress
    By bigmen2007 in forum C Programming
    Replies: 12
    Last Post: 04-28-2010, 06:56 AM
  3. MAC adress
    By crvenkapa in forum Tech Board
    Replies: 1
    Last Post: 02-10-2008, 11:24 AM
  4. Tell adress of object from adress of member
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2007, 05:04 AM
  5. Some help with point adress
    By xxxrugby in forum C Programming
    Replies: 5
    Last Post: 03-25-2005, 06:19 PM