Thread: HELP with chars!!!!

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    35

    Angry HELP with chars!!!!

    hi,
    please can ne one try and help me with this very annoying problem i have created a simple chat program BUT every time the message sent has less chars than last message sent there are chars left over from previous message

    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <commctrl.h>
    #include "resource.h"
    #include <stdio.h>
    #include <string>
    
    #define WM_SCK    WM_USER+1
    
    HBITMAP g_hbmBall = NULL;
    
    char aa[1000];
    char bb[1000];
    SOCKET s;
    
    char rBuffer[1000];
    
    LPHOSTENT lpHostEntry;
    
    struct sockaddr_in a;
    
    HWND hStatus,hButton,hEdit,hwnd,hConsole;
    
    int error = 0;
    int d;
    char ip[500];
    
    
    char szNewBuffer[1024];
    std::string szBuffer2;    //or rename it if you want
    
    void OutputTextToScreen(char *p)
    {
         szBuffer2.append(p);
         SetWindowText((HWND)hConsole, szBuffer2.c_str());
    }
    
    
    /* Declare Windows procedure */
    LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
    /* Make the class name into a global variable */
    char szClassName[ ] = "WindowsApp";
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
    {
        InitCommonControls();
    
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof(WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_FRUIT));
        wincl.hIconSm = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_FRUIT));
        wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL; /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use light-gray as the background of the window */
        wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
    
        /* Register the window class, if fail quit the program */
        if(!RegisterClassEx(&wincl)) return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx(
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Fruit MaFiA 2oo3 Chat v1.00 (Client)",         /* Title Text */
               WS_POPUP | WS_SYSMENU | WS_CAPTION, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               600,                 /* The programs width */
               495,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow(hwnd, nFunsterStil);
        /* Run the message loop. It will run until GetMessage( ) returns 0 */
        while(GetMessage(&messages, NULL, 0, 0))
        {
               /* Translate virtual-key messages into character messages */
               TranslateMessage(&messages);
               /* Send message to WindowProcedure */
               DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage( ) gave */
        return messages.wParam;
    }
    
    /* This function is called by the Windows function DispatchMessage( ) */
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_DESTROY:
            {
                    DeleteObject(g_hbmBall);
                    PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            }
            break;
            case WM_CREATE:
            {
    
                    hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,hwnd, (HMENU)IDC_MAIN_STATUS, GetModuleHandle(NULL), NULL);
                    if(hStatus == NULL)
                             MessageBox(hwnd, "Could not create Status Bar.", "Error", MB_OK | MB_ICONERROR);
    
                    hButton = CreateWindowEx(0, "BUTTON", "Connect",WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,130, 50, 90, 20, hwnd, (HMENU)IDC_MAIN_CONNECT, GetModuleHandle(NULL), NULL);
                    if(hButton == NULL)
                             MessageBox(hwnd, "Could not create a Connect Button.", "Error", MB_OK | MB_ICONERROR);
    
                    hButton = CreateWindowEx(0, "BUTTON", "Send",WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,230, 50, 90, 20, hwnd, (HMENU)IDC_MAIN_SEND, GetModuleHandle(NULL), NULL);
                    if(hButton == NULL)
                             MessageBox(hwnd, "Could not create a Connect Button.", "Error", MB_OK | MB_ICONERROR);
    
                    hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "127.0.0.1",WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,20, 50, 100, 20, hwnd, (HMENU)IDC_MAIN_IP, GetModuleHandle(NULL), NULL);
                    if(hEdit == NULL)
                             MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
    
                    hConsole = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | WS_VSCROLL  ,20, 120, 430, 210, hwnd, (HMENU)IDC_MAIN_CONSOLE, GetModuleHandle(NULL), NULL);
                    if(hConsole == NULL)
                             MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
    
                    hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | WS_VSCROLL  ,20, 340, 430, 90, hwnd, (HMENU)IDC_MAIN_TEXTAREA, GetModuleHandle(NULL), NULL);
                    if(hEdit == NULL)
                             MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
    
                    hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | WS_VSCROLL  ,460, 120, 120, 310, hwnd, (HMENU)IDC_MAIN_CLIENTS, GetModuleHandle(NULL), NULL);
                    if(hEdit == NULL)
                             MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
    
    
                    int statwidths[] = {200, -1};
    
                    SendMessage(hStatus, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM)statwidths);
                    SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Ready...");
    
                    g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BACKGROUND));
                    if(g_hbmBall == NULL)
                    MessageBox(hwnd, "Could not load IDB_BACKGROUND!", "Error", MB_OK | MB_ICONEXCLAMATION);
            }
            break;
            case WM_PAINT:
            {
                    BITMAP bm;
                    PAINTSTRUCT ps;
    
                    HDC hdc = BeginPaint(hwnd, &ps);
    
                    HDC hdcMem = CreateCompatibleDC(hdc);
                    HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmBall);
    
                    GetObject(g_hbmBall, sizeof(bm), &bm);
    
                    BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
                    SelectObject(hdcMem, hbmOld);
                    DeleteDC(hdcMem);
    
                    EndPaint(hwnd, &ps);
            }
            break;
            case WM_COMMAND:
            {
               switch( wParam )
               {
                case IDC_MAIN_CONNECT :
                {
    
                error = 0;
    
                int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_MAIN_IP));
                if(len > 0)
                {
                 int i;
                 char* buf;
    
                 buf = (char*)GlobalAlloc(GPTR, len + 1);
                 GetDlgItemText(hwnd, IDC_MAIN_IP, buf, len + 1);
                 strcpy(ip,buf);
                 GlobalFree((HANDLE)buf);
                }else{
                 MessageBox(0,"Please Enter A Address","Error",0);
                 error = 1;
                }
                if(error != 1){
                         WSADATA ws;
                         d = WSAStartup(0x101,&ws);
                         if(d != 0){
                                   SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"WSA...Failed");
                                   error = 1;
                         }else{
                                   SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"WSA...OK");
                         }
                }
                if(error != 1){
                         s = socket(AF_INET,SOCK_STREAM,0);
                         if(s == -1){
                                   SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Socket...Failed");
                                   error = 1;
                         }else{
                                   SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Socket...OK");
                         }
                }
                if(error != 1){
    
                         a.sin_family = AF_INET;
                         a.sin_port = htons(6000);
                         a.sin_addr.s_addr = inet_addr(ip);
    
                         WSAAsyncSelect(s, hwnd, WM_SCK, FD_READ | FD_CONNECT | FD_WRITE | FD_CLOSE);
    
                         d = connect(s, (struct sockaddr *)&a,sizeof(a));
                         if(d != 1){
                                   SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Connect...OK");
                                   error = 1;
                         }else{
                                   SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Connect...Failed");
                         }
                }
    
                }
                break;
                case IDC_MAIN_SEND :
                {
                int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_MAIN_TEXTAREA));
                if(len > 0)
                {
                 int i;
                 char* buf;
    
                 buf = (char*)GlobalAlloc(GPTR, len + 1);
                 GetDlgItemText(hwnd, IDC_MAIN_TEXTAREA, buf, len + 1);
                    sprintf(bb,"\0");
                    sprintf(bb,"Server Sending...\"%s\"\r\n",buf);
                    OutputTextToScreen(bb);
    			 d = send(s,buf,strlen(buf),0);
                }else{
                 MessageBox(0,"Please Enter A Message","Error",0);
                 error = 1;
                }
                }
                break;
               }
            }
            break;
            case WM_SCK:
            {
            switch(WSAGETSELECTEVENT(lParam))
            {
             case FD_READ:
             {
              recv(s, rBuffer, sizeof(rBuffer), 0);
              sprintf(bb,"Server Receiving...\"%s\"\r\n",rBuffer);
              OutputTextToScreen(bb);
             }
             break;
            }
            }
            break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    thx in advance
    Theres a sucker born every minute, but a swallower is harder to find

  2. #2
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    this is just a guess (i didn't actually read all the code..) but if you have a buffer that you store input in (i assume that's what rBuffer is?) then i think you'll need to clear it between inputs. just have a function such as:
    Code:
    void clearBuff(char buffer[], int size)
    {
       for(int i = 0; i < size; ++i)
          buffer[i] = '\0';
    }
    and call that before you get input. i may be wrong, since i didn't actually check all the code

    btw, what OS is that...?
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    35

    YAY

    thx alot mad hatter im using xp but i doesnt matter no thx alot
    Theres a sucker born every minute, but a swallower is harder to find

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  2. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  3. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 12:47 PM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 02:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM