next up previous contents
Next: 7 Source Code Presentation Up: 6 Application Layer Sample Previous: 6.1 Simple Stream-oriented Client/Server

6.2 Server

The top of the server side is similar to the client:

 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 void main()
12 {
13     struct XUDPHost *xudphost;
14     char            *buffer;
15     FILE            *out;
16     u_short         length;
17     int             total=0;
18     char            type;
19     int             i;
20 
21     if ((buffer=malloc(10240))==0) exit(0);

Top of the stream-based receiving side server.

Again, the XUDP API follows a process very similar to the Berkeley Sockets API. Below, line 22 starts with xudpSocket forming a connection to the local XUDP server (this time on the application layer server-side machine.) Line 23, xudpBind instructs the XUDP server to bind the UDP port that it will use to receive incoming communication from the network to the specified port number (its zero, so XUDP uses the default port). xudpListen tells the XUDP server to begin listening for incoming connection requests on the previously bound UDP port. Line 25, xudpAccept blocks until a foreign XUDP server client forms a successful connection with us.

22     xudphost=xudpSocket();
23     xudpBind(xudphost, 0);
24     xudpListen(xudphost);
25     xudpAccept(xudphost);
26     printf("Server connected to %s\n", xudphost->pHostent->h_name);
27     fflush(stdout);
28     
29     out=fopen("parcel_recv.log","w");

Initialization of the stream-based receiving side server.

The receive loop of the server is markedly similar to that of the client. As was the case with xudpSend, xudpRecv is intended to act similarly to recv. The argument formatting for xudpRecv is listed below:

int xudpRecv(struct XUDPHost *xudphost, char *buffer, u_short *bytes, u_char *type);

Again, type is provided only as a means for future expansion.

30     for (i=0; i<100; i++)
31     {
32         xudpRecv(xudphost, buffer, &length, &type);
33         fwrite(buffer, length, 1, out);
34         total+=length;
35         printf("received %d\n", total);
36         fflush(stdout);
37     }
38     
39     xudpClose(xudphost);
40     fclose(out);
41     sleep(15);
42 }

Receive loop of the stream-based receiving side server.


next up previous contents
Next: 7 Source Code Presentation Up: 6 Application Layer Sample Previous: 6.1 Simple Stream-oriented Client/Server

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