# Socket Program Challenging issue, please help



## ragursr (Feb 3, 2018)

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.


----------



## silentbogo (Feb 3, 2018)

It's kinda hard to guess what's wrong without source code.



ragursr said:


> 1. After sending first message Server socket program is exiting.


My crystal ball vaguely shows that you only execute the server-side subroutine once, hence it exits immediately after receiving the first message. 



ragursr said:


> 2. how to keep waiting the server socket program and to send frequent changing results from child program to socket.


Throw the message sending/receiving subroutine inside a _while_ loop.
Here's a simple tutorial on sockets, which may help you a bit. Scroll down to "Live server" part for continuous listening on the server side.
http://www.binarytides.com/socket-programming-c-linux-tutorial/


----------



## ragursr (Feb 3, 2018)

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


----------



## silentbogo (Feb 3, 2018)

And what do you get in STDERR?


----------



## ragursr (Feb 7, 2018)

am not getting any error application hanged


----------

