Thread: chat program client server ---- debug help

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    4

    chat program client server ---- debug help

    hi guys/girls, im trying to learn C++ and followed a video from youtube (https://www.youtube.com/watch?v=IydkqseK6oQ) to write some code only the program doesnt behave as required and the server is always looking for connections and never finds one....... does anyone know why this might be i dont get compile time errors.

    Here's the code for the server:

    Code:
    #include<iostream>
    #include<string.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<netinet/ip.h>
    using namespace std;
    
    int main()
    {
        int client, server;
        int portNum = 1500;
        bool isExit = false;
        int bufsize = 1024;
        char buffer[bufsize];
        
    struct sockaddr_in server_addr;
        socklen_t size;
    
    // init socket
    
        client = socket(AF_INET, SOCK_STREAM, 0);
    
        if (client < 0)
        {
        cout << "Error establishing connection" << endl;
        exit(1);
        }
    
        cout << "Server Socket connection created..." << endl;
    
        server_addr.sin_family = AF_INET;
        server_addr.sin_addr.s_addr = htons(INADDR_ANY);
        server_addr.sin_port = htons(portNum);
    
    // Binding socket  
    
        if (bind(client, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
        {
        cout << "Error binding socket...." << endl; 
        exit(1);
        }
    
    size = sizeof(server_addr);
    cout << "Looking for clients..." << endl;
    
    // Listening socket
    
        listen(client, 1);
    
    // Accept client
    
        server = accept(client, (struct sockaddr*)&server_addr, &size);
    if (server < 0)
    {
    cout << "Error on accepting.." << endl;
    exit(1);
    }
    
    while (server > 0)
    {
        strcpy(buffer, "Server connected...\n");
        send(server, buffer, bufsize, 0);
        
        cout << "Connected with client..." << endl;
        cout << "Enter # to end the connection" << endl;
    
        cout << "Client: ";
    do {
            recv(server, buffer, bufsize, 0);
            cout << "buffer" << " ";
            if (*buffer == '#')
            {        
                *buffer = '*';
                isExit = true;
            }    
       }
    
    while (*buffer != '*');
        
        do{
               cout << "\nServer: ";
            do{
                cin >> buffer;
                send(server, buffer, bufsize, 0);
                if (*buffer == '#')
            {
                send(server, buffer, bufsize, 0);
                *buffer = '*';
                isExit = true;
            
            }
    }while (*buffer != '*');
        cout << "Client: ";
        do {
            recv(server, buffer, bufsize, 0);
            cout << buffer << " ";
            if(*buffer == '#')
            {
                *buffer == '*';
                isExit = true;
            }
        }
    while (*buffer != '*');
    }
    while (!isExit);
    cout << "Connection terminated..." << endl;
    cout << "Goodbye..." << endl;
    isExit = false;
    exit(1);
    }
    close(client);
    return 0;
    }
    here's the code for the client:
    Code:
    #include<iostream>
    #include<string.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<netdb.h>
    
    using namespace std;
    
    int main()
    {
    
    int client, server;
        int portNum = 1500;
        bool isExit = false;
        int bufsize = 1024;
        char buffer[bufsize];
        char *ip = "127.0.0.1";
    
        struct sockaddr_in server_addr;
    
    // Init socket 
    
    
        client = socket(AF_INET, SOCK_STREAM, 0);
    
        if (client < 0)
        {
        cout << "Error creating socket" << endl;
        exit(1);
        }
    
    cout << "Client socket created" << endl;
    
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(portNum);
    
    // Connecting socket server
    
    if (connect(client, (struct sockaddr*)&server_addr, sizeof(server_addr)) == 0)
    
    {
         cout << "Connecting to server..." << endl;
    }
        recv(client, buffer, bufsize, 0);
        cout << "Connection confirmed" << endl;
        cout << "Enter # to end the connection" << endl; 
    
    do {
        cout << "Client: ";
        do {
            cin >> buffer;
            send(client, buffer, bufsize, 0);
            if (*buffer == '#')
            {    
                send(client, buffer, bufsize, 0);
                *buffer = '*';
                isExit = true;
            }
        }
        while (*buffer != 42);
    
        cout << "Server: ";
    
        do{
            recv(client, buffer, bufsize, 0);
            cout << buffer << " ";
            
        if(*buffer == '#')
            {
                *buffer == '*';
                isExit = true;
            }
    
        }
        while (*buffer != 42);
    
        cout << endl;
    }
        while(!isExit);
            cout << "Connection terminated..." << endl;
            cout << "Goodbye..." << endl;
    
        close(client);
    
        return 0;
    }
    thanks in advance guys/girls....
    Last edited by yoyo123456; 01-07-2016 at 07:54 PM. Reason: added youtube source

  2. #2
    Registered User
    Join Date
    Jan 2016
    Posts
    4
    server side code - i added a line which allows the connections to work, but the dialogue is still missing.....

    server_addr.sin_addr.s_addr = inet_addr("192.168.0.5");

    Code:
    #include<iostream>
    #include<string.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<netdb.h>
    
    using namespace std;
    
    int main()
    {
    
    int client, server;
        int portNum = 1500;
        bool isExit = false;
        int bufsize = 1024;
        char buffer[bufsize];
        char *ip = "127.0.0.1";
    
        struct sockaddr_in server_addr;
    
    // Init socket 
    
    
        client = socket(AF_INET, SOCK_STREAM, 0);
    
        if (client < 0)
        {
        cout << "Error creating socket" << endl;
        exit(1);
        }
    
    cout << "Client socket created" << endl;
    
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(portNum);
    server_addr.sin_addr.s_addr = inet_addr("192.168.0.5");
    // Connecting socket server
    
    if (connect(client, (struct sockaddr*)&server_addr, sizeof(server_addr)) == 0)
    
    {
         cout << "Connecting to server..." << endl;
    }
        recv(client, buffer, bufsize, 0);
        cout << "Connection confirmed" << endl;
        cout << "Enter # to end the connection" << endl; 
    
    do {
        cout << "Client: ";
        do {
            cin >> buffer;
            send(client, buffer, bufsize, 0);
            if (*buffer == '#')
            {    
                send(client, buffer, bufsize, 0);
                *buffer = '*';
                isExit = true;
            }
        }
        while (*buffer != 42);
    
        cout << "Server: ";
    
        do{
            recv(client, buffer, bufsize, 0);
            cout << buffer << " ";
            
        if(*buffer == '#')
            {
                *buffer == '*';
                isExit = true;
            }
    
        }
        while (*buffer != 42);
    
        cout << endl;
    }
        while(!isExit);
            cout << "Connection terminated..." << endl;
            cout << "Goodbye..." << endl;
    
        close(client);
    
        return 0;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,436
    Start by copy/pasting Beej's examples.
    Beej's Guide to Network Programming
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    4
    Quote Originally Posted by Salem View Post
    Start by copy/pasting Beej's examples.
    Beej's Guide to Network Programming
    any chance to help debug the OP?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,436
    Quote Originally Posted by yoyo123456 View Post
    any chance to help debug the OP?
    Not really, I've no interest in wading through badly indented code based on guesswork.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    4

    Cool

    Quote Originally Posted by Salem View Post
    Not really, I've no interest in wading through badly indented code based on guesswork.
    ok thanks anyways, i managed to fix it anyhow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Asynchronous chat server/client + UI
    By Annonymous in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 05-02-2012, 10:40 PM
  2. Chat udp server/client
    By Phoenix_Rebirth in forum C Programming
    Replies: 1
    Last Post: 11-17-2008, 12:30 PM
  3. Chat Client/Server Problems
    By _Cl0wn_ in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 11:38 AM
  4. Chat server/client trial -- anyone interested?
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-23-2003, 10:47 PM
  5. Client/Server chat program! Please Help
    By Silverdream in forum C Programming
    Replies: 2
    Last Post: 03-21-2002, 06:34 AM

Tags for this Thread