How Can I receive messages from more than one named pipes? - c

I have to use a fifo in my code.
I use sock to accept new client. For each client I create new thread to send and receive message to him.
In the function of the thread I use fifo to send and receive messages also to another process and here is my code:
int s_to_c=open(myfifo1,O_WRONLY);
int c_to_s=open(myfifo2,O_RDONLY);
char echoBuffer[RCVBUFSIZE];
int recvMsgSize;
for(;;)
{
bzero(echoBuffer,RCVBUFSIZE);
read(c_to_s, echoBuffer, RCVBUFSIZE);
write(sock, echoBuffer, strlen(echoBuffer));
bzero(echoBuffer,RCVBUFSIZE);
read(sock, echoBuffer, RCVBUFSIZE);
write(s_to_c,echoBuffer,strlen(echoBuffer));
}
close(c_to_s);
close(s_to_c);
close(sock);
And on the other side (The other process) my code:
int s_to_c=open(myfifo1,O_RDONLY);
int c_to_s=open(myfifo2,O_WRONLY);
char echoBuffer[RCVBUFSIZE];
int recvMsgSize;
for(;;)
{
bzero(echoBuffer,RCVBUFSIZE);
fgets(echoBuffer,RCVBUFSIZE,stdin);
echoBuffer[strlen(echoBuffer)-1]='\0';
write(c_to_s, echoBuffer, strlen(echoBuffer));
bzero(echoBuffer,RCVBUFSIZE);
read(s_to_c, echoBuffer, RCVBUFSIZE);
printf("%s\n", echoBuffer);
}
My problem is in this process : s_to_c and c_to_s take always the value(3,4).
So the first client connect correctly sending and receiving his message.
But when the second connect the first client become disable.And the messages of the second client sends and receives to and from the two processes.
Can I have some help please.Should I have to use tags for example??

select() allows you to check the status of a file descriptor (in your case the ones connected to your pipes). When select() returns, it tells you which pipes have data to process. That way, you can monitor many pipes in the server process.
The client process will always use the file descriptors 3 and 4 for the pipes since those are the first free ones after the stdio (0=stdin, 1=stdout, 2=stderr). So that is correct.
If you see the combination of 3 and 4 on your server as well, then you have a bug in the code where you create the pipes, not in the place where you use them.
If you use Linux, there is an easy way to see what a file descriptor is connected to: Look into /proc/PID/fd/ (replace PID with ID of the process that you want to examine) or use lsof -n -p PID (which shows a lot of other things as well like loaded shared libraries).

Related

systemd socket activation, sd_listen_fds return 0 fd

I am writing a sample systemd service to test few things.
unfortunately, the test code is not working.
(i expected it to be simple ..)
$>cat test.socket
[Unit]
Description=TEST-SERVICE
[Socket]
ListenStream=5575
Accept=yes
[Install]
WantedBy=sockets.target
$>cat test#.service
[Unit]
Description=TEST-SERVICE
Requires=test.socket
[Service]
StandardInput=socket
PIDFile=/var/run/test.pid
ExecStart=/usr/bin/server_d
Type=simple
[Install]
WantedBy=default.target
The server code looks like below
int main(int argc, char *argv[]){
char buffer[255]; //Temporary buffer to read and write data
int sockfd, newsockfd ; //File descriptor for Sockets
struct sockaddr_in cli_addr; //Structure to hold Client details
socklen_t cli_len; //Client length
int nfd = sd_listen_fds(0);
if (nfd != 1) {
fprintf(stderr, "No or too many file descriptors received.\n");
fprintf(stderr, "FD Count: %d.\n",nfd);
exit(1);
}
sockfd = SD_LISTEN_FDS_START + 0;
The output comes when I connect to the service as below.
>telnet 15.218.114.55 5575
Trying 15.218.114.55...
Connected to 15.218.114.55.
Escape character is '^]'.
No or too many file descriptors received.
FD Count: 0.
Connection closed by foreign host.
It looks to me like sd_listen_fds(0) is returning 0.
what could be the reason for it here?
Thanks for the help in advance.
There are 2 different ways a socket unit works. With Accept=yes, systemd will do the bind() and listen() calls for each LISTEN...=... socket, then wait with select() on them all. Whenever there is a connection, a socket becomes ready, and systemd will do an accept() call on the socket connection, then pass the new socket fd to the service unit ready to use with read/write etc. Systemd then carries on waiting for new connections, and will start a new service with each of them similarly.
In this case the (template) service does not use sd_listen_fds(). If you have StandardInput=socket, then you can simply read/write fd 0.
The second type of socket unit has Accept=no (the default if not specified). Systemd will do the bind() and listen() calls for each LISTEN...=... socket, then wait with select() on them all. As soon as the first socket is ready, it will start the service once only and pass all the open socket file descriptors to it. This is where the sd_listen_fds() api comes in. It says how many socket file descriptors got passed. They start at 3 (SD_LISTEN_FDS_START).
The service application now has to find out which of the passed sockets was ready. Typically, there is only one LISTEN line in the socket unit, so only one fd is passed, and so it is easy (otherwise simply do a select()).
The service application now has to do an accept() on the ready socket, and can then read/write to the new fd. Normally, it would also continue to wait for more connections on the listened-to sockets it was given, do more accepts, and so on.

C Socket Programming with Polling: How to detect if client hangs up

I am working on a simple C program where there are two source files: server.c and client.c
I connected everything properly with sockets, and I use the poll() function to poll the keyboard for data (as well as a shell connected to a pipe).
I am able to detect when the client uses the ^D and ^C command to know to kill the child process, and exit both client and server terminals.
However, is there a way to detect if the client hangs up (i.e. I explicitly click x on the client terminal tab)? Because right now, the server tab doesn't detect this, and doesn't close. I want to avoid using a timeout
You can continuously probe the client socket with the recv() sys call. It is designed to return 0 when the client disconnects.
while (1) {
//Get request from client, leave if client quits
if (recv(client_socket, client_request, sizeof(client_request), 0) == 0) {
break;
}
}
OR
// This while condition will fail when recv returns 0, because C
while (recv(client_socket, client_request, sizeof(client_request), 0)) {
// insert code here
}
Hope this helps 🤓

Server program is stuck at send

I am building a server client model in C. The clients connects to the server and they start exchanging data. However, the user can end the client at any time in the program, but the server is not notified about it. The server keeps sending that data even after the client is closed.
I was in the impression that send function will return -1 if the server is unable to send the data, but my server program just stuck at send
if((byteSent = send(new_fd, fileContents, strlen(fileContents), 0)) == -1){ //
the program just halts at the above line.
How do I overcome this problem?
//Code
exitT = 0;
//execution_count = 1;
for(i=0;i<execution_count;i++)
{
sleep(time_delay);
//getting the current time on the server machine
time_t t;
time(&t);
char *time=ctime(&t);
printf("The Execution time at server = %s\n",time);
system(exec_command);
/*Open the file, get file size, read the contents and close the file*/
// Open the file
fp = fopen(fileName,"r");
// Get File Size
fseek(fp,0,SEEK_END);
dataLength = ftell(fp);
rewind(fp);
fileContents = (char*)malloc(dataLength+1);
// Read File
fread(fileContents,1,dataLength,fp);
fileContents[dataLength] = '\0';
// Close file
fclose(fp);
printf("sockfd = %d \n",new_fd);
// send file length to client
rc=send(new_fd, &dataLength, sizeof(dataLength), 0) ;
printf("length of client data = %d \n",rc);
printf("sockfd = %d \n",new_fd);
// send time to client
rc=send(new_fd, time, strlen(time), 0) ;
printf("length of client time = %d \n",rc);
usleep(20000);
// Send file contents to Client
while(dataLength>0){
printf("sockfd = %d \n",new_fd);
if((byteSent = send(new_fd, fileContents, strlen(fileContents), 0)) == -1){
printf("bytes sent = %d \n",byteSent);
exitT = 1;
break;
}
dataLength-=byteSent;
}
//Delete the log file
sprintf(deleteCommand,"rm %s",fileName);
system(deleteCommand);
if(exitT == 1)
break;
}
bzero(fileName,sizeof(fileName));
bzero(exec_command,sizeof(exec_command));
bzero(deleteCommand,sizeof(deleteCommand));
//decClientNum();
kill(parent_id,SIGALRM);
close(new_fd); // parent doesn't need this
printf("STATUS = CLOSED\n");
exit(0);
}
Thanks
I assume you are coding for a Linux or Posix system.
When a syscall like send fails it returns -1 and sets the errno; you very probably should use errno to find out why it failed.
You could use strace to find out which syscalls are done by your sever, or some other one. Of course, use also the gdb debugger.
You very probably need to multiplex inputs or outputs. The system calls doing that are poll, select (and related ppoll and pselect). Read e.g. the select_tut(2) man page.
You may want to use (or at least to study the source code of) existing event oriented libraries like libevent, libev etc.. (Both Gtk and Qt frameworks provide also their own, which might be used even outside of GUI applications).
I strongly suggest reading about advanced unix programming and unix network programing (and perhaps also about advanced linux programming).
maybe you're using a tcp protocol and the server is waiting for an ACK. Try using udp if you want your connection to be asynchronous.
From the man page: No indication of failure to deliver is implicit in a send(). Locally detected errors are indicated by a return value of -1.
Proably something like this might help: http://stefan.buettcher.org/cs/conn_closed.html
I think I am pretty late in the party, but I think this answer might help someone.
If space is not available at the sending socket to hold the message to be transmitted, and the socket file descriptor does not have O_NONBLOCK set, send() shall block until space is available.
When send() function gets stuck, there might be a situation like, TCP window size has become 0. It happens when the other end of the connection is not consuming received data.
There might be a scenario like this, the receiving end process is running by GDB and segfault occurred.
The TCP connection remains established.
Data is being send continuously.
The receiver end is not consuming it.
Consequently the receiver TCP window size will keep decreasing and you can send data till it is greater than zero. Once it becomes 0, send() function will get stuck forever.
As the situation mentioned in the question is not a scenario of closed connection. When a process writes something on a closed TCP connection, it receives a signal SIGPIPE. Default handler of SIGPIPE terminates the process. So, in a closed connection scenario if you are not using your own SIGPIPE handler then process should be terminated by default handler whenever something is written on the socket.

Two mains in program

here is the situation: I need to send a data to a neighbor(socket) and then switch to listening mode. Ive got a client part in client.c, which just listens, and server part in server.c - sends data. Using sockets I need to have a main() in both of them. How should I get them "cooperate" together, so both mainss are not going result in error?
Or any other ideas how to solve this issue with sending and listening?
Thanks in advance!
Lucas
You can always create two executables from the sources. Each of them will have its own main.
Or, you can create a single executable and let it fork another process or create another thread. When creating a new thread you'll specify the second "main" to be the thread function.
When fork-ing, you should create two functions main_server and main_client and let the actual main decide which of them to call, just after the fork. See snippet:
int main_server(int argc, int argv){
//TODO: complete
return 0;
}
int main_client(int argc, int argv){
//TODO: complete
return 0;
}
int main(int argc, int argv){
//TODO: parse args and get argv_server, argv_client, argc_server, argc_client
int pid = fork();
if (pid < 0) {
//TODO: handle error and leave
} else if (pid) {
// start client here for example
main_client(argc_client, argv_client);
} else {
main_server(argc_server, argv_server);
wait(pid);
}
return 0;
/* TODO: each of the above calls should be checked for errors */
}
Hope it helps.
Note: it's better to create a separate executable but if you are required to have only one, use the above snippet.
The thing to remember is that these programs will compile into separate binaries that become separate processes. You will start the "server" program (which will run its main) and then the client program (which will run its main). They communicate over the socket you're creating.
Another solution to do this is using "select()" method. This is only for the socket programming in Linux/Unix environment. Using this you can have both sending and listening task done in the same main(). Here's the tutorial for this method.
http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#selectman
What it does is that instead of using fork() it puts all the sockets in a read_set. and then it goes into an infinite do-while() loop. Now this is very useful for socket programming in Linux/Unix. What happens in Linus/Unix each socket is assigned a File Descriptor(FD) in which they write the data and then it is transferred. It treats I/O console as a FD. So it puts the console FD in read_set, then all the other listening ports in read_set and then waits for the data from any of the above FD. So if you have data in console it will select that FD and perform the task you've written. Or will be in the listening mode until you close the program.
Now this is better than the fork() one because while using fork(), if didn't handled properly it could create a fork-bomb which would create processes recursively and will bomb your main memory. So its better to create a single process and have both functionality in it.

C file transfer question

I implemented a client-server program that allows to transfer files b/w them. The server is using select() to check changes of sockets.
Every test is good except this one:
- When server is sending a huge file to client (not yet finished), client hit "Ctrl-C" to kill the client program, then the server is killed too :(
The snippet:
fprintf(stderr,"Reading done, sending ...\n");
if(send(sockClient, sendBuf, chunk_length, 0) < 0)
{
printf("Failed to send through socket %d \n", sockClient);
return -1;
}
fprintf(stderr,"Sending done\n");
When the client is killed, the server terminal displays:
user$./server
Reading done, sending ...
Sending done
Reading done, sending ...
Sending done
Reading done, sending ...
Sending done
Reading done, sending ...
user$
What's wrong with it?
Thanks for your answers!
You probably want to ignore SIGPIPE. Try adding something like this in your server startup:
#include <signal.h>
signal(SIGPIPE, SIG_IGN);
The send() call may be used only when the socket is in a connected state (so that the intended recipient is known). the return is the bytescount sent...
if(send(sockClient, sendBuf, chunk_length, 0) < 0)
so when disconnected, it skipped out...
MSG_NOSIGNAL is not portable and will not be available on Windows.

Resources