Anonymní profil Lukas – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Anonymní profil Lukas – Programujte.comAnonymní profil Lukas – Programujte.com

 

Příspěvky odeslané z IP adresy 2001:718:2601:2c6:d51d:2a...–

Lukas
C / C++ › Obrazce v konzoli
8. 12. 2015   #207148

Zdravím,mam program ve kterém pres chat komunikuje client se serverem a potřebuju dodělat že kdyz zadam nejaky symbol tak mi to vypise na serveru nejaky obrazec, třeba smajlík neob tak neco a nevím si stím rady,zatím jsem rozjel jen zaklad vypisu pro hvězdičku,kdyby věděl co dal a jak budu rad za kazdou radu.

#include "Server.h"
#include<stdlib.h>

WSockServer::WSockServer(int RequestVersion)
{
    hSocket = INVALID_SOCKET;
    ClientSocket = INVALID_SOCKET;

    if(WSAStartup(MAKEWORD(RequestVersion, 0), &wsaData) == 0)        // Check required version
    {
        if(LOBYTE(wsaData.wVersion < RequestVersion))
        {
            throw "Requested version not available.";
        }
    }
    else
        throw "Startup failed.";
}

WSockServer::~WSockServer()
{
    if(WSACleanup() != 0)
        throw "Cleanup failed.";
    if(hSocket != INVALID_SOCKET)
        closesocket(hSocket);
    if(ClientSocket != INVALID_SOCKET)
        closesocket(hSocket);
}


void WSockServer::SetServerSockAddr(sockaddr_in *sockAddr, int PortNumber)
{
    sockAddr->sin_family = AF_INET;
    sockAddr->sin_port = htons(PortNumber);
    sockAddr->sin_addr.S_un.S_addr = INADDR_ANY;            // Listen on all available ip's
}


bool WSockServer::RunServer(int PortNumber)
{    
    if((hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)        // Create socket
        throw "Could not create socket.";
    SetServerSockAddr(&sockAddr, PortNumber);                                        // Fill sockAddr
    if(bind(hSocket, (sockaddr*)(&sockAddr), sizeof(sockAddr)) != 0)
        throw "Could not bind socket.";
    if(listen(hSocket, SOMAXCONN) != 0)
        throw "Could not put the socket in listening mode.";

    cout << "Server settings: " << endl;
    cout << "IP: " << inet_ntoa(sockAddr.sin_addr) << endl;
    cout << "PORT: " << ntohs(sockAddr.sin_port) << endl << endl;

    int SizeAddr = sizeof(ClientAddr);

    cout << "Waiting for clients... ";
    ClientSocket = accept(hSocket, (sockaddr*)(&ClientAddr), &SizeAddr);
    cout << "client found!" << endl;
    return true;
}

void WSockServer::StartChat()
{
    while (true)
    {
        int BytesRec = recv(ClientSocket, Buffer, sizeof(Buffer), 0);

        if (BytesRec == 0)
        {
            cout << "The client closed the connection. " << endl;
        }
        else if (BytesRec == SOCKET_ERROR)
        {
            throw "The client seems to be offline.";
        }
        else
        {
            Buffer[BytesRec] = 0;
            cout << "Client: " << Buffer << endl;
            if (strcmp(Buffer, "*") == 0){
                system("cls");
                for (int j = 0; j < 10; j++){
                    for (int i = 0; i < 10 + j; i++)
                        cout << "*";
                    cout << endl;
                }
            }
        }
    }
}

 

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý