CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2013
    Posts
    30

    Winsock Math Operations client \ server

    I have an assignment that takes existing client and server code, that is fully able to make a TCP connection with arguments and share data and add to it.

    We're asked to add some code to it so that the following happens:
    1. Client asks for two floating point numbers
    2. Client asks for which Math operation to use (+,-,/,*)..

    Then sends those variables to the server for processing.
    The server in turn, needs to send back the result to the client.

    I've got the client prompting the user for the numbers and operator and storing the input in 3 variables, but from there, I'm lost. While the remaining code continues on to make a connection with the server, I've no idea how to actually tell the server to execute the operation.

    Can anyone point out a code example of this type of thing that I might be able to study?

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Winsock Math Operations client \ server

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Feb 2013
    Posts
    30

    Re: Winsock Math Operations client \ server

    Thanks.
    While that has all of the nuts and bolts to establish the connection, I'm really after seeing an example that actually passes a function back and forth.
    Again, I'm trying to collect input from the client side, pass it on to the server for processing and have the result be given back to the client.

    Any help there would be greatly appreciated.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Winsock Math Operations client \ server

    One of your previous excercises (client/server that had the server return the time to the client) contains all that you need to establish the connection - the existing client/server code. What you need to do is to change what is passed between the client/server to enable you to undertake what is required. In your previous code on the client side

    Code:
    sprintf(szBuf, "From the Client [%s]", szSvr);
    nRet = sendto(theSocket,		     // Socket
    		  szBuf,			// Data buffer
    		  (int)strlen(szBuf),		// Length of data
    		  0,				// Flags
    		  (LPSOCKADDR)&saServer,	// Server address
    		  sizeof(struct sockaddr));    // Length of address
    So instead of passing the message 'From the client...' you pass a string containing what you want your server to do eg. "+,1.23,3.45". In your server code

    Code:
    nRet = recvfrom(theSocket,		 // Bound socket
             	szBuf,			 // Receive buffer
    		sizeof(szBuf),		 // Size of buffer in bytes
    		0,			// Flags
    		(LPSOCKADDR)&saClient,	// Buffer to receive client address 
    		&nLen);			// Length of client address buffer
    szbuf would now contain "+,1.23,3.45". The server code would parse this, calculate the result and then pass it back to the client the same way as it passed back the time. The client would then display that result instead of displaying the time. Easy!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Feb 2013
    Posts
    30

    Re: Winsock Math Operations client \ server

    As always 2kaud, Thanks in advance for your insight. It's greatly valued.

    As you referenced, I am using the very same code as the last exercise, which accepts command arguments and establishes a connection.
    So on the client end, I took a crack at adding some code to prompt the end-user.
    I'm able to retrieve all the info from the user and stuff it in 3 variables from the client side, see code:
    HTML Code:
    float f1;
    	float f2;
    	char mFunc;
    	
    	// Prompt user for two floating point numbers and what math operation.
    	cout << "Enter the first decimal number." << endl;
    	cin >> f1;
    	cout << "Enter the second decimal number." << endl;
    	cin >> f2;
    	cout << "Which mathmatical operation would you like to use? (+, -, *, /)" << endl;
    	cin >> mFunc;
    	cout << "You've entered " << f1 <<" " << mFunc << " "<< f2 << " to be calculated. Sending to server for processing, One moment please." << endl;
    but if I'm reading you correctly, I'd need to send that in a string to the server in this portion of code:
    HTML Code:
    sprintf(szBuf, "From the Client [%s]", szSvr);
    
    	nRet = send(theSocket,					
    				  szBuf,				
    				  (int)strlen(szBuf),		
    				  0);
    Have I missed the boat completely, or am I on the right track?

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Winsock Math Operations client \ server

    Yes, you're on the right track. Just change the sprintf statement in the client.

    Code:
    sprintf(szBuf, "%c,%f,%f", mFunc,f1,f2);
    In the server following the recvfrom statement you will need to parse szBuf to extract the operation and the two numbers (each separated by comma). Then perform the operation and finally send the result as a string back to the client (instead of sending the time).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Feb 2013
    Posts
    30

    Re: Winsock Math Operations client \ server

    Okay, so doing so passes the variables to the server and the server acknowledges what was sent.

    Here's where this really gets tricky for me.
    In an effort to fully understand the operation side of it (to actually "do the math"), I created a full program that just handles the cin \ cout and function of doing the math operation.
    The program works perfectly, but obviously is not a client \ server program. However, the function that I created to do so should work in the actual project.

    The function called mathOp and I added it to the server side
    Here it is:
    HTML Code:
    int mathOp()
    {
    	
    		if (mFunc == '+')
    		{
    			 answer = f1 + f2;
    		}
    	    if (mFunc == '-')
    		{
    			 answer = f1 - f2;
    		}
    		if (mFunc == '*')
    		{
    			 answer = f1 * f2;
    		}
    		if (mFunc == '/')
    		{
    			 answer = f1 / f2;
    		}
    	    cout << "The solution to your math query is: " << answer << endl;
    		
    
    return 0;
    }
    I declare it as a function prototype at the head of the program and call it out at the same place that my teacher's old time statement was.
    The timestamp code:
    HTML Code:
    GetTime(szBuf, szSvr);
    My call:
    HTML Code:
    mathOp();
    Note that he puts the szBuf and szSvr in as arguments. When I try to do it with my mathOp, it tells me that the function can't hold arguments.
    With the additions that I've made, the server does compile and load, but I know that I have NOT parsed the szBuf. I've searched all night trying to find how to do it, but am having no luck. Can you elaborate on "parsing." I know it means taking "parts" of the string and putting them on their own lines (or other). I don't know what code makes that happen though.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Winsock Math Operations client \ server

    If you don't have a sufficient grasp of c++ basics to be able to code a simple function that takes arguments, then I suggest you look at

    http://www.learncpp.com/cpp-tutorial...and-arguments/

    One of the ways to extract numbers as a double from a string is to use strtod(..). So if

    Code:
    sprintf(szBuf, "%c,%f,%f", mFunc, f1, f2);
    is used to create a string, then

    Code:
    double	f1,
                    f2;
    
    char	*s = &szBuf[2],
    	*e,
    	mFunc;
    
    	mfunc = *szBuf;
    	f1 = strtod(s, &e);
    	f2 = strtod(e + 1, &s);
    can be used to parse it back into operator and the two numbers. These can then be passed as parameters to your mathop function (which should return a float as your f1, f2 numbers are of type float).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Feb 2013
    Posts
    30

    Re: Winsock Math Operations client \ server

    You're correct. I haven't got enough knowledge yet of the basics to have performed this yet.
    Been looking all night

    This link is a great help. Thanks again.

  10. #10
    Join Date
    Feb 2013
    Posts
    30

    Re: Winsock Math Operations client \ server

    While the link you provided helps with building functions with arguments, or parameters, my real problem is putting it all in context to the stuff that's already there.

    I see and appreciate what you've done here, taking the string that entered the server and parsing it with this code:
    HTML Code:
    double	f1,
                    f2;
    
    char	*s = &szBuf[2],
    	*e,
    	mFunc;
    
    	mfunc = *szBuf;
    	f1 = strtod(s, &e);
    	f2 = strtod(e + 1, &s);
    However, how is it I incorporate these variables into my mathOp function to actually perform the calculation?
    HTML Code:
    double mathOp()
    {
    
    			if (mFunc == '+')
    			{
    				 answer = f1 + f2;
    			}
    			if (mFunc == '-')
    			{
    				 answer = f1 - f2;
    			}
    			if (mFunc == '*')
    			{
    				 answer = f1 * f2;
    			}
    			if (mFunc == '/')
    			{
    				 answer = f1 / f2;
    			}
    		
    
    	return 0;
    }

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Winsock Math Operations client \ server

    Quote Originally Posted by tmcfadden View Post
    However, how is it I incorporate these variables into my mathOp function to actually perform the calculation?
    Your approach to this problem is backwards, to be honest with you. You should start from the "inside-out", not from the "outside-in".

    The first thing you should have done is write a function that takes two double arguments, an operation, and returns the answer. It doesn't matter where the arguments came from, whether it be from a server, the keyboard, an input file, or hard-coded values from a sample main() program.
    Code:
    #include <iostream>
    
    double mathOp(double arg1, double arg2, char functionToDo)
    {
        switch (functionToDo)
        {
            case '*':
                return arg1 * arg2;
        }
        return 0;  
    //...
    }
    
    int main()
    {
       std::cout << "The answer to 3 * 6 is " << mathOp(3, 6, '*');
    }
    The function above doesn't care where the numbers or operation came from. Then you test the daylights out of that function, replacing 3, 6, and the '*' with other numbers, operations, etc. to make sure it works correctly.

    Once you have a simple main() program working, then and only then do you now consider "client / server", parsing input, etc. The function is in place, and now it's just a matter of taking the data from wherever it comes from, and supplying the data to the function.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 15th, 2013 at 07:11 AM.

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