• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Socket Program Challenging issue, please help

ragursr

New Member
Joined
Feb 3, 2018
Messages
3 (0.16/day)
Likes
0
#1
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.
 
Joined
Nov 20, 2013
Messages
2,642 (1.70/day)
Likes
2,724
Location
Kiev, Ukraine
System Name Evil Midget
Processor i3-6100
Motherboard MSI B150I PRO AC
Cooling Noctua NH-L9i
Memory 2x8GB Kinkston HyperX DDR4-2133 CL14
Video Card(s) ASUS GTX950 MINI
Storage Samsung XP941, Sandisk X400 512GB
Display(s) Samsung U24E590D (4K/UHD)
Case LianLi Q11B
Audio Device(s) Int.
Power Supply SeaSonic 450W 80+ GOLD
Mouse Logitech G5
Keyboard Zalman K500 modded
Software Windows 10
Benchmark Scores Can fit into a backpack =)
#2
It's kinda hard to guess what's wrong without source code.

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.

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

New Member
Joined
Feb 3, 2018
Messages
3 (0.16/day)
Likes
0
#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
 
Joined
Nov 20, 2013
Messages
2,642 (1.70/day)
Likes
2,724
Location
Kiev, Ukraine
System Name Evil Midget
Processor i3-6100
Motherboard MSI B150I PRO AC
Cooling Noctua NH-L9i
Memory 2x8GB Kinkston HyperX DDR4-2133 CL14
Video Card(s) ASUS GTX950 MINI
Storage Samsung XP941, Sandisk X400 512GB
Display(s) Samsung U24E590D (4K/UHD)
Case LianLi Q11B
Audio Device(s) Int.
Power Supply SeaSonic 450W 80+ GOLD
Mouse Logitech G5
Keyboard Zalman K500 modded
Software Windows 10
Benchmark Scores Can fit into a backpack =)
#4
And what do you get in STDERR?