15 July, 2011

Simple Chat Program + Source

Hello Programmers,

It's a very simple chat program. The only problem is that the server doesn't handle multiple clients. I'll make one as soon as I gain in depth knowledge in Winsocks. If possible, can anyone link me to a guide to learn about handling multiple clients?

Client source:
Code:



#include <iostream>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

SOCKET sock;

void recvdata()
{
    while(1)
    {
        char msg[256];
        memset(msg, 0, 256);
        int r = recv(sock, msg, 256, 0);
        if(r == SOCKET_ERROR)
        {
            std::cout << "\n\nServer is disconnected\n\n";
            std::cin.ignore();
            std::cin.get();
            closesocket(sock);
            WSACleanup();
            exit(1);
        }
        else
            std::cout << "Friend: " << msg << std::endl;
    }
}

void senddata()
{
    while(1)
    {
        char msg[256];
        memset(msg, 0, 256);
        std::cin.get(msg, 256);
        int ret = send(sock, msg, strlen(msg), 0);
        if(ret == SOCKET_ERROR)
        {
            std::cout << "\n\nServer is disconnected\n\n";
            std::cin.ignore();
            std::cin.get();
            closesocket(sock);
            WSACleanup();
            exit(1);
        }
        std::cin.ignore();
    }
}

int main()
{
    WSADATA wsData;
    if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
    {
        std::cout << "WSAStartup!\n";
    }
    else
    {
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(sock == INVALID_SOCKET)
        {
            std::cout << "socket()\n";
            WSACleanup();
        }
        else
        {
            sockaddr_in sin;
            sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
            sin.sin_port = htons(626);
            sin.sin_family = AF_INET;

            while(connect(sock, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
                connect(sock, (sockaddr*)&sin, sizeof(sin));
            std::cout << "Connected to your friend\n\n";
            HANDLE hThread[2];
            DWORD thread1, thread2;
            hThread[0] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)recvdata, NULL, NULL, &thread1);
            hThread[1] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)senddata, NULL, NULL, &thread2);
            WaitForMultipleObjects(2, hThread, true, INFINITE);
        }
    }

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

Server source:

Code:
/**

Program: Simple chat program
Author: Sri Krishna

**/

#include <iostream>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

SOCKET sock, acceptsock;

void recvdata()
{
    while(1)
    {
        char msg[256];
        memset(msg, 0, 256);
        int r = recv(sock, msg, 256, 0);
        if(r == SOCKET_ERROR)
        {
            std::cout << "\n\nServer is disconnected\n\n";
            std::cin.ignore();
            std::cin.get();
            closesocket(sock);
            WSACleanup();
            exit(1);
        }
        else
            std::cout << "Friend: " << msg << std::endl;
    }
}

void senddata()
{
    while(1)
    {
        char msg[256];
        memset(msg, 0, 256);
        std::cin.get(msg, 256);
        int ret = send(sock, msg, strlen(msg), 0);
        if(ret == SOCKET_ERROR)
        {
            std::cout << "\n\nServer is disconnected\n\n";
            std::cin.ignore();
            std::cin.get();
            closesocket(sock);
            WSACleanup();
            exit(1);
        }
        std::cin.ignore();
    }
}

int main()
{
    WSADATA wsData;
    if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
    {
        std::cout << "WSAStartup!\n";
    }
    else
    {
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(sock == INVALID_SOCKET)
        {
            std::cout << "socket()\n";
            WSACleanup();
        }
        else
        {
            sockaddr_in sin;
            sin.sin_addr.S_un.S_addr = INADDR_ANY;
            sin.sin_port = htons(626);
            sin.sin_family = AF_INET;

            while(bind(sock, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
                bind(sock, (sockaddr*)&sin, sizeof(sin));

            while(listen(sock, 1) == SOCKET_ERROR)
            {
                listen(sock, 1);
            }

            while(1)
            {
                acceptsock = SOCKET_ERROR;
                while(acceptsock == SOCKET_ERROR)
                {
        acceptsock = accept(sock, NULL, NULL);
                }
                std::cout << "Connected to your friend!\n\n";
                sock = acceptsock;
                break;
            }
        }
    }
    HANDLE hThread[2];
    DWORD thread1, thread2;
    hThread[0] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)recvdata, NULL, NULL, &thread1);
    hThread[1] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)senddata, NULL, NULL, &thread2);
    WaitForMultipleObjects(2, hThread, true, INFINITE);
    std::cin.ignore();
    std::cin.get();
    return 0;
}

Client code with C++ highlights: http://pastebin.com/Zbc7qwDc
Server code with C++ highlights: http://pastebin.com/R3stNU0v

Also, I need your guys' favor. The only way I can improve my knowledge is by receiving projects that require me to search for new methods and concepts. So, if possible, give me some tough projects or tough concepts to research. I need some concepts to learn....

Thanks,

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home