HomeModule 3 (Networks)Connect
I ntroduce
C onnect
A pply
R eflect
E xtend

Connect - Networks

The Network Module depends on code found in the compressed tar file networks.tar. Download and decompress that file to access the directories and files described in this module.

In addition, each separate Client-Server subsection (C-Client/C-Server, Labview-Client/C-Server, and Labview-Client/Labview-Server) will provide a compressed tar file pertinent to that subsection.

Before we start with the higher level concept of the client-server networking model, it makes sense to define and get a sense of the meaning of the terms we'll use in describing client-server networks.

  • An Application Programming Interface (API) is a well-defined collection of software functions and structures that are made available to a programmer in order to pass information to and from a program. This collection of code is often referred to as a library and it facilitates software development by collecting a group of commonly used and related functionality together into a reusable form. Use of libraries and APIs free a programmer from the tedium of redeveloping common code functionality.
  • A socket is an example of an API for facilitating computer network communications.  Sockets operate with four basic steps:
    1. create a socket
    2. bind to the socket,
    3. listen to a socket, and
    4. accept incoming data from a socket.
      A "telephone" metaphor can be used to help describe each step, in the following way:
      • Create the Socket:  This function creates an endpoint for communication. It is as if we were connecting a telephone up to an incoming line.
      • Bind: This function associates a port number with the socket. This is analogous to the phone company assigning a phone number to the phone line.
      • Listen: The listen function is what actually allows a connection to be attempted. This is like the phone company actually throwing the switch that makes the phone line live.
      • Accept: This function does exactly as its name implies. Now that the phone is ready and waiting, the call can be answered.
  • In proper parlance, a program is the series of instructions that are stored (either in memory or on the hard drive) while a process is the actual instance of that program when runs.  Note that one program can have many processes, but each process will only be running one program at a time.
  • The client/server model is a functional model for process management that is very common in computer programming. In this model, there is a single server process whose function is to provide a defined service. Typically a server is structured so that it runs indefinitely on a certain machine (as a daemon), waiting for requests for that service from clients. The clients are other processes, often on other machines, which communicate with the server, generally, through a socket, and request either functionality. The client/server model is very powerful because it allows for a single process (the server) to perform a specialized action on demand over and over again.

Client-Server Models

We will be focusing on the client-server networking model for this course, and we will present a separate Connect subsection to develop an understanding for how each functions. Click on the model type below to go that subsection:

C-Server/C-Client
3 examples demonstrate this type of client-server model
LabView-Client/C-Server
This Client/Server model also provide 3 examples
Labview-Client/Labview-Server
3 more examples provide details about this type of Client/Server model.

C-Server/C-Client Programs
Let's start off with three examples using c programs for the servers and c programs for the clients. Each example uses the same format to create the connections between the server and the client. Starting with the server in my_server.c, the first thing done is to check the arguments passed to make sure that the server was initiated correctly. The following test is made to make sure the correct number of arguments were given and if not, the proper usage is printed out and the program is exited.

if (argc != 3) {
---printf("Usage: my_server -p # \n");
---printf(" where -p # is port number\n");
---exit(0);
}

Next the number given at the command line after -p is assigned to port. Each server then makes the following function call:

descriptor = create_socket(port);
if(descriptor == 0) {
---printf("failed to make a socket connection for port &d \n", port);
---exit(0);
}

After some preliminary steps, this function does all four steps that were described above about sockets. It creates the socket, binds to the socket, listens and accepts. Go through create_socket.c to get a better look at how this is done. Notice that accept() gives socket_descriptor an int which is then returned to my_server.c and assigned to descriptor. Descriptor is then used to send or recieve data.

The client, my_client.c does similar things to make a connection with my_server.c. It also checks to make sure the correct number of arguments were made at the command line, in this case 5. Then it takes the number after -p and assigns it to port, and the string after -m and assigns it to machine. Then the following function call is made.

descriptor = make_connection( port, machine );
if(!descriptor) {
---printf("connect failure\n");
---exit(0);
}

The function make_connection.c after its preliminary steps only has two steps. It creates a socket and then issues the connect() command which specifies the peer with which the socket is to be associated. In this case that peer is the socket created by the server. make_connection.c also returns a descriptor to my_client.c which is also used to send or recieve data.

If I wanted to send data from the client to the server, I would issue the write() call in the client and a corresponding read() call in the server. For example, if I had a float called send_value I would send it in the following manner.

write(descriptor, (char *) &send_value, sizeof(float));

Then on the server side I would issue a similar call, in this case I am assigning the recieved float to recieve_value and would issue the call as shown.

read(descriptor, (char *) &recieve_value, sizeof(float));

It is important to make sure that the data type being sent and recieved is the same. It is also important to note that the information is converted into a char. This is done because TCP/IP sends its information in a byte stream. Also both read() and write() return the number of bytes sent or recieved. So if you wanted to record the value of the number of bytes sent or recieved you could issue the call in this manner.

i = write(descriptor, (char *) &send_value, sizeof(float));

This will assign the number of bytes sent into i.

In the first example under the directory Networks/Cs_Cc/Cs_Cc1/ an example is given on passing single numbers between the client and the server. The client sends two numbers to the server. The server then returns the product of those numbers to the client. This is done within a while loop so that the process can be done more than once, if desired.

Try going to the Cs_Cc1/ directory and typing make all. This will compile both my_server and my_client. Then try running them.

In the second example in the directory Cs_Cc2/ an example is used that transfers structures. In this case the client sends a structure called properties to the server which contains values for the cross-sectional area, young's modulus, original length and the magnitude of the force. The server then computes and returns the new length to the client. Notice that the read and write have sizeof(struct properties) in them as follows.

read(descriptor, (char *) p, sizeof(struct properties));

Finally in the directory Cs_Cc3/ is an example that transfers an array of structures. Upon reading the values from deformation.dat, the client then sends those values, 5 struct properties to the server where each new length for the 5 struct properties is computed and returned to the client. Notice that now the read and write now have n*sizeof(struct properties) in them.

read(descriptor, (char *) p, n*sizeof(struct properties));

It is probably a good idea to go through each example a few times to get used to the methods used and the terminology used with each method.

Click here to move on to the next Connect section (Labview-Client/C-Server).