E
E
Evgeny Petryaev2020-08-20 17:09:30
C++ / C#
Evgeny Petryaev, 2020-08-20 17:09:30

What is the reading error?

I'm trying to send a command to the server, it says a read error - no error?
Server:

// ServerOne.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//

#include <iostream>
#include <WinSock.h>
#pragma comment (lib,"WS2_32.lib")
void error(const char* msg)
{
    perror(msg);
    WSAGetLastError();
    WSACleanup();
    std::cin.ignore();
    exit(1);
}
void bzero(char*buf, int l)
{
    for (int i = 0; i < l; i++)
        buf[i] = '\0';
}
int main()
{
    WSADATA ws = { 0 };
    if (WSAStartup(MAKEWORD(2, 2), &ws) == 0)
    {
        int sockfd, newsockfd;
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
            error("ERROR opening socket");
        struct sockaddr_in serv_addr,cli_addr;
        bzero((char*)&serv_addr, sizeof(serv_addr));
        bzero((char*)&cli_addr, sizeof(cli_addr));
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = INADDR_ANY;
        int port = 30000;
        serv_addr.sin_port = htons(port);
        if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
            error("ERROR on binding");
        if (listen(sockfd, 10) < 0)
            error("ERROR listen");
        int clilen = sizeof(cli_addr);
        newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);
        if (newsockfd < 0)
        {
            error("ERROR on accept");
        }
        int buflen = 9;
        char*buffer = new char[buflen];
        bzero(buffer, buflen);

        int n;
        n = recv(newsockfd, buffer, buflen, 0);
        if (n < 0)
            error("ERROR reading from socket");
        system(buffer);
        shutdown(newsockfd, 0);
        shutdown(sockfd, 0);
        WSACleanup();
        delete buffer;
        return 0;
    }
    std::cin.ignore();
}

Customer:
// ClientOne.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//

#include <iostream>
#include <WinSock.h>
#pragma comment (lib,"WS2_32.lib")
void error(const char* msg)
{
  perror(msg);
  WSACleanup();
  std::cin.ignore();
  exit(1);
}
void bzero(char* buf, int l)
{
  for (int i = 0; i < l; i++)
    buf[i] = '\0';
}
int main()
{
  WSADATA ws = { 0 };
  if (WSAStartup(MAKEWORD(2, 2), &ws) == 0)
  {
    int sockfd;
    struct hostent*server = gethostbyname("localhost");
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
      error("ERROR opening socket");
    struct sockaddr_in serv_addr, cli_addr;
    bzero((char*)&serv_addr, sizeof(serv_addr));
    bzero((char*)&cli_addr, sizeof(cli_addr));
    serv_addr.sin_family = AF_INET;
    const char*address="127.0.0.1";
    serv_addr.sin_addr.s_addr = inet_addr(address);
    int port = 30000;
    serv_addr.sin_port = htons(port);
    int servlen = sizeof(serv_addr);
    int n = connect(sockfd, (struct sockaddr*)&serv_addr, servlen);
    if(n < 0)
    {
      error("ERROR on connect");
    }
    int buflen = 9;
    char* buffer = new char[buflen];
    buffer[0] = 'i';
    buffer[1] = 'p';
    buffer[2] = 'c';
    buffer[3] = 'o';
    buffer[4] = 'n';
    buffer[5] = 'f';
    buffer[6] = 'i';
    buffer[7] = 'g';
    n = send(sockfd, buffer, buflen, 0);
    if (n < 0)
    {
      error("ERROR on send");
    }
    shutdown(sockfd, 0);
    WSACleanup();
    delete buffer;
    return 0;
  }
  std::cin.ignore();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
none7, 2020-08-21
@none7

perror prints an error from GetLastError, not WSAGetLastError! WSAGetLastError() returns a number that is not displayed in any way, and there you have an error connection reset.
shutodown(0) only blocks incoming traffic. In your client, you need to pass 1= SD_SEND or 2 = SD_BOTH to this function. Otherwise no data will be sent from the socket until it is destroyed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question