CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2008
    Posts
    29

    Question finding problem in recieving message via socket

    Hi...

    I am facing a problem in receiving a message from the server. Actually I am developing a server cleint code using WinAPIs(not MFC). My server is a property sheet which has a send button and a edit box . Same is there on the client. My client is a dialog box having an edit box and a recieve button. Now my problem is while if I write anything on the server'z edit box and click send I am able to send the message which I am checking by a message box and on the client side I am able to recieve also which is also checked by message box . But I am not able to produce the same message been recieved on the client's edit box. I have the client and the server code and I am not able to fine where exactly my problem lies.

    On the server side I have

    char szSendBuffer[1024] = GetDlgItemString(IDC_EDIT_SEND);// IDC_EDIT_SEND is the ID for the edit box.

    if(send(ClientSocket,&szSendBuffer[0],sizeof(szSendBuffer),0)== -1)

    MessageBox(NULL,TEXT("Message cant be send"),NULL,MB_ICONERROR);
    else
    MessageBox(NULL,TEXT("Message is succesfully send to the client"),NULL,MB_ICONERROR);

    The GetDlgItemString function is

    char GetDlgItemString(int nIDDlgItem)

    {
    int nLength = 1 + ::GetWindowTextLength(GetDlgItem(hModelssDialog, nIDDlgItem));

    char String;

    if (nLength > 0)
    {
    TCHAR* szString = new TCHAR[nLength];
    if (NULL == szString)
    MessageBox(NULL,TEXT("string is null"),NULL,MB_ICONERROR);
    ::GetDlgItemText(hModelssDialog, nIDDlgItem, szString, nLength);
    String = (char)szString;

    delete []szString;
    }

    return String;
    }

    Here I am recieving the message box of message being send

    In the client side I have

    ZeroMemory(szRecvBuffer,sizeof(szRecvBuffer));

    if(recv(ClientSocket,&szRecvBuffer[0],sizeof(szRecvBuffer),0) == -1)

    MessageBox(NULL,TEXT("Data is not recieved from the server!"),NULL,MB_ICONERROR);

    else

    MessageBox(NULL,TEXT("Data is recieved from the server!"),NULL,MB_ICONERROR);

    Append(IDC_EDIT_RECEIVE, szRecvBuffer);

    Here I recieve the messagebox comments as Data is recieved.

    Now in the Append function I have

    HWND hEdit = GetDlgItem(hModelssDialog1, nID);
    EnableWindow(hEdit, TRUE);

    int ndx = GetWindowTextLength (hEdit);

    SendMessage (hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx);

    if(SendMessage (hEdit, EM_REPLACESEL, 0, (LPARAM) buf)== -1)

    MessageBox(NULL,TEXT("Message cant be shown"),NULL,MB_ICONERROR);

    else
    MessageBox(NULL,TEXT("Message is shown"),NULL,MB_ICONERROR);

    This is the main function I am concerened with. Here I am not sure whether for printing the message on the edit box my approach is correct. But I am recieving the message box message as message is shown"

    Please help me in soughting out this problem.

    Thanks in advance!!!!!!!!!!!

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: finding problem in recieving message via socket

    Well just from looking at your code, it is a mistake to display your network messages in a MessageBox because it causes your thread of execution to pause waiting for a response. It would be better to display it in a screen control such as an edit box. This will not disrupt the execution of your server.
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Sep 2008
    Posts
    29

    Re: finding problem in recieving message via socket

    Hi..thanks for your reply ......I will ultimately delete those message boxes in future implementations ...those I have put just as to check the flow of my code. Actually you got it right I have to send the messages to the edit boxes only but somehow its not been shown.In the client I am able to recieve the message from the server but how t o show it in the edit box. I have written the code for the client where I was trying to do that only in the send message function but somehow its not getting shown on the edit box. I even dont know my approach is correct or not. If you help me how to show the message on that message box it will be a great help.


    Thanks in advance!!!!!!!!!!!!!!!!!!!!!!!

  4. #4
    Join Date
    Sep 2008
    Posts
    29

    Unhappy Re: finding problem in recieving message via socket

    Hi.........I am indeed in deep trouble....I have a property sheet which is my server and I want to have a handle to one of its pages which I am not able to get.

    I am using PropSheet_GetCurrentPageHwnd(Dlg) macro to get my handle but its not working as when I check this by a message box its shows t hat the handle is NULL. I am defining it on my Send() function from where I am sending the data from the property sheet(server) to my client.

    Any help on this will be a great help to me.......Thanks in advance!!!!!!!!!!!!

  5. #5
    Join Date
    Sep 2008
    Posts
    29

    Re: finding problem in recieving message via socket

    Please help me out............

  6. #6
    Join Date
    Aug 2007
    Location
    Birmingham, UK
    Posts
    360

    Re: finding problem in recieving message via socket

    A lot of people will ignore your post since it contains code without code tags. Please edit your fist post and add the code tags and ensure your code is formatted correctly. How to do this you can read in the "before you post" link that is at the top of each forum here.

  7. #7
    Join Date
    Nov 2008
    Posts
    2

    Re: finding problem in recieving message via socket

    send() and recv() are not guaranteed to send or receive the total number of bytes specified in the length parameter. In order to ensure all bytes are sent or received on a blocking socket, you could loop until the total number of bytes returned reaches the number in your length parameter, or until a socket error occurs. Keep in mind that in addition to keeping track of the total number of bytes returned, you also need to add the total to the data parameter and subtract it from the length parameter after each iteration in order to have the correct offset and buffer length for each call.

  8. #8
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: finding problem in recieving message via socket

    This code clearly does not do what you think it does or what you want it to do:
    Code:
    char GetDlgItemString(int nIDDlgItem)
    {
      int nLength = 1 + ::GetWindowTextLength(GetDlgItem(hModelssDialog, nIDDlgItem));
      char String;
      if (nLength > 0)
      {
        TCHAR* szString = new TCHAR[nLength];
        if (NULL == szString)
          MessageBox(NULL,TEXT("string is null"),NULL,MB_ICONERROR); 
        ::GetDlgItemText(hModelssDialog, nIDDlgItem, szString, nLength);
        String = (char)szString;
    
        delete []szString;
      }
    
      return String;
    }
    
    // in use ....
    
    char szSendBuffer[1024] = GetDlgItemString(IDC_EDIT_SEND);// IDC_EDIT_SEND is the ID for the edit box.
    
    if(send(ClientSocket,&szSendBuffer[0],sizeof(szSendBuffer),0)== -1)
    
    // etc ...
    PS: Please use [ code ][ /code ] tags as shown in my post above

  9. #9
    Join Date
    Sep 2008
    Posts
    29

    Re: finding problem in recieving message via socket

    Thanks for your advice I will take care of that. Now coming back to my problem....let me explain it briefly....

    When I am sending the message from a dialog box which is supposed to be my client i have used the following code snippet, and this is working fine as by applying breakpoints(during debugging) on my server code I was able to get that message from the clinet

    code for the client

    Code:
       hEd= GetDlgItem(hModelssDialog1,IDC_EDIT_SEND);
       ZeroMemory(szSendBuffer,sizeof(szSendBuffer));
       GetWindowText(hEd,(LPWSTR)&szSendBuffer[0],sizeof(szSendBuffer);
       send(ClientSocket,szSendBuffer,strlen(szSendBuffer),0);
    Here hModelssDialog1 is the handle to my dialog box(client) and hEd is my handle to the edit box which has the ID as IDC_EDIT_SEND. This codeis working fine. Now t he problem arises in the server code which is as follows

    code for the server

    Code:
       ZeroMemory(szRecvBuffer,sizeof(szRecvBuffer));
       recv(ClientSocket,szRecvBuffer,sizeof(szRecvBuffer),0);
       
       hPropPage=GetActiveWindow();
       
      SetDlgItemText(hPropPage,IDC_EDIT_RECEIVE,(LPCWSTR)szRecvBuffer);
    Let me tell you something about my server. I have a dialogbox in which I have a modal porperty sheet, my property sheet is the server. Now first of all I am not getting the handle of the pages for the property sheet that I require to put on SetDlgItemText so I have used hPropPage to get the active window but its not working at all. Actually I have a button called receive which has a function OnReceive() in which I have implemented the above code of my server. SO I was thinking that as soon as I will click the receive button then it will be a active window and my hPropPage will work.....But I was wrong.... there is something I am not able to do.Although I am able to recieve t hat message from the client but am not able to show it in the edit box whose ID is IDC_EDIT_RECEIVE .

    Any help on this, I will be greatfull to you..

    Thanks in advance!!!!!!!!!!!!!

  10. #10
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: finding problem in recieving message via socket

    Quote Originally Posted by anup007star View Post
    Thanks for your advice I will take care of that. Now coming back to my problem....
    The faulty code is your problem. Did you fix it? Or remove it?
    When I am sending the message from a dialog box which is supposed to be my client i have used the following code snippet, and this is working fine as by applying breakpoints(during debugging) on my server code I was able to get that message from the clinet
    If it's "working fine", then it seems to be a pure coincidence. The code you show below also seems faulty, since there is a Unicode/ASCII mismatch:

    Code:
       hEd= GetDlgItem(hModelssDialog1,IDC_EDIT_SEND);
       ZeroMemory(szSendBuffer,sizeof(szSendBuffer));
       GetWindowText(hEd,(LPWSTR)&szSendBuffer[0],sizeof(szSendBuffer);  // assumes Unicode
       send(ClientSocket,szSendBuffer,strlen(szSendBuffer),0);  // assumes ASCII, and hence will give the incorrect length to send
    Let me tell you something about my server. I have a dialogbox in which I have a modal porperty sheet, my property sheet is the server. Now first of all I am not getting the handle of the pages for the property sheet that I require to put on SetDlgItemText so I have used hPropPage to get the active window but its not working at all. Actually I have a button called receive which has a function OnReceive() in which I have implemented the above code of my server. SO I was thinking that as soon as I will click the receive button then it will be a active window and my hPropPage will work.....But I was wrong.... there is something I am not able to do.Although I am able to recieve t hat message from the client but am not able to show it in the edit box whose ID is IDC_EDIT_RECEIVE .

    Any help on this, I will be greatfull to you..

    Thanks in advance!!!!!!!!!!!!!
    This is an entirely different problem. Design a simpler UI and get your client and server to work correctly with the simpler UI before tackling a more complex one. Then post your problem in a more appropriate forum, such as the Windows forum

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured