• 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.00/day)
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

Moderator
Staff member
Joined
Nov 20, 2013
Messages
5,474 (1.44/day)
Location
Kyiv, Ukraine
System Name WS#1337
Processor Ryzen 7 3800X
Motherboard ASUS X570-PLUS TUF Gaming
Cooling Xigmatek Scylla 240mm AIO
Memory 4x8GB Samsung DDR4 ECC UDIMM
Video Card(s) Inno3D RTX 3070 Ti iChill
Storage ADATA Legend 2TB + ADATA SX8200 Pro 1TB
Display(s) Samsung U24E590D (4K/UHD)
Case ghetto CM Cosmos RC-1000
Audio Device(s) ALC1220
Power Supply SeaSonic SSR-550FX (80+ GOLD)
Mouse Logitech G603
Keyboard Modecom Volcano Blade (Kailh choc LP)
VR HMD Google dreamview headset(aka fancy cardboard)
Software Windows 11, Ubuntu 20.04 LTS
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.00/day)
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

Moderator
Staff member
Joined
Nov 20, 2013
Messages
5,474 (1.44/day)
Location
Kyiv, Ukraine
System Name WS#1337
Processor Ryzen 7 3800X
Motherboard ASUS X570-PLUS TUF Gaming
Cooling Xigmatek Scylla 240mm AIO
Memory 4x8GB Samsung DDR4 ECC UDIMM
Video Card(s) Inno3D RTX 3070 Ti iChill
Storage ADATA Legend 2TB + ADATA SX8200 Pro 1TB
Display(s) Samsung U24E590D (4K/UHD)
Case ghetto CM Cosmos RC-1000
Audio Device(s) ALC1220
Power Supply SeaSonic SSR-550FX (80+ GOLD)
Mouse Logitech G603
Keyboard Modecom Volcano Blade (Kailh choc LP)
VR HMD Google dreamview headset(aka fancy cardboard)
Software Windows 11, Ubuntu 20.04 LTS
And what do you get in STDERR?
 
Top