Advertisement
SirNickolas

OS 5 client.c

Oct 31st, 2014
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/iomsg.h>
  5. #include <sys/neutrino.h>
  6. #include <unistd.h>
  7. #include "httpd_pid.h"
  8.  
  9. char input[65535], response[65535];
  10.  
  11. int main(void) {
  12.     FILE* f = fopen(HTTPD_PID, "r");
  13.     if (!f) {
  14.         fputs("Cannot open \"" HTTPD_PID "\"\n", stderr);
  15.         return 1;
  16.     }
  17.     int nodeDescriptor, serverId, channelId;
  18.     bool ok = fscanf(f, "%d/%d/%d", &nodeDescriptor, &serverId, &channelId) == 3;
  19.     fclose(f);
  20.     if (!ok) {
  21.         fputs("Invalid file format\n", stderr);
  22.         return 1;
  23.     }
  24.     int connectionId = ConnectAttach(nodeDescriptor, serverId, channelId, 0, 0);
  25.     if (connectionId == -1) {
  26.         fputs("Server is not found\n", stderr);
  27.         return 1;
  28.     }
  29.     while (fputs(">>> ", stdout) >= 0 && fgets(input, sizeof input / sizeof(char), stdin)) {
  30.         int len = strlen(input);
  31.         input[len - 1] = '\0';
  32.         if (!strcmp(input, "exit"))
  33.             break;
  34.         if (MsgSend(connectionId, input, len, response, sizeof response) == -1) {
  35.             fputs("Cannot send a message\n", stderr);
  36.             return 1;
  37.         }
  38.         puts(response);
  39.     }
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement