чтение / запись: незаконный поиск — переполнение стека клиент-сервер

Я нашел в клиент-серверном коде stackoverflow (используя именованные каналы) и изменил его, чтобы позволить клиенту отправлять сообщения в цикле while. Хотя я получаю сообщения от сервера, я получаю «Незаконный поиск» при чтении / записи, и я не уверен, является ли это серьезной ошибкой, если что-то здесь действительно плохо?

server.cpp

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

int main()
{
int client_to_server;
char *myfifo = "/tmp/client_to_server_fifo";

int server_to_client;
char *myfifo2 = "/tmp/server_to_client_fifo";

char buf[BUFSIZ];

/* create the FIFO (named pipe) */
mkfifo(myfifo, 0666);
mkfifo(myfifo2, 0666);

/* open, read, and display the message from the FIFO */
client_to_server = open(myfifo, O_RDONLY);
server_to_client = open(myfifo2, O_WRONLY);

printf("Server ON.\n");

while (1)
{
read(client_to_server, buf, BUFSIZ);

if (strcmp("exit",buf)==0)
{
printf("Server OFF.\n");
break;
}

else if (strcmp("",buf)!=0)
{
printf("Received: %s\n", buf);
printf("Sending back...\n");
write(server_to_client,buf,BUFSIZ);
}

/* clean buf from any data */
memset(buf, 0, sizeof(buf));
}

close(client_to_server);
close(server_to_client);

unlink(myfifo);
unlink(myfifo2);
return 0;
}

client.cpp

#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
int client_to_server;
char *myfifo = "/tmp/client_to_server_fifo";

int server_to_client;
char *myfifo2 = "/tmp/server_to_client_fifo";

char str[BUFSIZ];//printf("Input message to serwer: ");
//scanf("%s", str);/* write str to the FIFO */
client_to_server = open(myfifo, O_WRONLY);
server_to_client = open(myfifo2, O_RDONLY);

while(true)
{

printf("> ");
scanf("%s", str);
write(client_to_server, str, sizeof(str));

perror("Write:"); //Very crude error check

read(server_to_client,str,sizeof(str));

perror("Read:"); // Very crude error check

printf("From  server: %s\n",str);
}
close(client_to_server);
close(server_to_client);

/* remove the FIFO */

return 0;
}

Пример вывода:

клиент :

> hello, server!
Write:: Success
Read:: Illegal seek
From  server: hello,
Write:: Illegal seek
Read:: Illegal seek
> From  server: server!
> its client here!
Write:: Illegal seek
Read:: Illegal seek
From  server: its
Write:: Illegal seek
Read:: Illegal seek
> From  server: client
Write:: Illegal seek
Read:: Illegal seek
> From  server: here!
> > hello, server!
// and so on ...

сервер :

Server ON.
Received: hello,
Sending back...
Received: server!
Sending back...
Received: its
Sending back...
Received: client
Sending back...
Received: here!
Sending back...
Received: >
Sending back...
Received: hello,
Sending back...
Received: server!
Sending back...
Received: Write::
Sending back...
Received: Success
Sending back...
Received: Read::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: From
Sending back...
Received: server:
Sending back...
Received: hello,
Sending back...
Received: Write::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: Read::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: >
Sending back...
Received: From
Sending back...
Received: server:
Sending back...
Received: server!
Sending back...
Received: >
Sending back...
Received: its
Sending back...
Received: client
Sending back...
Received: here!
Sending back...
Received: Write::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: Read::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: From
Sending back...
Received: server:
Sending back...
Received: its
Sending back...
Received: Write::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: Read::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: >
Sending back...
Received: From
Sending back...
Received: server:
Sending back...
Received: client
Sending back...
Received: Write::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: Read::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: >
Sending back...
Received: From
Sending back...
Received: server:
Sending back...
Received: here!
Sending back...
Received: >
Sending back...

0

Решение

Не звони perror если бы не было ошибки.

Вам нужно проверить, не был ли предыдущий вызов функции, прежде чем вы сможете вызвать perror или вы получите бессмысленные сообщения об ошибках, когда ошибки на самом деле не произошло. Функции (обычно) не требуется устанавливать errno к чему-то конкретному, если они не подведут.

2

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]