O
O
Oleg Komenda2018-01-19 19:46:08
C++ / C#
Oleg Komenda, 2018-01-19 19:46:08

How to change the encoding, what's wrong?

Hello, I made a small program for chatting.
Here is the program server code:

#pragma comment(lib, "Ws2_32.lib")
#include<WinSock2.h>
#include<iostream>
#include<Ws2tcpip.h>
#include<ctime>

SOCKET Connect;
SOCKET* Connections;
SOCKET Listen;

int ClientCount = 0;

void SendMessageToClient(int ID) {
  char* buffer = new char[1024];
  for (;; Sleep(75)) {
    memset(buffer, 0, sizeof(buffer));
    if (recv( Connections[ID], buffer, 1024, NULL)) {
      printf(buffer);
      printf("\n");
      for (int i = 0; i <= ClientCount; i++) {
        send(Connections[i], buffer, strlen(buffer), NULL);
      }
    }
  }
  delete buffer;
}

int main() {
  setlocale(LC_ALL, "russian");
  WSAData data;
  WORD version = MAKEWORD(2, 2);
  int res = WSAStartup(version, &data);
  if (res != 0) {
    return 0;
  }

  struct addrinfo hints;
  struct addrinfo * result;

  Connections = (SOCKET*)calloc(64, sizeof(SOCKET));

  ZeroMemory(&hints, sizeof(hints));

  hints.ai_family = AF_INET;
  hints.ai_flags = AI_PASSIVE;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;

  getaddrinfo(NULL, "7770", &hints, &result);

  Listen = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  bind(Listen, result->ai_addr, result->ai_addrlen);
  listen(Listen, SOMAXCONN);

  freeaddrinfo(result);

  printf("Start server....");
  char m_connect[] = "Connect...;;;5";
  for (;; Sleep(75)) {
    if (Connect = accept(Listen, NULL, NULL)) {
      printf("Client connect...\n");
      Connections[ClientCount] = Connect;
      send(Connections[ClientCount], m_connect, strlen(m_connect), NULL);
      ClientCount++;
      CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)SendMessageToClient, (LPVOID)(ClientCount - 1), NULL, NULL);
    }
  }
  return 1;
}

5a62202a11f48898933993.jpeg
one). - Pay attention to the console: what's wrong with the text? How to change the text encoding (code above).
2). - Is it possible to hide the console process and make it invisible? If yes, then throw the code how this can be implemented
Thanks in advance)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2018-01-19
@Mercury13

#include <iostream>
#include <windows.h>
#include <io.h>
#include <fcntl.h>

int main()     // wmain, получается, не обязателен
{
    std::wstring s = L"Тут курят! Błąd, kurwa!";
    // Проверка на юникодность. Если вышла бурда — такой компилятор фтопку!
    MessageBoxW(0, s.c_str(), L"Test", MB_OK);

    // Вот что надо сделать — так что не забывайте, что придётся работать не в UTF-8, а в wchar’ах
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << s << std::endl;

    // Задержка
    std::cin.ignore(1);
    return 0;
}

Tested on MinGW.
With the second question - "is it possible to hide the console process and make it invisible" - the answer is simple. In the project settings, disable the production of a console program. In my Qt Creator command CONFIG += console c++11, I had to remove the word console. It is advisable (but not required) to change from an entry point
to a specific Windows-specific
What needs to be done in the project - read the doc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question