Advertisement
Agus_Darmawan

cli1.c

Mar 30th, 2021
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. /*
  2.  * cli1.c -     For ICT374 Topic 8
  3.  *              Hong Xie
  4.  *              Last modified: 16/10/2020
  5.  *      test for UNIX domain stream socket (client part).
  6.  *      This is a very **crude** program.
  7.  */
  8.  
  9. #include  <unistd.h>
  10. #include  <stdlib.h>
  11. #include  <sys/types.h>
  12. #include  <sys/socket.h>
  13. #include  <sys/un.h>
  14. #include  <string.h>
  15. #include  <stdio.h>
  16. #include  <string.h>
  17.  
  18. char serversockname[]="serversocket";
  19.  
  20. int main()
  21. {
  22.      int sd, n;
  23.      char buf[256];
  24.      struct sockaddr_un server_addr;
  25.      char mesg[]="hello, world from client";
  26.  
  27.      /* create client socket */
  28.      sd = socket(PF_UNIX, SOCK_STREAM, 0);
  29.  
  30.      /* request connection to serversoc */
  31.      server_addr.sun_family = AF_UNIX;
  32.      strcpy(server_addr.sun_path, serversockname);
  33.      connect(sd, (struct sockaddr *)&server_addr, sizeof(server_addr));
  34.  
  35.      /* send request to server */
  36.      write(sd, mesg, strlen(mesg)+1);
  37.      
  38.      /* read results back from server */
  39.      read(sd, buf, sizeof(buf));
  40.      printf("server echoed '%s'\n", buf);
  41.  
  42.      exit(0);
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement