-1

Yesterday I started writing a code for server-client TCP/IP for chating.

It's code for server:

#include <iostream>
#include <winsock2.h>
#pragma comment(lib, "Ws2_32.lib")

int main() {
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);

    WSAStartup(Version, &wsa);

    SOCKET Listen = socket(AF_INET, SOCK_STREAM, 0);
    SOCKET Connect = socket(AF_INET, SOCK_STREAM, 0);

    SOCKADDR_IN Server;

    Server.sin_addr.s_addr = inet_addr("127.0.0.1");
    Server.sin_family = AF_INET;
    Server.sin_port = htons(100);

    bind(Listen, (SOCKADDR*)&Server, sizeof(Server));
    listen(Listen, 1);
    int size = sizeof(Server);
    char buffer[512];
    char *fn = "";
    int iResult;
    std::cout <<"Listening...";

    if(Connect = accept(Listen, (SOCKADDR*)&Server, &size)) {
        std::cout << "\nConnecting was reached :)";
    }


do {

       char *fn;

iResult = recv(Connect, buffer, sizeof(buffer), 0);
if (iResult > 0) {
buffer[iResult] = '\0';
fn = buffer;
        std::cout << fn;
} else if (iResult == 0){
            printf("Connection closing...\n");
        iResult = 0;
}


    } while (iResult > 0);


    WSACleanup();
    std::cin.get();
    return 0;
}

And it's for client:

#include <iostream>
#include <winsock2.h>
#include <string>

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

int main() {
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);

    WSAStartup(Version, &wsa);

    int iResult;

    std::string text;
char buffer[512];

    SOCKADDR_IN Client;

    Client.sin_addr.s_addr = inet_addr("127.0.0.1");
    Client.sin_family = AF_INET;
    Client.sin_port = htons(100);

    SOCKET Connect = socket(AF_INET, SOCK_STREAM, 0);

    std::cout << "Wcisnij enter aby polaczyc";
    std::cin.get();



    if(connect(Connect, (SOCKADDR*)&Client, sizeof(Client))) {
        std::cout << "Nawiazano polaczeniee";
    }

    do {

    std::cout << "Waxer: ";
        std::cin >> text;
        text.insert( 0, "Waxer: " );
strcpy_s(buffer, text.c_str());

// Send an initial buffer
    iResult = send(Connect, buffer, strlen(buffer), 0);
    if (iResult == SOCKET_ERROR) {
        printf("send failed with error: %d\n", WSAGetLastError());
        closesocket(Connect);
        WSACleanup();
        return 1;
    }


    } while(text != "ban");

    std::cout<< "Polaczenie wyslane!";
    std::cin.get();
    return 0;
}

My question is? What should I change in this code for multiple clients. I mean how to connect new clients?

crashmstr
  • 28,043
  • 9
  • 61
  • 79

1 Answers1

0

Your question is quite open, but in general you need threads.

Pseudocode:

int main() 
{
  std::vector<std::thread> clients;
  Socket listener("localhost", "1337");

  for (;;)
  {
    Socket tmp = listener.accept();
    clients.push_back(std::thread(&myHandlerFunction, tmp));
    clients.back().detach();      
  }
}

In the end you have a thread for every connected client and can codify your chat logic so that they can communicate with each other.

LMF
  • 463
  • 2
  • 10
  • Tkanki you. But What is &myHandlerFunction and tmp? And how "get" message form Clint who wrote IT? – user3599358 Nov 24 '14 at 20:21
  • As already said thats pseudocode, you need to implement everything by yourself. This code should only demonstrate how it generally works. *tmp* is the socket over which you can communicate with one client. I recommend you to read a good book/documentation over sockets or use librarys like boost::asio before you do try&error programming – LMF Nov 24 '14 at 21:36
  • Ok I unserstood. Can you recommend me any good tutorial for C++ on Windows? – user3599358 Nov 24 '14 at 21:59
  • Check out this [book list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). For socket programming I can recommend using boost::asio or reading the OS specific documentation. – LMF Nov 25 '14 at 08:09