Client/Server Socket Receive Problem

This is a discussion on Client/Server Socket Receive Problem within the Networking/Device Communication forums, part of the General Programming Boards category; Im trying to send an array from the server application to the client on the same machine. The array is ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    Question Client/Server Socket Receive Problem

    Im trying to send an array from the server application to the client on the same machine. The array is loaded and Sent from the server and recieved at the client but the array seems to be empty on the client side. I am using the socket.Receive function on the client side. I can send string messages back and forth without any problems. I am using Visual C++ MFC IDE tool to generate program.

    Code:
    //This is the server side declaration and Socket Send. I know that on the array has data in it on the server side
    
    struct Countries
    {
    	CString m_strCountries;
    	double m_lOffset;
    	CString m_strSummer;
    };
    Countries Acountries[CLEN];
    
    iSent = m_sConnectSocket.Send(Acountries,NULL,NULL);
    
    //This is the client side declaration an Socket Receive
    
    struct Countries
    {
    	CString m_strCountries;
    	double m_lOffset;
    	CString m_strSummer;
    };
    
    Countries Acountries[CLEN];
    
    iRcvd = m_sConnectSocket.Receive(Acountries,NULL,NULL);
    
    
    //Receive the message ** This is what I use to get the string
    // messages that work correctly for both client and server
    
    char *pBuf = new char[1025];
    	int iBufSize = 1024;
    
    	iRcvd = m_sConnectSocket.Receive(pBuf, iBufSize);

    Any and all comments greatly appreciated.

    MER
    Last edited by mariabair; 12-23-2003 at 10:35 AM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Post the code that sends and receives data.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    Added the code

    Thank you for you interest in my problem. I have edited the original message, adding the code. I hope this will clarify the problem.

    Thanks again,
    MER

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > iSent = m_sConnectSocket.Send(Acountries,NULL,NULL);
    How does this know how much to send?
    Check the return value in iSent to make sure it is what you expect.

    > CString m_strCountries;
    If this contains any hidden pointers, the data which they point to will NOT be copied across the link for you. All you will see at the receiver is an invalid pointer.
    Since you can't know from one implementation to the next whether this class contains pointers or not, it's best to extract each string and send that separately.

    > iRcvd = m_sConnectSocket.Receive(pBuf, iBufSize);
    Check the value in iRcvd.
    There are a couple of points to note - the most important one being that the network can fragment a message, so you may receive "hello world" in two or more separate messages, eg "he" followed by "llo world"

    > I can send string messages back and forth without any problems
    Then I suggest you do things like
    m_sConnectSocket.Send(Acountries[0].m_strCountries.c_str(),NULL,NULL);
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    Unhappy Got to be a better way

    Salem
    First, I am greatly appreciative for your solution. It will get the information over to the client.

    The problem now is that I am trying to send database information (lots of it, too) to the client. The Countries structure is one database record. Currently I have only 29 reocrds but this database is actually loaded with hundreds of records.

    Can you suggest a better way for me to send this database information to the client?

    MER

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > Can you suggest a better way for me to send this database information to the client?
    So what exactly is wrong with a for loop?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    one suggestion is to place all shared sturctures into a single *.h file and then #include this file in both programs(client/server).
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Datagram Unix socket problem
    By karas in forum C Programming
    Replies: 9
    Last Post: 07-27-2006, 10:08 AM
  2. RE: client/server shared memory problem
    By hampycalc in forum C Programming
    Replies: 0
    Last Post: 03-10-2006, 01:26 PM
  3. sock receive fdset problem
    By yahn in forum Networking/Device Communication
    Replies: 6
    Last Post: 03-03-2006, 04:34 PM
  4. Please Help! Socket Problem
    By robsmith in forum C Programming
    Replies: 2
    Last Post: 05-06-2005, 12:35 PM
  5. Socket programming problem.
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-14-2002, 08:44 PM

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