IBR-DTNSuite  0.8
tools/src/c_api_examples/dtnping_sync.c
Go to the documentation of this file.
00001 /* DTNPing C API synchronous ersion */
00002 
00003 
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <unistd.h>
00008 #include <ibrdtn/api/dtn_api.h>
00009 
00011 DTN_EP dep=-1;
00012 
00014 int16_t waitfor=0;
00015 
00016 
00018 void print_usage(char *me) {
00019     printf("Usage: %s dst-eid message\n",me);
00020     exit(-1);
00021 }
00022 
00023 
00024 
00026 int main(int argc, char *argv[])
00027 {
00028 
00029     char buf[128];
00030     int count;
00031     int i;
00032 
00033     if (argc < 3)
00034         print_usage(argv[0]);
00035 
00036     //No receive callback=synchronous mode
00037     dep=dtn_register_endpoint("test",NULL, NULL); //sync mode
00038 
00039     if (dep < 0) {
00040         puts("Problems registering endpoint. Terminating");
00041         return -2;
00042     }
00043 
00044     printf("Sending %s\n", argv[2]);
00045     waitfor=strlen(argv[2]+1);
00046 
00047     dtn_endpoint_set_option(dep,DTN_OPTION_DSTEID,argv[1]);
00048     //Uncomment to test fragmentation: force fragment using small buffer
00049     //dtn_endpoint_set_option(dep,DTN_OPTION_TXCHUNKSIZE,5);
00050 
00051     dtn_write(dep, argv[2],strlen(argv[2])+1);
00052     dtn_flush(dep);
00053 
00054     while (waitfor > 0) {
00055             count=dtn_read(dep,buf,128);
00056             if (count < 0) {
00057                 puts("Error reading from endpoint");
00058                 waitfor=0;
00059                 continue;
00060             }
00061             printf("Read %i bytes: ", count);
00062             for (i=0; i< count; i++)
00063                 putchar( buf[i] );
00064             puts("");
00065             waitfor-=count;
00066     }
00067 
00068     return 0;
00069 }