G
G
German2018-10-18 18:07:32
C++ / C#
German, 2018-10-18 18:07:32

Client not connecting to VDS server?

There is an Amazon Dedik, I wrote a server for Linux and a client for Windows (rewritten based on Lunux's), after starting the server and client, the client does not connect.
I also tried to specify instead of the external IP (on the server side), like this:
addr_struct.sin_addr.s_addr = htonl(INADDR_ANY);
But this does not help, there is no connection.

Server side on Linux Dedicated

#include <cstring>
#include <clocale>
#include <iostream>
#include <netinet/in.h>
#include <arpa/inet.h>

using namespace std;

int main()
{
    setlocale(LC_ALL, "Rus");
    char buff[256] = { "void" };
    int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    sockaddr_in addr_struct;
    addr_struct.sin_family = AF_INET;
    addr_struct.sin_addr.s_addr = htonl(inet_addr("Внешний IP дедика"));
    addr_struct.sin_port = htons(5257);
    bind(sock, (struct sockaddr*)&addr_struct, sizeof(addr_struct));
    listen(sock, SOMAXCONN);
    cout << "Ожидаем клиент-собеседника\n";
    int sock1 = accept(sock, 0, 0);
    cout << "Соединение установленно\n";
    while(1)
    {
        cout << "Ожидаем сообщение...\n";
        recv(sock1, &buff, sizeof(buff), MSG_NOSIGNAL);

        cout << "Входящее сообщение: " << buff << "\n";
        cout << "Ввод сообщения: ";
        cin >> buff;
        send(sock1, buff, sizeof(buff), MSG_NOSIGNAL);
    }
    return 0;
}


Client side on Windows
//client for Linux

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <clocale>
#include <sys/types.h>
#include <Windows.h>
#include <tcpestats.h>

#pragma comment(lib, "ws2_32.lib")

using namespace std;

int main()
{
  setlocale(LC_ALL, "Rus");
  char buff[256] = { "void" };
  int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  sockaddr_in addr_struct;
  addr_struct.sin_family = AF_INET;
  //addr_struct.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  addr_struct.sin_addr.s_addr = htonl(inet_addr("Внешний IP дедика"));
  addr_struct.sin_port = htons(5257);
  cout << "Подключаемся к сервер-собеседнику\n";
  if (connect(sock, (struct sockaddr *)&addr_struct, sizeof(addr_struct)) == -1)
  {
    cout << "Произошла ошибка при подключении к серверу-собеседнику\n";
    return 0;
  }
  while (strcmp(buff, "end"))
  {
    cout << "Ввод сообщения: ";
    cin >> buff;
    send(sock, buff, sizeof(buff), MSG_DONTROUTE);
    cout << "Ожидаем сообщение...\n";
    recv(sock, buff, sizeof(buff), MSG_DONTROUTE);
    cout << "Входящее сообщение: " << buff << "\n";
  }
  return 0;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question