Facebook
From Voluminous Motmot, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 85
  1. /*
  2.  Źródło: https://www.geeksforgeeks.org/tcp-server-client-implementation-in-c/
  3.  $ gcc serwer_0.c -o serwer_0
  4.  
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <netdb.h>
  9. #include <netinet/in.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/socket.h>
  13. #include <sys/types.h>
  14. #include <ctype.h>
  15.  
  16. // #include <fcntl.h>
  17. #include <unistd.h>
  18.  
  19. #define MAX 80
  20. #define PORT 8081
  21. #define SA struct sockaddr
  22.  
  23. // Function designed for chat between client and server.
  24. void func(int sockfd)
  25. {
  26.         char buff[MAX];
  27.         int n;
  28.         // infinite loop for chat
  29.         for (;;) {
  30.                 bzero(buff, MAX);
  31.  
  32.                 // read the message from client and copy it in buffer
  33.                 read(sockfd, buff, sizeof(buff));
  34.                
  35.                 // print buffer which contains the client contents
  36.                 int i = 0;
  37.    
  38.                 while(buff[i]) {
  39.                         buff[i] = toupper(buff[i]);
  40.                         i++;
  41.                 }
  42.  
  43.                 printf("From client: %s\t To client : ", buff);
  44.                 //bzero(buff, MAX);
  45.                 //n = 0;
  46.                 // copy server message in the buffer
  47.                 //while ((buff[n++] = getchar()) != '\n')
  48.                 //      ;
  49.  
  50.                 // and send that buffer to client
  51.                 write(sockfd, buff, sizeof(buff));
  52.  
  53.                 // if msg contains "Exit" then server exit and chat ended.
  54.                 if (strncmp("EXIT", buff, 4) == 0) {
  55.                         printf("Server Exit...\n");
  56.                         break;
  57.                 }
  58.         }
  59. }
  60.  
  61. // Driver function
  62. int main()
  63. {
  64.         int sockfd, connfd, len;
  65.         struct sockaddr_in servaddr, cli;
  66.  
  67.         // socket create and verification
  68.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  69.         if (sockfd == -1) {
  70.                 printf("socket creation failed...\n");
  71.                 exit(0);
  72.         }
  73.         else
  74.                 printf("Socket successfully created..\n");
  75.         bzero(&servaddr, sizeof(servaddr));
  76.  
  77.         // assign IP, PORT
  78.         servaddr.sin_family = AF_INET;
  79.         servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  80.         servaddr.sin_port = htons(PORT);
  81.  
  82.         // Binding newly created socket to given IP and verification
  83.         if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
  84.                 printf("socket bind failed...\n");
  85.                 exit(0);
  86.         }
  87.         else
  88.                 printf("Socket successfully binded..\n");
  89.  
  90.         // Now server is ready to listen and verification
  91.         if ((listen(sockfd, 5)) != 0) {
  92.                 printf("Listen failed...\n");
  93.                 exit(0);
  94.         }
  95.         else
  96.                 printf("Server listening..\n");
  97.         len = sizeof(cli);
  98.  
  99.         // Accept the data packet from client and verification
  100.         connfd = accept(sockfd, (SA*)&cli, &len);
  101.         if (connfd < 0) {
  102.                 printf("server acccept failed...\n");
  103.                 exit(0);
  104.         }
  105.         else
  106.                 printf("server acccept the client...\n");
  107.  
  108.         // Function for chatting between client and server
  109.         func(connfd);
  110.  
  111.         // After chatting close the socket
  112.         close(sockfd);
  113. }