How to create an unique Named Pipe for each Client - c

I have a typical Client-Server architecture where the Server is constantly reading from the pipe to see if any Client has sent something. Usually what is done is that there's also a named pipe so that the Server is able to also send whatever it needs to the Client.
The problem I have is that I need to send a specific message to specific Clients, according to my needs. For example, at the moment, with Client A and Client B, if Client A sends a message through the pipe with "process", I need the Server to send back a reply with "OK" to Client A, but what happens is that a random one gets it (not really sure what happens here, but I assume that once one of the Client reads what's in the pipe, the other one won't be able to read it anymore).
How am I able to create a Named Pipe for each Client, so that the Server can send a specific message to a specific Client? Below is the code that I have:
Client.c
#define FIFO_FILE_1 "../tmp/fifo"
#define FIFO_FILE_2 "../tmp/fifo2"
int main(int argc, char *argv[]){
/*if(mkfifo("fif", 0666) < 0){
perror("mkfifo");
}*/
int client_to_server;
int server_to_client;
if((client_to_server = open(FIFO_FILE_1, O_WRONLY))<0){
perror("open fifo");
exit(1);
}
if((server_to_client = open(FIFO_FILE_2, O_RDONLY))<0){
perror("open fifo");
exit(1);
}
if(argc>2){
write(client_to_server, "process\n", 9);
}
int bytes_read = 0;
char buf[1024];
while(1){
while((bytes_read = read(server_to_client, buf, 1024)) > 0){
write(1,"Received",7);
}
}
close(client_to_server);
close(server_to_client);
unlink(FIFO_FILE_1);
unlink(FIFO_FILE_2);
return 0;
}
Server.c
#define FIFO_FILE_1 "../tmp/fifo"
#define FIFO_FILE_2 "../tmp/fifo2"
int main(int argc, char *argv[]){
// Create named pipes
if(mkfifo(FIFO_FILE_1, 0666) < 0){
perror("mkfifo");
}
if(mkfifo(FIFO_FILE_2, 0666) < 0){
perror("mkfifo");
}
int client_to_server;
int server_to_client;
if((client_to_server = open(FIFO_FILE_1, O_RDONLY))<0){
perror("open fifo");
exit(1);
}
if((server_to_client = open(FIFO_FILE_2, O_WRONLY))<0){
perror("open fifo");
exit(1);
}
char buf[1024];
char bufaux[1024];
while(1){
int n = readCommand(client_to_server, buf); //Just reads until \n, shouldn't matter for the problem
if (n<=0)
continue;
strcpy(bufaux,buf);
char * token = first_arg(bufaux); //Returns the token until it hits a space, in this case it will return "process".
if(strcmp(token,"process")==0){
write(server_to_client,"OK", 3);
}
close(client_to_server);
close(server_to_client);
unlink(FIFO_FILE_1);
unlink(FIFO_FILE_2);
return 0;
}
There's aditional functions that aren't shown, that are simply used to parse whatever comes through the Pipe from the Client. Just assume that strcmp(token,"process")==0 will always be true.
How and when would I create a Pipe for each Client? My idea was to send some sort of identifier whenever the Client sends the first message, that would then be used to write a message to that same Client.
Obviously the problem is much more complex, there's data in memory that is what's going to be sent into each specific Client, but as an example, consider I just need to send an "OK". Also, I can't use sockets.

A simple way to solve your problem is to let the clients create the pipes for the return messages.
You can use the PID of the process as part of the name to make it unique per process.
Then in the clients communication with the server, it always include the PID so the server knows which pipe to write the response to.

Related

Named Pipe client and server, message truncated on server?

I got a server that is always running, it creates a log file that receives via named pipe one argument and stores it on the log.txt file.
Clients sent a message via argument to the named pipe.
cliente side i guess its ok, if i cat /tmp/talk its there the full message, but on the server its only storing the first char. why is that?
And a simplier question, is there a better way to implement the server cycle to check the pipe?
client
int main(int argc, char const *argv[]){
char *myfifo = "/tmp/talk"; int fd,n;
fd = open(myfifo,O_WRONLY);
write(fd,argv[1],strlen(argv[1])+1); printf("Sent to server: %s \n",argv[1]);
close(fd);
}
server
int main(int argc, char const *argv[]){
char *myfifo = "/tmp/talk";
char buffer[2024];
//char *log = "log.txt";
int fd,n;
mkfifo(myfifo, 0666);
int log = open("log.txt",O_CREAT|O_APPEND|O_WRONLY, 0666);
fd = open(myfifo,O_RDONLY);
while(1) {
if(n = read(fd,buffer,1024)>0) {
write(log,buffer,n);
write(1,buffer,n);
//printf("Client connected sent: %s",buffer);
}
}
}
n = read(fd,buffer,1024)>0
evaluates like
n = (read(fd,buffer,1024)>0)
so 1 is stored in n (instead of the number of bytes read) if read returns a positive value. Use instead:
(n = read(fd,buffer,1024))>0
as conditional, then it should work as expected.

Using threads to handle multiple read/writes from sockets?

I am trying to create two programs a client and server, where the client opens a socket connection and then writes data to the server who on accepting the connection spawns a new threads and then detaches it, to handle the rest of the read/writes. The problem is that when I make multiple writes then reads from the client the reads aren't getting the correct data, however on the server side it prints that it sent the correct data.
This is what my code looks like to generate new threads, and how I handle those threads.
while(1){
listen(sockfd,5);
// determine the size of a clientAddressInfo struct
clilen = sizeof(clientAddressInfo);
int *newsockfd = malloc(sizeof(int));
// block until a client connects, when it does, create a client socket
*newsockfd = accept(sockfd, (struct sockaddr *) &clientAddressInfo, &clilen);
// if the connection blew up for some reason, complain and exit
if (*newsockfd < 0){
error("ERROR on accept");
}
connection_args *args = malloc(sizeof(connection_args));
args->file_descrp = newsockfd;
pthread_t tid;
pthread_create(&tid,NULL, handle_connect, args);
}
void * handle_connect(void* args){
connection_args* connect_arg = (connection_args*)args;
pthread_detach(pthread_self());
int n = -1;
char buffer[256];
bzero(buffer,256);
//while not close;
while(1){
// try to read from the client socket
n = read(*connect_arg->file_descrp,buffer,255);
printf("input: %s\n", buffer);
// if the read from the client blew up, complain and exit
if (n < 0){
error("ERROR reading from socket");
}
int fd;
if(strcmp("open",buffer) == 0){
fd = open("file.txt",0);
bzero(buffer,256);
sprintf(buffer,"%d",fd);
}else if(strcmp("read",buffer) == 0){
char *read_buffer = malloc(sizeof(char)*256);
bzero(read_buffer,256);
fd = read(get_filedescrp(),read_buffer,30);
bzero(buffer,256);
sprintf(buffer,"%s,%d",read_buffer,fd);
}else if(strcmp("close",buffer) == 0){
break;
}
printf("buffer_send: %s\n",buffer);
// try to write to the client socket
n = write(*connect_arg->file_descrp,buffer,sizeof(buffer));
// if the write to the client below up, complain and exit
if (n < 0){
printf("here!!\n");
error("ERROR writing to socket");
}
bzero(buffer,256);
}
printf("Left thread\n");
return NULL;
}
You cannot implement client server communication over TCP/IP without some sort of protocol. The data written by the sender can be sliced and diced along the way and come in different chunk lengths to the reader side. You must have a way to tell if you have received a full frame before trying to interpret the data.
For example, you can use a very simple line based protocol: read data upto and including the '\n' byte. Reading one byte at a time into a lien buffer is somewhat inefficient but easy to implement.
A socket read call may or may not return the entire data sent by client in a single call.
Each read call returns number of bytes are that are read in that call. So the application should call read in a loop till expected number of bytes are read.

Program displays strange characters on the screen

I'm developing a client-server program, and this is my server_2 file, who will comunicate with the main server.
The program displays on the screen these lines when is running. I think that those lines after the mkfifo are causing this.
i�e|楬���h�.N=��.8��
i�H��h� ��h� �i���Ǭ��ǬjǬ�dǬ�#��i�P#h�Ǭ���h����h�jǬ��ǬP
Structures
typedef struct request req;
struct request
{
char str[256];
int client_pid;
int login; // In case of client, to identify if is logged
int whois; // To identify who is the client and the server
};
typedef struct answer ans;
struct answer
{
char str[256];
int server_pid;
int type;
int login;
int num_users;
};
Main:
#include "header.h"
int main(int argc, char *argv[])
{
int fifo_1, fifo_2;
struct request req;
struct answer ans;
if(argc == 2) // Check if the command was well prompted
{
if(strcasecmp(argv[1], "show") == 0 || strcasecmp(argv[1], "close") == 0)
{
if(fifo_2 = open("FIFO_SERV", O_WRONLY) == -1)
{
perror("[SERVER_2] Error: on the FIFO_SERVER opening!\n");
sleep(2);
exit(EXIT_FAILURE);
}
if(mkfifo("FIFO_SERV_2", 0777) == -1)
{
perror("[SERVER_2] Error: on the FIFO_SERVER_2 creation!\n");
sleep(2);
exit(EXIT_FAILURE);
}
strcpy(req.str, argv[1]); // Copy the argumento to the structure
write(fifo_2, &req, sizeof(req)); // Write a request to the server
strcpy(req.str,""); // Clean the string
fifo_1 = open("FIFO_SERV_2", O_RDONLY);
read(fifo_1, &ans, sizeof(ans)); //Read an answ
}
//close(fifo_1);
unlink("FIFO_SERVER_2");
sleep(2);
exit(EXIT_SUCCESS);
}
The precedence rules of operators = and == make the line
if(fifo_2 = open("FIFO_SERV", O_WRONLY) == -1)
equivalent to
if(fifo_2 = (open("FIFO_SERV", O_WRONLY) == -1))
which essentially assigns 0 to fifo_2 if open succeeds and 1 if open fails. The values 0 and 1 also happens to be the respective values of the standard input and output file descriptor in POSIX standard library implementations (see File descriptor on wikipedia), so later when you execute
write(fifo_2, &req, sizeof(req)); // Write a request to the server
you are either trying to write to standard input (undefined behavior), or to standard output depending on whether the file could be opened rather than to the server. To fix this, you can replace the open expression with:
if((fifo_2 = open("FIFO_SERV", O_WRONLY)) == -1)
Then, you may have to figure out why you can't open the file (since you are presumably writing to standard output, which means open failed).

Executing child process in new terminal

I want to make a simple chat application for unix.
I have created one server which supports multiple clients. When ever a new client connects to the server a new process is created using fork command. Now the problem is all the child processes share the same stdin on the server, cause of this in order to send a message to 2nd clien 1st child prosess has to terminte. In order to resolve this I would like to run each child process in a new terminal.
This can be achieved by writing the code for the child process code in a new file and executing it like xterm -e sh -c .(i have not tried this though).
What i really want is not to have two file just to fireup a new terminal and run rest of the code in it.
int say(int socket)
{
char *s;
fscanf(stdin,"%79s",s);
int result=send(socket,s,strlen(s),0);
return result;
}
int main()
{
int listener_d;
struct sockaddr_in name;
listener_d=socket(PF_INET,SOCK_STREAM,0);
name.sin_family=PF_INET;
name.sin_port=(in_port_t)htons(30000);
name.sin_addr.s_addr=htonl(INADDR_ANY);
int c = bind(listener_d,(struct sockaddr *)&name,sizeof(name)); //Bind
if(c== -1)
{
printf("\nCan't bind to socket\n");
}
if(listen(listener_d,10) == -1) // Listen
{
printf("\nCan't listen\n");
}
puts("\nWait for connection\n");
while(1)
{
struct sockaddr_storage client_addr;
unsigned int address_size = sizeof(client_addr);
int connect_d = accept(listener_d,
(struct sockaddr*)&client_addr,&address_size); //Accept
if(connect_d== -1)
{
printf("\nCan't open secondary socket\n");
}
if(!fork())
{
close(listener_d);
char *msg = "welcome Sweetone\n";
if(send(connect_d,msg,strlen(msg),0))
{
printf("send");
}
int k=0;
while(k<5)
{
say(connect_d);
++k;
}
close(connect_d);
exit(0);
}
close(connect_d);
}
close(listener_d);
return 0;
}
I think the message sending between your client and servers is a bit unusual. It is more common, in this simple "just test how it works" scenario to have the clients sending messages to the server. As an example I could mention a simple echo service, which mirrors everything a client sends, back to the client. Is this design forced by some requirements?
Critique aside, I have two separate changes that could make your current design work. They both involve changing the reading of input in the subservers.
Alternative 1:
Instead of reading from stdin, create a named pipe ( see man 3 mkfifo), fex /tmp/childpipe"pid_of_subserver_here". You could create the pipe in say() and open it for reading. Then use echo (man echo) to write to the pipe echo "My message" > /tmp/childpipe"NNNN". Before exiting the child, remember to remove the pipe with unlink()
Alternative 2:
Create an unnamed pipe between server and each subserver. This makes the code much more messy, but avoids creating named pipes and using echo. Example code is included below. It has insufficient error handling (like most example code) and does not handle disconnecting client properly.
Example usage: 1) start server ./a.out 2) (connect client in external window (e.g. nc localhost 30000) 3) write to client 1 by typing "1Hello client one" 4) (connect second client in third window etc) 4) Write to second client by typing "2Hello second client"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
enum max_childeren{
MAX_CHILDEREN = 50
};
int say(int socket)
{
char buf[513] = {0};
fgets(buf, sizeof(buf), stdin);
int result=send(socket, buf, strlen(buf),0);
return result;
}
int main()
{
int listener_d;
struct sockaddr_in name;
listener_d=socket(PF_INET,SOCK_STREAM,0);
name.sin_family=PF_INET;
name.sin_port=(in_port_t)htons(30000);
name.sin_addr.s_addr=htonl(INADDR_ANY);
int on = 1;
if (setsockopt(listener_d, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0){
perror("setsockopt()");
}
int c = bind(listener_d,(struct sockaddr *)&name,sizeof(name)); //Bind
if(c== -1)
{
printf("\nCan't bind to socket\n");
}
if(listen(listener_d,10) == -1) // Listen
{
printf("\nCan't listen\n");
}
// Edited here
int number_of_childeren = 0;
int pipes[2] = {0};
int child_pipe_write_ends[MAX_CHILDEREN] = {0};
fd_set select_fds;
FD_ZERO(&select_fds);
puts("\nWait for connection\n");
while(1)
{
struct sockaddr_storage client_addr;
unsigned int address_size = sizeof(client_addr);
// Edited here, to multiplex IO
FD_SET(listener_d, &select_fds);
FD_SET(STDIN_FILENO, &select_fds);
int maxfd = listener_d + 1;
int create_new_child = 0;
int connect_d = -1; // moved here
select(maxfd, &select_fds, NULL, NULL, NULL);
if (FD_ISSET(listener_d, &select_fds)){
connect_d = accept(listener_d,
(struct sockaddr*)&client_addr,&address_size); //Accept
if(connect_d== -1)
{
printf("\nCan't open secondary socket\n");
exit(EXIT_FAILURE);
}
create_new_child = 1;
}
char buf[512] ={0};
char *endptr = NULL;
if (FD_ISSET(STDIN_FILENO, &select_fds)){
fgets(buf, sizeof(buf), stdin);
long int child_num = strtol(buf, &endptr, 10);
if (child_num > 0 && child_num <= number_of_childeren) {
write(child_pipe_write_ends[child_num - 1], endptr, strnlen(buf, sizeof(buf)) - (endptr - buf));
}
else {
printf("Skipping invalid input: %s\n", buf);
}
}
if (create_new_child != 1)
continue;
number_of_childeren++; // Edited here
int error = pipe(pipes);
if (error != 0){
//handle errors
perror("pipe():");
exit(EXIT_FAILURE);
}
child_pipe_write_ends[number_of_childeren - 1] = pipes[1];
if(!fork())
{
error = dup2(pipes[0], STDIN_FILENO);
if (error < 0){ // could also test != STDIN_FILENO but thats confusing
//handle errors
perror("dup2");
exit(EXIT_FAILURE);
}
close(pipes[0]);
close(listener_d);
char *msg = "welcome Sweetone\n";
if(send(connect_d,msg,strlen(msg),0))
{
printf("send\n");
}
int k=0;
while(k<5)
{
say(connect_d);
++k;
}
close(connect_d);
exit(0);
}
close(connect_d);
close(pipes[0]);
}
close(listener_d);
return 0;
}
The code needs refactoring into functions. It is too long. I tried to do the least possible amount of changes, so I left the restructuring as an exercise.
fscanf(stdin,"%79s",s);
Why? Is it tcp-chat? You have some socket for each client and if yoy want to "say" something then you must to use client. It's true logick.
The server usually sends a service messages only. It's true logick too.
But if you want new terminal then you can try to use a exec's family from unistd.h .

Using sockets to read from client side

I basically have a server set up and I'm accepting new clients(UNIX) and i'm using select() command to wait for activity on file descriptor but I'm not sure how to write from the clients side and then read it on the servers side
FD_ZERO(&readfds);
FD_SET(server_sockfd, &readfds);
FD_SET(STDIN_FILENO, &readfds);
while (1) {
testfds = readfds;
select(4 + MAXCLIENTS, &testfds, NULL, NULL, NULL);
for (fd = 0; fd < 4 + MAX_CLIENTS; fd++) {
if (FD_ISSET(fd, &testfds)) {
if (fd == server_sockfd) { /* new connection request */
client_sockfd = accept(server_sockfd, NULL, NULL);
if (num_clients < MAXCLIENTS) {
FD_SET(client_sockfd, &readfds);
num_clients++;
} else {
sprintf(message, "XSorry, too many clients. Try again later.\n");
write(client_sockfd, message, strlen(message));
close(client_sockfd);
}
} else if (fd == STDIN_FILENO) {
fgets(kb_message, BUFSIZ + 1, stdin);
if (strcmp(kb_message, "quit\n") == 0) {
sprintf(message, "XServer is shutting down.\n");
for (fd2 = 4; fd2 < 4 + MAX_CLIENTS; fd2++) {
if (FD_ISSET(fd2, &readfds)) {
write(fd2, message, strlen(message));
close(fd2);
}
}
close(server_sockfd);
exit(EXIT_SUCCESS);
} else {
sprintf(message, "M%s", kb_message);
for (fd2 = 4; fd2 < 4 + MAX_CLIENTS; fd2++)
if (FD_ISSET(fd2, &readfds))
write(fd2, message, strlen(message));
}
} else { /* client leaving */
close(fd);
FD_CLR(fd, &readfds);
num_clients--;
}
}
}
}
How would I handle write request from clients and then write back to them, would it be under "else" and how can I check if client is exiting or writing.
Thanks
The most common mistake with select(2) is not re-initializing the descriptor sets, since second, third, and forth arguments are input-output parameters.
Setup an fd_set, for reading before the outer loop, add listening socket descriptor to it, enter the loop, make a copy of the this fd_set and give the copy to select(2). When new connection arrives, add its descriptor to the original fd_set. Same for closed socket (error or EOF on read(2)) - remove the descriptor from the original fd_set.
Hope this helps.
You are correct in thinking you need the read code in your 'else' block. If a file descriptor triggers and it isn't stdin or the 'connect' descriptor, then it is one of your clients attempting to send you data. When one of those file descriptors is triggered in the select, you need to call 'read' on that descriptor to read the data into the buffer. The read command will return you the number of bytes read. If this is a positive number, then it indicates the client has sent you data. If it is zero, then that indicates that the client has ended the TCP connection to your server.
The else block will look something like:
else
{
//Existing connection has data for us to read
if((nBytes = read(fd, buffer, MAXBUFFER)) <= 0)
{
if(nBytes == 0)
{
//Actually, its sending us zero bytes, connection closed
printf("Socket %d hung up\n", fd;
}
else
printf ("Read Error"\n)
}
Also, Follow Nikolai N Fetissov's advice above and make sure that when client's connect you store their fd in a permanent fd_set structure, as the one you are using is being modified by the select call.
Your problem might be that you have a variable called read. It's going to mask one of hte functions you need to use - the read() system call to get data out of the socket. The client puts it in with write(). You might also want to check the return value from select(), which will tell you how many of the file descriptors are ready for reading. Then you can check which ones using FD_ISSET(). It looks like you're doing that part already (except you seem to be checking the wrong variable?)... just call read() on that file descriptor to get out the data the client wrote.
else
{
bzero(buf,100);
n=read(i,buf,100); // Read the client message
buf[n]='\0';
if(n==0) // Check the client is closed or not
{
printf("%d is closed\n",i);
close(i);
FD_CLR(i, &master);
if(i==fdmax)
fdmax--;
}
else
{
n=strlen(buf);
write(1,buf,n);
fflush(stdout);
write(1,"Enter the message\n",18);
bzero(buf,100);
read(0,buf,100);
buf[n]='\0';
write(i,buf,n);
fflush(stdout);
}
}
Notes:
After accept the client, Add the client in the fd set.
Then read the message from the client
If the message is equal to 0, then the client is closed.
If you want to send the message to the client, using the client fd, you can send to the client

Resources