Учимcя программировать на C++

Модератор: t800

Учимcя программировать на C++

Сообщение t800 » 26 авг 2016, 19:21

Решил написать уроки программирования на C++ для своих товарищей.

Чтобы программировать на C++ нужен компилятор g++. В Windows он ставится в пакете MINgv. В Linux он встроен в систему.


Урок 1 Программа Hello, Word!

В терминале создаёте файл командой

Код: Выделить всё
nano hello.cc


В редакторе вставляете следующий код

Код: Выделить всё
// программа  'Hello World!' 

#include <iostream>

int main()
{
  std::cout << "Привет всем, это Даня! \n";
  return 0;

}


Сохраняетесь (ctrl+o). Потом в терминале даете следующую команду:

Код: Выделить всё
g++ hello.cc -o hello


Потом запускаете программу командой

Код: Выделить всё
./hello


Урок 2 программа калькулятор

В терминале создаёте файл командой

Код: Выделить всё
nano kakul.cc


В редакторе вставляете следующий код

Код: Выделить всё

#include <stdio.h>



int main(void)

{

float num1;

float num2;

char op;

printf("Первое число: ");

scanf("%f",&num1);

printf("Второе число: ");

scanf("%f",&num2);

printf("Оператор ( + - * / ): ");

while ((op = getchar()) != EOF)

{

if (op == '+')

{

printf("%6.2f\n",num1 + num2);

break;

}

else if(op == '-')

{

printf("%6.2f\n",num1 - num2);

break;

}

else if(op == '*')

{

printf("%6.2f\n",num1 * num2);

break;

}

else if(op == '/')

{

if(num2 == 0)

{

printf("Ошибка: деление на ноль!\n");

break;

}

else

{

printf("%6.2f\n",num1 / num2);

break;

}

}

}

return 0;

}




Сохраняетесь (ctrl+o). Потом в терминале даете следующую команду:

Код: Выделить всё
g++ kakul.cc -o kakul


Потом запускаете программу командой

Код: Выделить всё
./kakul



Справочник и Форум Green Dragon
T-800 MOD для Heroes of Might and Magic 2

Основатель клуба "Армагеддон"

Основатель клуба "Пещера Гремлинов"

ИзображениеИзображение
Аватара пользователя
t800
Experienced
Experienced
 
Сообщения: 849
Зарегистрирован: 19 окт 2015, 14:52
Откуда: Казахстан, Алматинская область
Благодарил (а): 172 раз.
Поблагодарили: 136 раз.

Re: Учимcя программировать на C++

Сообщение t800 » 27 авг 2016, 14:18

Программирование на C++ (Урок второй)

Делаем русскую игру Тетрис-Черепочки на свободных исходниках.

Для этого понадобится.

1) Компилятор g++ 4.7
2) Cmake 2.8.12
3) Любой текcтовый редактор

Во первых, качаем исходники обычной игры Тетрис (анлийская верси) c github

https://github.com/Teyu/Tetris-SDL-game


2) Разархивируем куда нибудь


3) Создаем папку tetris

Код: Выделить всё
mkdir tetris


Переходим в папку tetris

Код: Выделить всё
cd tetris


И cmake собираем make файл скачаных исходников

Код: Выделить всё
cmake ~/Tetris-SDL-game-master


затем даем команду

Код: Выделить всё
make


собираем игру

И запускаем ее командой

Код: Выделить всё
./Tetris


Игра всем хороша с небольшим исключением. Вс-первых все по английски. А во-вторых какая-скучаная.
Давайте сделаем ее по русский и слегка повеселей.

Делаем по русски.

Для этого открываем в редакторе файл Framework.cpp
(вот этот https://github.com/Teyu/Tetris-SDL-game ... mework.cpp )

Ищем в нем строки

Код: Выделить всё
m_ptext_points = TTF_RenderText_Solid(font, "Points: ", text_color);
   m_ptext_level = TTF_RenderText_Solid(font, "Level: ", text_color);
m_ptext_lines = TTF_RenderText_Solid(font, "Lines: ", text_color);



И изменям их на

Код: Выделить всё
m_ptext_points = TTF_RenderUTF8_Solid(font, "Очки: ", text_color);
m_ptext_level = TTF_RenderUTF8_Solid(font, "Уровень: ", text_color);
m_ptext_lines = TTF_RenderUTF8_Solid(font, "Слои черепов: ", text_color);



Так же меням размер шрифтов строку

Код: Выделить всё
font = TTF_OpenFont("FreeSans.ttf", 24);


меняем на

Код: Выделить всё
font = TTF_OpenFont("FreeSans.ttf", 20);


Сохраняем файл (Ctrl-O)

и собриаем игру командой

Код: Выделить всё
make



Игра стала по русски, но все равно скучаная, поэтому скачиваем архив с черепачками

Вот он http://wiki.kvkozyrev.org/uroki/Skulls.zip

И разархивируем его в папку ~/tetris/data

И игра у нас страла русскрой и веселой!



Домашнее задание к второму уроку.

Вставте в игру фоном какую-нибудь веселую картинку
Справочник и Форум Green Dragon
T-800 MOD для Heroes of Might and Magic 2

Основатель клуба "Армагеддон"

Основатель клуба "Пещера Гремлинов"

ИзображениеИзображение
Аватара пользователя
t800
Experienced
Experienced
 
Сообщения: 849
Зарегистрирован: 19 окт 2015, 14:52
Откуда: Казахстан, Алматинская область
Благодарил (а): 172 раз.
Поблагодарили: 136 раз.

Re: Учимcя программировать на C++

Сообщение t800 » 07 окт 2016, 11:58

Программирование на С++ (урок третий)

На этот уроке мы изучик как запускать выполнять другие программы из нашей программы делается с помощью команды system вот так system ("И тут можно написать любую команду для консоли");

А вот и пример программы которая выполняет команду DIR

Код: Выделить всё
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* system*/

int main ()
{
  printf ("Выполняем команду  DIR \n");
  system ("dir");
  printf ("Команда DiR выполнена \n");
 }



А вот и видео

Справочник и Форум Green Dragon
T-800 MOD для Heroes of Might and Magic 2

Основатель клуба "Армагеддон"

Основатель клуба "Пещера Гремлинов"

ИзображениеИзображение
Аватара пользователя
t800
Experienced
Experienced
 
Сообщения: 849
Зарегистрирован: 19 окт 2015, 14:52
Откуда: Казахстан, Алматинская область
Благодарил (а): 172 раз.
Поблагодарили: 136 раз.

Re: Учимcя программировать на C++

Сообщение t800 » 09 окт 2016, 23:48

Программирование на C++ (Урок 5) Делаем ЧАТ.

Чтобы сделать ЧАТ нам понадобится код сервера


Код: Выделить всё
#include <iostream>
#include <cstdlib>
#include <string>

#include <SDL_net.h>

using namespace std;

const unsigned short PORT        = 1234;            // The port our server will listen for incoming connecions on
const unsigned short BUFFER_SIZE = 512;             // Size of our message buffer
const unsigned short MAX_SOCKETS = 4;               // Max number of sockets
const unsigned short MAX_CLIENTS = MAX_SOCKETS - 1; // Max number of clients in our socket set (-1 because server's listening socket takes the 1st socket in the set)

// Messages to send back to any connecting client to let them know if we can accept the connection or not
const string SERVER_NOT_FULL = "OK";
const string SERVER_FULL     = "FULL";

int main(int argc, char **argv)
{
    IPaddress serverIP;                  // The IP of the server (this will end up being 0.0.0.0 - which means roughly "any IP address")
    TCPsocket serverSocket;              // The server socket that clients will use to connect to us
    TCPsocket clientSocket[MAX_CLIENTS]; // An array of sockets for the clients, we don't include the server socket (it's specified separately in the line above)
    bool      socketIsFree[MAX_CLIENTS]; // An array of flags to keep track of which client sockets are free (so we know whether we can use the socket for a new client connection or not)
 
    char buffer[BUFFER_SIZE];            // Array of characters used to store the messages we receive
    int receivedByteCount = 0;           // A variable to keep track of how many bytes (i.e. characters) we need to read for any given incoming message i.e. the size of the incoming data
 
    int clientCount = 0;                 // Count of how many clients are currently connected to the server
 
    bool shutdownServer = false;         // Flag to control when to shut down the server
 
    // Initialise SDL_net (Note: We don't initialise or use normal SDL at all - only the SDL_net library!)
    if (SDLNet_Init() == -1)
    {
        cout << "Failed to intialise SDL_net: " << SDLNet_GetError() << endl;
        exit(-1); // Quit!
    }
 
    // Create the socket set with enough space to store our desired number of connections (i.e. sockets)
    SDLNet_SocketSet socketSet = SDLNet_AllocSocketSet(MAX_SOCKETS);
    if (socketSet == NULL)
    {
        cout << "Failed to allocate the socket set: " << SDLNet_GetError() << "\n";
        exit(-1); // Quit!
    }
    else
    {
        cout << "Allocated socket set with size:  " << MAX_SOCKETS << ", of which " << MAX_CLIENTS << " are availble for use by clients." <<  endl;
    }
 
    // Initialize all the client sockets (i.e. blank them ready for use!)
    for (int loop = 0; loop < MAX_CLIENTS; loop++)
    {
        clientSocket[loop] = NULL;
        socketIsFree[loop] = true; // Set all our sockets to be free (i.e. available for use for new client connections)
    }
 
    // Try to resolve the provided server hostname. If successful, this places the connection details in the serverIP object and creates a listening port on the provided port number
    // Note: Passing the second parameter as "NULL" means "make a listening port". SDLNet_ResolveHost returns one of two values: -1 if resolving failed, and 0 if resolving was successful
    int hostResolved = SDLNet_ResolveHost(&serverIP, NULL, PORT);
 
    if (hostResolved == -1)
    {
        cout << "Failed to resolve the server host: " << SDLNet_GetError() << endl;
    }
    else // If we resolved the host successfully, output the details
    {
        // Get our IP address in proper dot-quad format by breaking up the 32-bit unsigned host address and splitting it into an array of four 8-bit unsigned numbers...
        Uint8 * dotQuad = (Uint8*)&serverIP.host;
 
        //... and then outputting them cast to integers. Then read the last 16 bits of the serverIP object to get the port number
        cout << "Successfully resolved server host to IP: " << (unsigned short)dotQuad[0] << "." << (unsigned short)dotQuad[1] << "." << (unsigned short)dotQuad[2] << "." << (unsigned short)dotQuad[3];
        cout << " port " << SDLNet_Read16(&serverIP.port) << endl << endl;
    }
 
    // Try to open the server socket
    serverSocket = SDLNet_TCP_Open(&serverIP);
 
    if (!serverSocket)
    {
        cout << "Failed to open the server socket: " << SDLNet_GetError() << "\n";
        exit(-1);
    }
    else
    {
        cout << "Sucessfully created server socket." << endl;
    }
 
    // Add our server socket to the socket set
    SDLNet_TCP_AddSocket(socketSet, serverSocket);
 
    cout << "Awaiting clients..." << endl;
 
    // Main loop...
    do
    {
        // Check for activity on the entire socket set. The second parameter is the number of milliseconds to wait for.
        // For the wait-time, 0 means do not wait (high CPU!), -1 means wait for up to 49 days (no, really), and any other number is a number of milliseconds, i.e. 5000 means wait for 5 seconds
        int numActiveSockets = SDLNet_CheckSockets(socketSet, 0);
 
        if (numActiveSockets != 0)
        {
            cout << "There are currently " << numActiveSockets << " socket(s) with data to be processed." << endl;
        }
 
        // Check if our server socket has received any data
        // Note: SocketReady can only be called on a socket which is part of a set and that has CheckSockets called on it (the set, that is)
        // SDLNet_SocketRead returns non-zero for activity, and zero is returned for no activity. Which is a bit bass-ackwards IMHO, but there you go.
        int serverSocketActivity = SDLNet_SocketReady(serverSocket);
 
        // If there is activity on our server socket (i.e. a client has trasmitted data to us) then...
        if (serverSocketActivity != 0)
        {
            // If we have room for more clients...
            if (clientCount < MAX_CLIENTS)
            {
 
                // Find the first free socket in our array of client sockets
                int freeSpot = -99;
                for (int loop = 0; loop < MAX_CLIENTS; loop++)
                {
                    if (socketIsFree[loop] == true)
                    {
                        //cout << "Found a free spot at element: " << loop << endl;
                        socketIsFree[loop] = false; // Set the socket to be taken
                        freeSpot = loop;            // Keep the location to add our connection at that index in the array of client sockets
                        break;                      // Break out of the loop straight away
                    }
                }
 
                // ...accept the client connection and then...
                clientSocket[freeSpot] = SDLNet_TCP_Accept(serverSocket);
 
                // ...add the new client socket to the socket set (i.e. the list of sockets we check for activity)
                SDLNet_TCP_AddSocket(socketSet, clientSocket[freeSpot]);
 
                // Increase our client count
                clientCount++;
 
                // Send a message to the client saying "OK" to indicate the incoming connection has been accepted
                strcpy( buffer, SERVER_NOT_FULL.c_str() );
                int msgLength = strlen(buffer) + 1;
                SDLNet_TCP_Send(clientSocket[freeSpot], (void *)buffer, msgLength);
 
                cout << "Client connected. There are now " << clientCount << " client(s) connected." << endl << endl;
            }
            else // If we don't have room for new clients...
            {
                cout << "*** Maximum client count reached - rejecting client connection ***" << endl;
 
                // Accept the client connection to clear it from the incoming connections list
                TCPsocket tempSock = SDLNet_TCP_Accept(serverSocket);
 
                // Send a message to the client saying "FULL" to tell the client to go away
                strcpy( buffer, SERVER_FULL.c_str() );
                int msgLength = strlen(buffer) + 1;
                SDLNet_TCP_Send(tempSock, (void *)buffer, msgLength);
 
                // Shutdown, disconnect, and close the socket to the client
                SDLNet_TCP_Close(tempSock);
            }
 
        } // End of if server socket is has activity check
 
        // Loop to check all possible client sockets for activity
        for (int clientNumber = 0; clientNumber < MAX_CLIENTS; clientNumber++)
        {
            // If the socket is ready (i.e. it has data we can read)... (SDLNet_SocketReady returns non-zero if there is activity on the socket, and zero if there is no activity)
            int clientSocketActivity = SDLNet_SocketReady(clientSocket[clientNumber]);
 
            //cout << "Just checked client number " << clientNumber << " and received activity status is: " << clientSocketActivity << endl;
 
            // If there is any activity on the client socket...
            if (clientSocketActivity != 0)
            {
                // Check if the client socket has transmitted any data by reading from the socket and placing it in the buffer character array
                receivedByteCount = SDLNet_TCP_Recv(clientSocket[clientNumber], buffer, BUFFER_SIZE);
 
                // If there's activity, but we didn't read anything from the client socket, then the client has disconnected...
                if (receivedByteCount <= 0)
                {
                    //...so output a suitable message and then...
                    cout << "Client " << clientNumber << " disconnected." << endl << endl;
 
                    //... remove the socket from the socket set, then close and reset the socket ready for re-use and finally...
                    SDLNet_TCP_DelSocket(socketSet, clientSocket[clientNumber]);
                    SDLNet_TCP_Close(clientSocket[clientNumber]);
                    clientSocket[clientNumber] = NULL;
 
                    // ...free up their slot so it can be reused...
                    socketIsFree[clientNumber] = true;
 
                    // ...and decrement the count of connected clients.
                    clientCount--;
 
                    cout << "Server is now connected to: " << clientCount << " client(s)." << endl << endl;
                }
                else // If we read some data from the client socket...
                {
                    // Output the message the server received to the screen
                    cout << "Received: >>>> " << buffer << " from client number: " << clientNumber << endl;
 
                    // Send message to all other connected clients
                    int originatingClient = clientNumber;
 
                    for (int loop = 0; loop < MAX_CLIENTS; loop++)
                    {
                        // Send a message to the client saying "OK" to indicate the incoming connection has been accepted
                        //strcpy( buffer, SERVER_NOT_FULL.c_str() );
                        int msgLength = strlen(buffer) + 1;
 
                        // If the message length is more than 1 (i.e. client pressed enter without entering any other text), then
                        // send the message to all connected clients except the client who originated the message in the first place
                        if (msgLength > 1 && loop != originatingClient && socketIsFree[loop] == false)
                        {
                            cout << "Retransmitting message: " << buffer << " (" << msgLength << " bytes) to client number: " << loop << endl;
                            SDLNet_TCP_Send(clientSocket[loop], (void *)buffer, msgLength);
                        }
 
                    }
 
                    // If the client told us to shut down the server, then set the flag to get us out of the main loop and shut down
                    if ( strcmp(buffer, "shutdown") == 0 )
                    {
                        shutdownServer = true;
 
                        cout << "Disconnecting all clients and shutting down the server..." << endl << endl;
                    }
 
                }
 
            } // End of if client socket is active check
 
        } // End of server socket check sockets loop
 
    }
    while (shutdownServer == false); // End of main loop
 
    // Free our socket set (i.e. all the clients in our socket set)
    SDLNet_FreeSocketSet(socketSet);
 
    // Close our server socket, cleanup SDL_net and finish!
    SDLNet_TCP_Close(serverSocket);
 
    SDLNet_Quit();
 
    return 0;
}


Cобирать надо такой командой :

Код: Выделить всё
g++ server.cc -w -lSDL_net -o server



И код клинета


Код: Выделить всё
// Includes for non-blocking keyboard input
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <termios.h> // If we do not include termios.h the client WILL compile but it WILL NOT WORK!
#include <unistd.h>
#include <fcntl.h>

#include <iostream>
#include <string>

#include <SDL_net.h>

using namespace std;

const unsigned short PORT        = 1234; // The port we are connecting to
const unsigned short BUFFER_SIZE = 512;  // Size of our message buffer (i.e. maximum length of characters in a message)

struct termios orig_termios;

// Function to reset the terminal to blocking mode
void reset_terminal_mode()
{
    tcsetattr(0, TCSANOW, &orig_termios);
}

// Sets the terminal mode to conio mode
void set_conio_terminal_mode()
{
    struct termios new_termios;

    // Take two copies - one for now, one for later
    tcgetattr(0, &orig_termios);
    memcpy(&new_termios, &orig_termios, sizeof(new_termios));

    // register cleanup handler, and set the new terminal mode
    //atexit(reset_terminal_mode); // Commented out because I switch and swap terminal modes a lot - just remember to call reset_terminal_mode() when we finish up
    cfmakeraw(&new_termios);
    tcsetattr(0, TCSANOW, &new_termios);
}

// Fuction to check if a key has been pressed
int kbHit()
{
    // How long to wait for input
    // Note: As soon as we get input the wait is immediately over - so it's not like our typing rate is limited in any way!
    long waitSeconds      = 1L;
    long waitMicroSeconds = 0L;
    struct timeval tv = { waitSeconds, waitMicroSeconds };

    // Create a file descriptor set
    fd_set fds;

    FD_SET(0, &fds);
    return select(1, &fds, NULL, NULL, &tv);
}

// Function to read the contents of the keypress
int getch()
{
    int r;
    unsigned char c;
    if ((r = read(0, &c, sizeof(c))) < 0)
    {
        //cout << "About to return a number..." << endl;
        return r;
    }
    else
    {
        //cout << "About to return a character..." << endl;
        return c;
    }
}

int main(int argc, char **argv)
{
    const char *host;         // Where we store the host name

    IPaddress serverIP;       // The IP we will connect to
    TCPsocket clientSocket;   // The socket to use
    string    serverName;     // The server name

    string userInput = "";    // A string to hold our user input
    int inputLength  = 0;     // The length of our string in characters
    char buffer[BUFFER_SIZE]; // Array of character's we'll use to transmit our message. We get input into the userInput string for ease of use, then just copy it to this character array and send it.

    // Initialise SDL_net
    if (SDLNet_Init() < 0)
    {
        cout << "Failed to intialise SDN_net: " << SDLNet_GetError() << "\n";
        exit(-1); // Quit!
    }

    // Ask the user for a server to connect to - can be entered as a hostname (i.e. localhost etc.) or an IP address (i.e. 127.0.0.1 etc.)
    cout << "Server Name: ";
    //getline(cin, serverName); // Uncomment this and remove the below line to change the server we're connecting to...
    serverName = "localhost";

    // Create the socket set with enough space to store our desired number of connections (i.e. sockets)
    SDLNet_SocketSet socketSet = SDLNet_AllocSocketSet(1);
    if (socketSet == NULL)
    {
        cout << "Failed to allocate the socket set: " << SDLNet_GetError() << "\n";
        exit(-1); // Quit!
    }
    else
    {
        cout << "Successfully allocated socket set." << endl;
    }

    // Try to resolve the host. If successful, this places the connection details in the serverIP object
    int hostResolved = SDLNet_ResolveHost(&serverIP, serverName.c_str(), PORT);

    if (hostResolved == -1)
    {
        cout << "Failed to resolve the server hostname: " << SDLNet_GetError() << "\nContinuing...\n";
    }
    else // If we successfully resolved the host then output the details
    {
        // Get our IP address in proper dot-quad format by breaking up the 32-bit unsigned host address and splitting it into an array of four 8-bit unsigned numbers...
        Uint8 * dotQuad = (Uint8*)&serverIP.host;

        //... and then outputting them cast to integers. Then read the last 16 bits of the serverIP object to get the port number
        cout << "Successfully resolved host to IP: " << (unsigned short)dotQuad[0] << "." << (unsigned short)dotQuad[1] << "." << (unsigned short)dotQuad[2] << "." << (unsigned short)dotQuad[3];
        cout << " port " << SDLNet_Read16(&serverIP.port) << endl << endl;
    }

    // Try to resolve the IP of the server, just for kicks
    if ((host = SDLNet_ResolveIP(&serverIP)) == NULL)
    {
        cout << "Failed to resolve the server IP address: " << SDLNet_GetError() << endl;
    }
    else
    {
        cout << "Successfully resolved IP to host: " << host << endl;
    }

    // Flag to keep track of when to disconnect and finish up. We initially set it so that we CANNOT connect, and only change this to false when we got an "OK" response from the server
    bool shutdownClient = true;

    // Try to open a connection to the server and quit out if we can't connect
    clientSocket = SDLNet_TCP_Open(&serverIP);
    if (!clientSocket)
    {
        cout << "Failed to open socket to server: " << SDLNet_GetError() << "\n";
        exit(-1);
    }
    else // If we successfully opened a connection then check for the server response to our connection
    {
        cout << "Connection okay, about to read connection status from the server..." << endl;

        // Add our socket to the socket set for polling
        SDLNet_TCP_AddSocket(socketSet, clientSocket);

        // Wait for up to five seconds for a response from the server
        // Note: If we don't check the socket set and WAIT for the response, we'll be checking before the server can respond, and it'll look as if the server sent us nothing back
        int activeSockets = SDLNet_CheckSockets(socketSet, 5000);

        cout << "There are " << activeSockets << " socket(s) with data on them at the moment." << endl;

        // Check if we got a response from the server
        int gotServerResponse = SDLNet_SocketReady(clientSocket);

        if (gotServerResponse != 0)
        {
            cout << "Got a response from the server... " << endl;
            int serverResponseByteCount = SDLNet_TCP_Recv(clientSocket, buffer, BUFFER_SIZE);

            cout << "Got the following from server: " << buffer << "(" << serverResponseByteCount << " bytes)" << endl;

            // We got an okay from the server, so we can join!
            if ( strcmp(buffer, "OK") == 0 )
            {
                // So set the flag to say we're not quitting out just yet
                shutdownClient = false;

                cout << "Joining server now..." << endl << endl;
            }
            else
            {
                cout << "Server is full... Terminating connection." << endl;
            }
        }
        else
        {
            cout << "No response from server..." << endl;
        }

    } // End of if we managed to open a connection to the server condition

    bool wrotePrompt = false; // Whether or not we've already written the prompt
    bool sendMessage = false; // Whether or not it's time to send the message (flips to true when the user presses return)

    // While it's not time to shutdown the client...
    while (shutdownClient == false)
    {
        // Write the prompt only once per line of input. This gets reset so that it's displayed again after a message is sent
        if (wrotePrompt == false)
        {
            cout << "Write something:" << endl;
            wrotePrompt = true;
        }

        // If we've detected that the user has pressed a key..
        set_conio_terminal_mode();
        int status = kbHit();
        reset_terminal_mode();

        //cout << "status is: " << status << endl;

        if (status != 0)
        {
            //cout << "key was pressed and status is" << status << endl;

            // Get the keypress
            set_conio_terminal_mode();
            char theChar = getch();
            reset_terminal_mode();

            // Output the character to stdout
            cout << theChar;

            // Flush the character to the screen
            fflush(stdout);

            // If the keypressed wasn't return then add the character to our message string
            if ((int)theChar != 13)
            {
                //cout << "Got the character: " << theChar << " (which is number: " << int(theChar) << ")" << endl;

                // Add the character to our input string
                userInput += theChar;
            }
            else // Otherwise (if the user pressed enter) then send the message
            {
                //cout << "user pressed return" << endl;

                // Copy our user's string into our char array called "buffer"
                strcpy( buffer, userInput.c_str() );

                // Calculate the length of our input and then add 1 (for the terminating character) to get the total number of characters we need to send
                inputLength = strlen(buffer) + 1;

                // Send the message to the server
                if (SDLNet_TCP_Send(clientSocket, (void *)buffer, inputLength) < inputLength)
                {
                    cout << "Failed to send message: " << SDLNet_GetError() << endl;
                    exit(-1);
                }
                else
                {
                    //cout << "Message sent successfully." << endl;

                    // If we've asked the server to shutdown or we want out then set the flag appropriately
                    if (sendMessage == true && (userInput == "quit" || userInput == "exit" || userInput == "shutdown"))
                    {
                        shutdownClient = true;
                    }

                    // Reset for the next message
                    cout << endl;
                    wrotePrompt = false;
                    sendMessage = false;
                    userInput = "";
                }

            } // End of message sending section

        } // End of if the user pressed a key test

        // Check our socket set for activity. Don't wait if there's nothing on the socket just continue
        int socketActive = SDLNet_CheckSockets(socketSet, 0);

        //cout << "Sockets with data on them at the moment: " << activeSockets << endl;

        if (socketActive != 0)
        {
            // Check if we got a response from the server
            int messageFromServer = SDLNet_SocketReady(clientSocket);

            if (messageFromServer != 0)
            {
                //cout << "Got a response from the server... " << endl;
                int serverResponseByteCount = SDLNet_TCP_Recv(clientSocket, buffer, BUFFER_SIZE);

                cout << "Received: " << buffer << endl;// "(" << serverResponseByteCount << " bytes)" << endl;

                if (strcmp(buffer, "shutdown") == 0)
                {
                    cout << "Server is going down. Disconnecting..." << endl;
                    shutdownClient = true;
                }
            }
            else
            {
                //cout << "No response from server..." << endl;
            }

        } // End of if socket has activity check

    } // End of main while loop

    // Close our socket, cleanup SDL_net, reset the terminal mode and finish!
    SDLNet_TCP_Close(clientSocket);

    SDLNet_Quit();

    reset_terminal_mode();

    return 0;
}


Собирать надо такой командой

Код: Выделить всё
g++ client.cc -w -lSDL_net -o client


И получаем такой вот ЧАТ

Справочник и Форум Green Dragon
T-800 MOD для Heroes of Might and Magic 2

Основатель клуба "Армагеддон"

Основатель клуба "Пещера Гремлинов"

ИзображениеИзображение
Аватара пользователя
t800
Experienced
Experienced
 
Сообщения: 849
Зарегистрирован: 19 окт 2015, 14:52
Откуда: Казахстан, Алматинская область
Благодарил (а): 172 раз.
Поблагодарили: 136 раз.

Re: Учимcя программировать на C++

Сообщение t800 » 19 апр 2017, 07:48

Добавил Компилятрор С++ на форум

Справочник и Форум Green Dragon
T-800 MOD для Heroes of Might and Magic 2

Основатель клуба "Армагеддон"

Основатель клуба "Пещера Гремлинов"

ИзображениеИзображение
Аватара пользователя
t800
Experienced
Experienced
 
Сообщения: 849
Зарегистрирован: 19 окт 2015, 14:52
Откуда: Казахстан, Алматинская область
Благодарил (а): 172 раз.
Поблагодарили: 136 раз.

След.

Вернуться в Технические вопросы

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 8

cron