6 #include "pdu.h" /* defines the test string (TESTSTRING)
7 * and Service ID (DEFAULT_SID) to use.
10 char* get_image(int* len) {
11 const char* IMG_PATH= "img/%02d.jpg";
12 const int IMG_CNT = 3;
13 static int img_idx = 0;
21 sprintf(path,IMG_PATH,img_idx);
22 printf("Sending image %s\n",path);
24 img_file = fopen(path,"r");
26 printf("get_image: ERROR opening image\n");
30 fseek(img_file,0,SEEK_END);
31 img_size = ftell(img_file);
32 fseek(img_file,0,SEEK_SET);
33 printf("Image size %d\n",img_size);
35 img_buf = malloc(img_size);
36 rl=fread(img_buf,1,img_size,img_file);
39 printf("get_image: ERROR reading image\n");
43 img_idx = (img_idx+1) % IMG_CNT;
45 if(len) *len = img_size;
50 int main(int argc, char* argv[])
52 /* Service socket to use for connecting to the service node. */
56 /* The first parameter specifies the service to connect to (SID).
57 * The other parameters are reserved for legacy purposes.
59 sockid = n_connect(DEFAULT_SID, NULL, NULL);
61 /* sockid will be the number of the socket we got (>0), if successful,
62 * or <0 if something went wrong. Let's check:
68 printf("Connected socket %d to service '%d'.\n", sockid, DEFAULT_SID);
76 img = get_image(&img_len);
78 smsg = pack_pdu(PUT_IMAGE, (uns8*)img, img_len, &len);
81 /* Send the through the socket specified by sockid. The last parameter,
82 * HSSendBlocking, means we will
83 * use blocking mode, i.e. the call to Hsend will only return once all
84 * data has been sent (or an error is detected).
86 err = n_send(sockid, smsg, len, HSSendBlocking);
92 /* If no error condition was raised, Hsend will return the
93 * number of bytes sent. Let's check this against what we
94 * intended to send, just to be sure (with a blocking call, the
95 * return value should always be equal to the "data_len" parameter
99 printf("Send successful.\n");
101 printf("Sent %d out of %d bytes.\n", err, len);
103 printf("Would you like to disconnect (\"y\" for yes)?\n");
106 // We're done; tell the server to close the connection.
108 smsg = pack_pdu (DISCONNECT,NULL, 0, &len);
109 n_send(sockid, smsg, len, HSSendBlocking);
120 n_disconnect(sockid);