Wrong output

This is a discussion on Wrong output within the Networking/Device Communication forums, part of the General Programming Boards category; Hi iv'e been working on a simple client server based chat room \ IM thing as my first networking project. ...

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    Wrong output

    Hi iv'e been working on a simple client server based chat room \ IM thing as my first networking project.
    everything seems to be in order except after about 2 or 3 sent strings of sent text the first half of the string is either missing or overwritten by an old string and i i can't see where the problem is if it's either sending wrong or the server is doing something with it when it recives it
    Theres also another error in the server where it wont let more then 2 clients connect but that one isn't bothering me as much

    sorry about the lenghth of the code
    Server:
    Code:
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <iostream>
    
    #define MAXCLIENTS 10
    #define WELCOMEMSG "Welcome Console Chat V1.0"
    #define NONBLOCK 1
    
    using namespace std;
    
    int main()
    {
    WSADATA wsaData;
    char port[5] = "8888";
    
    cout << "Connect To Port: ";
    //cin  >> port;
    cout << "\n Connecting...\n";
    
    int status =  0; //Error checker
    int sockdis = 0; //Socket Discriptor
    int clientx = 0; //starting socket
    int clientsock[MAXCLIENTS]; //client sockets
    struct sockaddr_storage client_addr[clientx];
    socklen_t addr_size[clientx];
    
    struct addrinfo base, *server, *x; //Set up address struct
    memset(&base, 0, sizeof base);     //Clean out address struct
    base.ai_family = AF_INET;        //Set address struct's params
    base.ai_socktype = SOCK_STREAM;
    base.ai_flags = AI_PASSIVE;
    
    
    /* INITIATE WSASTARTUP */
    status = WSAStartup(MAKEWORD(2, 2), &wsaData);
         cout << "Initalizing WSAStartup............ ";
         if(status != 0){cout << "Error! \n";}
         else{cout << "Success! \n";}
    
    /* COLECT ADRESS INFORMATION */
    status = getaddrinfo(NULL, port, &base, &server);
         cout << "Collecting Address Information.... ";
         if(status != 0){cout << "Error! \n";}
         else{cout << "Success! \n";}
    
    /* FIND AVALIABLE SOCKET */
    for(x = server; x != NULL; x = x->ai_next)
    {sockdis = socket(x->ai_family, x->ai_socktype, x->ai_protocol);}
         cout << "Connecting To Socket.............. ";
         if(sockdis == -1){cout << "Error! \n" ;}
         else{cout << "Success! \n";}
         
    /* BIND SOCKET TO PORT */
    {status = bind(sockdis, server->ai_addr , server->ai_addrlen);}
         cout << "Binding To Port................... ";
         if(status != 0){status = WSAGetLastError(); cout << "Error: " << status << endl;}
         else{cout << "Success! \n";}
    
    /* WAIT FOR CLIENTS */
    status = listen(sockdis, 15);
         cout << "Listening On Port................. ";
         if(status != 0){status = WSAGetLastError(); cout << "Error: " << status << endl;}
         else{cout << "Success! \n\n";
         cout << "Connected to socket:  "<< sockdis << endl;
         cout << "Connected to Port:    "<< port << endl;}
    
      cout << "\nWaiting for client to connect...";
    
    /* MAKE SOCKET NON BLOCKING MODE */
    u_long mode = 1;
    ioctlsocket(sockdis, FIONBIO, &mode);
    
    char Data[1024];
    int bytesrecvd;
    int a = 0, b =0; //loop counters
    
    
    /* LISTENING LOOP */
    while(1)
    {
        clientsock[clientx] = -1;
    
        while(clientsock[clientx] == -1)
        {
          clientsock[clientx] = accept(sockdis, (struct sockaddr *)&client_addr[clientx], &addr_size[clientx]);
       
    // ------------------------ IM BOUNCEBACK LOOP ------------------------ //
    
    for(a = 0; a < clientx ; a++)
    {
    
    if(bytesrecvd = recv(clientsock[a] , (char*)Data, sizeof Data, 0) > 1)// CHECK IF ANYTHING WAS SENT
    {
    
    cout << Data << endl <<"Reviced Returned: " << bytesrecvd << endl;
    
         for(b = 0; b < clientx ; b++)
              {
                   bytesrecvd = send(clientsock[b] , (char*)Data, sizeof Data, 0); //SEND RECIVED DATA TO ALL CLIENTS
                    cout << Data << endl <<"Send Returned: " << bytesrecvd << endl;
              }
    
    }
    }
    // -------------------- END OF IM BOUNCEBACK LOOP --------------------- //
    
       }// END OF WHILE
        
        cout << "Client Connected!" <<endl << "Client Port: " << clientsock[clientx] << "\n\n";
        send(clientsock[clientx] , (char*)WELCOMEMSG, sizeof WELCOMEMSG, 0);//welcome message
        
    
    /* MAKE SOCKET NON BLOCKING MODE */
    //long mode = 1
    ioctlsocket(clientsock[clientx], FIONBIO, &mode);
    
        clientx++;
        
        if(clientx > MAXCLIENTS)
        {cout << "clientx= " <<clientx<<endl;
        cout << "MAX CLIENTS RACHED!";}
    
      }
    
    WSACleanup();
    freeaddrinfo(server);
    system("pause");
        }//END OF MAIN
    And heres the client:
    Code:
    /** IM CLIENT **/
    
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <iostream>
    #include <string.h>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
    WSADATA wsaData;
    char port[10] = "8888";
    char ipaddress[] = "192.168.56.1";
    
    cout << "Connect To Server IP: ";
    //cin  >> ipaddress;
    cout << "Using Port: ";
    //cin  >> port;
    cout << "\n Connecting...\n";
    
    int status =  0; //Error checker
    int sockdis = 0; //Socket Discriptor
    //int clientsock; //client sockets
    
    struct addrinfo base, *server, *x; //Set up address struct
    memset(&base, 0, sizeof base);     //Clean out address struct
    base.ai_family = AF_INET;        //Set address struct's params
    base.ai_socktype = SOCK_STREAM;
    base.ai_flags = AI_PASSIVE;
    
    /* INITIATE WSASTARTUP */
    status = WSAStartup(  MAKEWORD(2, 2), &wsaData);
         cout << "Initalizing WSAStartup............ ";
         if(status != 0){cout << "Error! \n";}
         else{cout << "Success! \n";}
    
    /* COLECT ADRESS INFORMATION */
    status = getaddrinfo(ipaddress, port, &base, &server);
         cout << "Collecting Address Information.... ";
         if(status != 0){cout << "Error! \n";}
         else{cout << "Success! \n";}
         
    /* FIND AVALIABLE SOCKET */
    for(x = server; x != NULL; x = x->ai_next)
    {sockdis = socket(x->ai_family, x->ai_socktype, x->ai_protocol);}
         cout << "Connecting To Socket.............. ";
         if(sockdis == -1){cout << "Error! \n" ;}
         else{cout << "Success! \n";}
    
    /* CONNECT TO SERVER */
       cout << "Connecting To Server.............. ";
       
    status = connect(sockdis, server->ai_addr, server->ai_addrlen);
         
         if(status != 0){status = WSAGetLastError();
         cout << "Error!: " << status << "\nConnection Failed.\n\n";} 
         else{cout << "Success! \nConnected!\n\n";}
         
    /* MAKE SOCKET NON BLOCKING MODE */
    u_long mode = 1;
    ioctlsocket(sockdis, FIONBIO, &mode);
    
    
    //********************* CHAT CODE STARTS HERE  *************************//
    string Name;
    char Datain[1024];
    int y, Msgcount = 0; //loop counter, Messagelofcounter
    int ilogcount = 0;
    int BytesTransfered;
    char MessLog[21][1024];
    char InputLog[1024];
    
    cout << "Cleaning Messages...\n\n";
    for(y =0; y < 21; y++)//clean the message loop
    {
    for(Msgcount = 0; Msgcount < 1024; Msgcount++)//clean the message loop
    {
    MessLog[y][Msgcount] = 0;
    }}
    Msgcount = 0;
    
    cout << "Please Enter Name: ";
    cin >> Name;
    cout << "Type '^' To Quit" << endl;
    
    while(1)
    {
    
    /*********** RECIVING MESSAGES ***********/
    
    
    if(BytesTransfered = recv(sockdis, (char*)Datain, sizeof Datain, 0) > 1) //RECIVE DATA FROM OTHER CLIENTS
    {
        system("cls"); //clear screen
    
        strcpy(&MessLog[Msgcount][0] ,Datain);
    
    /*
    if(Msgcount < 20) //roll back messages
    {
    for(y = 1; y < 20 ; y++)
    {
    strcpy(MessLog[y], MessLog[y-1]);
    }
    
    }
    else
    { */    
          Msgcount++;
    //}
    
    
    for(y =0; y < 20; y++)
    {
          cout << MessLog[y]; //loop to print messge log
          cout << endl;
    }
    
    cout << "----------------------------------------------------\n" << Name << ": ";
    }
    
    
    /*********** KEYBOARD HANDELING ***********/
    
    if(kbhit()) //if the keyboard is hit...
    {
    
    InputLog[ilogcount] = getch(); //GET INPUT
    cout << InputLog[ilogcount];
    
    if(InputLog[ilogcount] == 0x8 )//BACKSPACE WAS HIT
    {--ilogcount;} 
    
    if(InputLog[ilogcount] == 0xd ) //ENTER WAS HIT
    {
    
    BytesTransfered = send(sockdis, (char*)InputLog, sizeof InputLog, 0); //SEND DATA TO SERVER
    
    ilogcount = 0; //RESET LOG COUNTER
    }
    
    else //if enter isn't hit
    {ilogcount++;}
    
    }// end of keyboard handing
    
    }//end of while loop
    
    //******************************** END OF CHAT CLIENT
    
    closesocket(sockdis);
    freeaddrinfo(server);
    WSACleanup();
    system("pause");
        }//END OF MAIN

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    bytesrecvd = send(clientsock[b] , (char*)Data, sizeof Data, 0); //SEND RECIVED DATA TO ALL CLIENTS
    your Data array contains only bytesrecvd bytes of info - the rest is garbage, why are you sending the whole array instead of bytesrecvd bytes?

    same problem goes for client - you have only ilogcount bytes to send - but send the whole array

    you do not send nul-terminator - but on receiving side - process buffer as if it is nul-terminated...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    Cheers thanks for the help man it's been bugging me for almost 3 days i've sorted it out and it's all running smoothly, now iv'e just gotta put on some finishing touches

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 03:50 PM
  2. Wrong Output!
    By kolliash in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2008, 07:55 AM
  3. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 06:58 PM
  4. Why is the output of this wrong?
    By Tokimasa in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2004, 12:58 PM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 11:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21