Thread: Socket Program Challenging issue, please help

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    3

    Talking Socket Program Challenging issue, please help

    Dear Experts


    Hope all doing Good


    facing a challenging problem in my C socket Program, Please help in this regard.




    Am writing a server Socket Program, it is working fine but my requirement is below.




    Am having 2 cprograms


    1. Child c program which will have a function to convert upper case as function as below.


    2. a server socket program to send the result of child program to socket specified port.


    if it is a chat kind of program , i can able to do, but it need send message to socket from other function and value.


    Problems,


    1. After sending first message Server socket program is exiting.
    2. how to keep waiting the server socket program and to send frequent changing results from child program to socket.




    Please help me in this regard, will help me a lot.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    Well it sounds like you have a program that works to some extent, so you should post it.
    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
    Feb 2018
    Posts
    3
    the scenerio is need to monitor thr Database activities through out by continusoly in specific interval.
    for this am using C application which will write the DB status information into an file.


    it will pass the processed_data by a function to a file in defined path.
    now my requirement is to bypass writing into a file and i want to stream through socket with port.
    now i have created below socket function and calling the function inside the program which writes into the file.


    server socket function.


    Code:
    int sendsoc(char *buffer)
    {
    
    
    int server_fd, new_socket;
        struct sockaddr_in address;
        int opt = 1;
        int addrlen = sizeof(address);
          
        // Creating socket file descriptor
        if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
        {
            perror("socket failed");
            /*exit(EXIT_FAILURE);*/
        }
        printf("socket created\n");
          
        // Forcefully attaching socket to the port 8080
        if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
                                                      &opt, sizeof(opt)))
        {
            perror("setsockopt");
            /*exit(EXIT_FAILURE);*/
        }
        printf("socket assigned\n");
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = INADDR_ANY;
        address.sin_port = htons( PORT );
          
        // Forcefully attaching socket to the port 8080
        if (bind(server_fd, (struct sockaddr *)&address, 
                                     sizeof(address))<0)
        {
            perror("bind failed");
            /*exit(EXIT_FAILURE);*/
        }
        
        printf("socket binded\n");
        if (listen(server_fd, 3) < 0)
        {
            perror("listen");
            /*exit(EXIT_FAILURE);*/
        }
        printf("socket listening\n");
        if ((new_socket = accept(server_fd, (struct sockaddr *)&address, 
                           (socklen_t*)&addrlen))<0)
        {
            perror("accept");
            /*exit(EXIT_FAILURE);*/
        }
        printf("socket accepted\n");
        write(new_socket , buffer , strlen(buffer) );
        printf("message send\n");
    
    
    
    return 0;
    /*end*/
    
    
    }



    and calling below function to write into socket.


    sendsoc(processed_data);




    issue is now it is sending only one message and this server is also closing and clien also closing
    and my application hanged and not able to proceed further is there any alternative method
    to send the message through socket continuously

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    Everything down to accept should be done once.

    Then in pseudo code
    Code:
    while ( accept ) {
      getData(buffer);
      write(new_socket , buffer , strlen(buffer) );
      close(new_socket);
    }
    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
    Feb 2018
    Posts
    3
    Dear Salem,


    Thanks a lot


    Please confirm below code is fine as per your suggestion.




    Code:
    
    int sendsoc(char *buffer)
    {
     
    int server_fd, new_socket;
        struct sockaddr_in address;
        int opt = 1;
        int addrlen = sizeof(address);
           
        // Creating socket file descriptor
        if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
        {
            perror("socket failed");
            /*exit(EXIT_FAILURE);*/
        }
        printf("socket created\n");
           
        // Forcefully attaching socket to the port 8080
        if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
                                                      &opt, sizeof(opt)))
        {
            perror("setsockopt");
            /*exit(EXIT_FAILURE);*/
        }
        printf("socket assigned\n");
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = INADDR_ANY;
        address.sin_port = htons( PORT );
           
        // Forcefully attaching socket to the port 8080
        if (bind(server_fd, (struct sockaddr *)&address, 
                                     sizeof(address))<0)
        {
            perror("bind failed");
            /*exit(EXIT_FAILURE);*/
        }
         
        printf("socket binded\n");
        if (listen(server_fd, 3) < 0)
        {
            perror("listen");
            /*exit(EXIT_FAILURE);*/
        }
        printf("socket listening\n");
        //if ((new_socket = accept(server_fd, (struct sockaddr *)&address, 
      //                     (socklen_t*)&addrlen))<0)
       // {
      //      perror("accept");
            /*exit(EXIT_FAILURE);*/
       // }
      //  printf("socket accepted\n");
      //  write(new_socket , buffer , strlen(buffer) );
       // printf("message send\n");
    
    
    while ( accept ) {
      getData(buffer);
      write(new_socket , buffer , strlen(buffer) );
      close(new_socket);
    }
     
    return 0;
    /*end*/
      
    }

    Please confirm the above code

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    No, it's wrong.

    You just dumped in my pseudo code.

    That means you have to THINK about what you're doing, and write some actual code.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with multicast when socket isn't bound to INADDR_ANY
    By Clairvoyant1332 in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-07-2011, 09:11 PM
  2. socket programming issue
    By santarus in forum C Programming
    Replies: 11
    Last Post: 10-20-2011, 12:14 PM
  3. Issue receiving data from socket (multicast)
    By JessH in forum C++ Programming
    Replies: 11
    Last Post: 08-31-2010, 09:01 AM
  4. most challenging thing to program
    By volk in forum A Brief History of Cprogramming.com
    Replies: 52
    Last Post: 03-28-2003, 03:56 PM

Tags for this Thread