SHARE
TWEET

Untitled

a guest Jun 25th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## server.c
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/socket.h>
  5. #include <arpa/inet.h>
  6. #include <netinet/in.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. int main(int argc, char const *argv[]) {
  11.     if (argc > 2)
  12.         return 128;
  13.    
  14.     int serv_sock, connect_sock, port = 13172;
  15.     char *msg = "Hello from server process.";
  16.     struct sockaddr_in address;
  17.     char buf[1024] = { 0 };
  18.     if (argc == 2)
  19.         port = atoi(argv[1]);
  20.    
  21.     if ((serv_sock = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
  22.         perror("socket");
  23.         return 1;
  24.     }
  25.    
  26.     address.sin_family = AF_INET;
  27.     address.sin_addr.s_addr = INADDR_ANY;
  28.     address.sin_port = htons(port);
  29.    
  30.     if (bind(serv_sock, (struct sockaddr*) &address, sizeof(address)) < 0) {
  31.         perror("bind");
  32.         return 1;
  33.     }
  34.    
  35.     if (listen(serv_sock, 1) < 0) {
  36.         perror("listen");
  37.         return 1;
  38.     }
  39.    
  40.     struct sockaddr_in peer;
  41.     int size = sizeof(struct sockaddr_in);
  42.     if ((connect_sock = accept(serv_sock, (struct sockaddr *) &peer, (socklen_t*) &size) < 0)) {
  43.         perror("accept");
  44.         return 1;
  45.     }
  46.    
  47.     printf("Read %d byte\n", read(connect_sock, buf, 1024));
  48.     fflush(stdout);
  49.     printf("Message: %s\n", buf);
  50.     send(connect_sock, msg, strlen(msg), 0);
  51.     shutdown(connect_sock, 0);
  52.     shutdown(serv_sock, 0);
  53.    
  54.     return 0;
  55. }
  56.  
  57. ## client
  58. #include <stdio.h>
  59. #include <sys/socket.h>
  60. #include <arpa/inet.h>
  61. #include <netinet/in.h>
  62. #include <unistd.h>
  63. #include <string.h>
  64. #include <stdlib.h>
  65.  
  66. int main(int argc, char const *argv[]) {
  67.     if (argc > 2)
  68.         return 128;
  69.    
  70.     int sock, port = 13172;
  71.     struct sockaddr_in server;
  72.     char *msg = "Hello from client process.";
  73.     char buf[1024] = { 0 };
  74.     if (argc == 2)
  75.         port = atoi(argv[1]);
  76.    
  77.     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  78.         perror("socket");
  79.         return 1;
  80.     }
  81.    
  82.     server.sin_family = AF_INET;
  83.     server.sin_port = htons(port);
  84.     if ((inet_pton(AF_INET, "127.0.0.1", &server.sin_addr)) <= 0)
  85.         return 1;
  86.    
  87.     if ((connect(sock, (struct sockaddr *) &server, sizeof(server))) < 0) {
  88.         perror("connect");
  89.         return 1;
  90.     }
  91.    
  92.     printf("Sent %d byte\n", send(sock, msg, strlen(msg), 0));
  93.     fflush(stdout);
  94.     int n = read(sock, buf, 1024);
  95.     printf("Message: %s", buf);
  96.     shutdown(sock, 0);
  97.    
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top