next up previous contents
Next: 6.2 Server Up: 6 Application Layer Sample Previous: 6 Application Layer Sample

6.1 Simple Stream-oriented Client/Server

For much of the initial testing of XUDP, I used a client/server pair that simply acted as receiver and sender of a parcelized stream of data. One hundred, 10240 byte parcels were transmitted one at a time with full reliability in this example.

6.1.1 Client

The source code for the sending side client is described below.

 1 /*
 2  * Testing platform for XUDP
 3  *
 4  */
 5 #include <stdio.h>
 6 #include <stdlib.h>
 7 #include <unistd.h>
 8 #include "xudp.h"
 9 #include "xudp_socket.h"
10 
11 #define  DATAFILE "test.txt"
12 
13 void main(int argc, char *argv[])
14 {
15     struct XUDPHost *xudphost;
16     FILE            *file;
17     char            *buffer;
18     int             i;
19     
20     if (argc<2)
21     {
22      printf("Whoa! No hostname!\n");
23      exit(0);
24     }
25 
26     if ((buffer=malloc(10240))==0) exit(0);

Top of the stream-based sending side client.

The client requires that the hostname of a machine on which a XUDP server is running, is specified on the command line. Lines 20-24 check for the presence of this command line argument. Line 26 allocates enough memory to hold one parcel's worth of data.

Lines 27 and 28, below, instruct XUDP in the connection process and are purposefully similar to the Berkeley sockets API. xudphost is a struct XUDPHost * that is filled in upon invoking xudpSocket with file descriptors for the UNIX streams used in communications with the XUDP server. xudpSocket actually opens this connection with the server at this time, informing the server of the presence of this application. xudpConnect sends a message through the UNIX streams to the XUDP server, instructing it to attempt a connection with the hostname given by the string specified in argument 2 of the command. The final argument of xudpConnect is used to specify an alternate port number to use that the foreign XUDP server will be listening on, if set to zero, the default is selected.

The remaining lines below open a simple text file and load the parcel buffer with 10,240 bytes of data from the file.

27     xudphost=xudpSocket();
28     xudpConnect(xudphost, argv[1], 0);
29     printf("Client connected to %s", xudphost->pHostent->h_name);
30     fflush(stdout);
31     
32     file=fopen(DATAFILE, "r");
33     fread(buffer, 10240, 1, file);
34     i=0;

Initialization procedure in the sending side client.

Below is the main loop of the code for the client side that focuses on sending the 100 parcels. Most importanly, line 37 contains the xudpSend command, used to transmit a parcel through an existing connection. The format of xudpSend is as follows:

int xudpSend(struct XUDPHost *xudphost, char *buffer, u_short bytes, u_char type, u_long timeout);

xudpSend is intended to act as an extended version of send, the prototype of which is shown below:

int send(int s, const void *msg, int len, unsigned int flags);

xudphost is the host address structure, referred to before, acting in place of the socket file descriptor. buffer is a pointer to a character buffer and bytes is the length in bytes of the buffer. The type field is utilized as a means of future expansion, currently only VIDEO is defined.

Timed obsolescence is implemented with the last argument, timeout. timeout specifies the parcel's expiration time in milliseconds from complete receipt of the parcel at the local XUDP server. If this timeout expires or will expire before the next round trip time, and the parcel hasn't been completely acknowledged, the parcel is deleted from the queue because it can't possibly reach the destination in time to be useful. However, in the case of this parcel, RELIABLE has been specified, indicating to the XUDP server that it should implement full reliability in transporting this parcel.

35     while (i<100)
36     {
37        if (xudpSend(xudphost, buffer, 10240, VIDEO, RELIABLE)>0)
38        {
39            i++;
40            printf("sent %d\n", i*10240);
41            fflush(stdout);
42        }
43        else
44        {
45            printf(".");
46            fflush(stdout);
47        }
48     }
49  
50     sleep(30);
51     xudpClose(xudphost);
52 }

Send loop of the client.

At the end of the process, the connection is closed with xudpClose.


next up previous contents
Next: 6.2 Server Up: 6 Application Layer Sample Previous: 6 Application Layer Sample

Mike Andrews
Wed Mar 19 16:07:58 EST 1997