If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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?

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.

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

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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
C, C++ Compiler: Microsoft VS2015

Re: Winsock Math Operations client \ server

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:

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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
C, C++ Compiler: Microsoft VS2015

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:

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.

Re: Winsock Math Operations client \ server

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.

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.