G
G
German2018-10-16 22:16:11
C++ / C#
German, 2018-10-16 22:16:11

Does the recv function throw an error on a socket?

I'm trying to send a message from the client to the server and so that the server would display this message:
There is such a server code

//server for Linux

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <clocale>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main()
{
    setlocale(LC_ALL, "Russian");
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in sa; //структура для bind
    sa.sin_family = AF_INET; //дублируем домен
    sa.sin_port = htons(12345); //указываем порт,
    //функция htons приводит к сетевому порядку байт
    sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); //тут мы указываем локальный IP-адрес
    bind(sock, (struct sockaddr *)&sa, sizeof(sa)); //привязали сокет
    listen(sock, SOMAXCONN); //слушаем
    //SOMAXCONN - максимальная длинна очереди
    //теперь мы должны начать принимать соединения
    char buff[32] = "default";
    std::cout << "Начинаем слушать\n";
    while(int s1 = accept(sock, 0, 0)) //принимаем соединения для работы с ними
    {
        std::cout << "Поймали\n";
        int k = recv(sock, &buff, sizeof(buff), MSG_NOSIGNAL);
        std::cout << "k = " << k << "\n";
        if(k == -1)
            std::cout << "Какая-то ошибка...\n";
        if(!k)
            std::cout << "Отсутствии записанных в сокет процессом-поставщиком данных\n";
        if(k > 0) 
            std::cout << "Сообщение: " << buff << "\n";
    }
    printf("Работа завершена!");
    return 0;
}

And such a client
//client for Linux

#include <stdlib.h>
//#include <stdio.h>
#include <iostream>
#include <clocale>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main()
{
    setlocale(LC_ALL, "Russian");
    char buff[32] = "It's my message! YEY!";
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in sa; //структура для bind
    sa.sin_family = AF_INET; //дублируем домен
    sa.sin_port = htons(12345); //указываем порт,
    //функция htons приводит к сетевому порядку байт
    sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); //тут мы указываем локальный IP-адрес
    std::cout << "Коннектимся\n";
    connect(sock, (struct sockaddr *)&sa, sizeof(sa)); //соединяемся
    std::cout << "Отправляем сообщение: " << buff << "\n";
    //write(sock, buff, 32);
    send(sock, &buff, sizeof(buff), MSG_NOSIGNAL);
    std::cout << "shutdown\n";
    shutdown(sock, SHUT_RDWR);
    std::cout << "close\n";
    close(sock);
    return 0;
}

In my case, recv returns -1, which means that an error occurred, what and why?.
And if I wrote something wrong in the comments, please correct it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2018-10-17
@jcmvbkbc

In my case, recv returns -1, which means that an error occurred, what and why?.

In vain you ask us - ask him. errno. . that perror's it.
Although in this case everything is clear:
while(int s1 = accept(sock, 0, 0)) //принимаем соединения для работы с ними
    {
        int k = recv(sock, &buff, sizeof(buff), MSG_NOSIGNAL);

-- the new connection is established on the socket s1and recvis reading from sock.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question