Note

advertisement
Everything you need to reference to do the assignment is:
smsa_server.c and the slides for networking
2 steps to this assignment:
1) Write code to connect client to server – this is in the slides
2) Write code to send messages. – this is in smsa_server.c (client is a little different
because you will need to move the bytes around for the packets)
You will need to make calls to the driver.
When running this you will need to run it in two windows. In first window run your client code.
In second window run the server. Use the -v the first time to figure out what is going on.
TIP: First write the code to connect to the server from the client and run it and debug it to see if
this works. Don't do all the code first and try to debug later because it will be complicated. Once
you get the connect working you can then write the code to send the messages.
Every time you call smsa_operation in your driver replace it with the interface
(smsa_client_operation). The arguments are the same. All you have to do in your driver
is replace smsa_operation with smsa_client_operation.
In smsa_client_operation:
FIRST check to see if you are connected. Do this in the clientConnect() function (described
below). If not connected then stop and don't continue in this function as there is no point if you
are not connected.
Suggestion: You can use a global variable called
int server_socket.
Set it to -1 to start. This is outside all function
declarations. Later
int clientConnect() will be called and it will
1)
setup a connection address (need an ipAddress and a port.
= smsa_default_id
and
ipAddress
port = smsa_default_port
(This is all defined in smsa_network.h and is in the spec).
2)
Next you need to create asocket. call socket()
here ----> server_socket = socket();
So check if socket is 0 for success
3) Next connect();
Suggestion: use printf log message function with the info level and "-v" to check
4) Finally return 0 if everything was successful and -1 if not.
// approximately 15-20 lines of code. Always check the return values.
At the very beginning of smsa_client_operation do this:
if (opcode == mount)
{
call function called clientConnect()
// you have to //create this
function
}
else if (opcode == unmount)
{
call function clientDisconnect()
// you have //to also create this
function
}
// In clientDisconnect() you need to
//close(server_socket); and return(0);
Now that you have checked if you are connected, these are the main steps for the
smsa_client_operation function.
1. Take opcode you are given and figure out what you are sending.
2. If opcode says to read then when you send the packet you send it without a buffer. Otherwise
send it with a buffer.
if(write)
{
send with buffer
}
else
{
do not send with buffer;
}
Note: some of the OPCODEs: mount, unmount, read, seek. Two different kinds of
"sends".
-If you want to send with a buffer - need a buf variable of size 256 + net header size. (Header has
4 values len op rd. Net header size is all of those sizes added together)
Convert these 4 values from host order to network order
len = htons(Net header len) // (2 bytes - short).
//length must be set to according to the spec
op = htonl(op); // long
ret = htons(0); // sending this so just set to 0
After you set up your packet then you send it
3. Write(buf, len);
if writing with a buffer then the length is longer and the pointer would be the same but
the buffer length would be different. Need a local variable length that you set up to be
header size if sending with buffer else header size + 256 if not sending with buffer.
4 After you write you want to receive something from the server so
if you are reading
{
Read(buf, header + 256);
}
else if you are writing
{
Read(buf, header);
}
This will wait make your network program sit and wait for the packet to come back.
5) Check if smsa_operation was a success. If retval is nonzero then fail, so check that it is 0. Also
check that opcode equals the same as the value of the opcode you sent. Confirm that length is
correct (if read len = header size+256; else if write then len =header size).
Assume we are not in a read (aka we did a mount or seek or something) -> you want to return
what return returned (aka nonzero).
Assume we are in a read -> copy the packet into the buffer that was handed into the function.
Then Return return.
^^^^^^^^above are all the steps for smsa_client_operation ^^^^^^^
Should be less than 100 lines. "If more complicated than these steps then you're doing it wrong."
This does not address any of the connections (clientConnect() addresses the connection – you did
this at the beginning of the function). This code assumes you already have a socket (again,
created at the beginning with the server_socket variable).
** NOTE: Check at the very top of this function whether or not you're connected. Otherwise
there is no point to your code. (We did this with clientConnect() ) **
You have to connect and mount. So in mount we are going to call a function to connect the client
to the server. (Did this in the beginning of function).
*Disclaimer: Could be errors in these notes due to the pace at which the review session
went. Let me know if something is wrong or you need clarification. –Kelly Hutchison*
Download