Facebook
From P, 8 Months ago, written in C.
Embed
Download Paste or View Raw
Hits: 342
  1. #include <netinet/in.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/socket.h>
  6. #include <unistd.h>
  7. #define PORT 8080
  8.  
  9. int main()
  10. {
  11.     struct sockaddr_in serverAddr, clientAddr;
  12.     int serverAddrLen = sizeof(serverAddr);
  13.     int clientAddrLen = sizeof(clientAddr);
  14.  
  15.     // Create a socket
  16.     int server_fd = socket(AF_INET, SOCK_STREAM, 0);
  17.  
  18.     if (server_fd < 0)
  19.     {
  20.         perror("\nCouldn't create socket");
  21.         exit(EXIT_FAILURE);
  22.     }
  23.  
  24.     // Initialize the server address structure
  25.     serverAddr.sin_family = AF_INET;
  26.     serverAddr.sin_addr.s_addr = INADDR_ANY;
  27.     serverAddr.sin_port = htons(PORT);
  28.  
  29.     // Bind the socket to the server address
  30.     if (bind(server_fd, (struct sockaddr *)&serverAddr;, serverAddrLen) < 0)
  31.     {
  32.         perror("\nCouldn't bind");
  33.         exit(EXIT_FAILURE);
  34.     }
  35.  
  36.     // Start listening for incoming connections
  37.     if (listen(server_fd, 10) < 0)
  38.     {
  39.         perror("\nCouldn't listen");
  40.         exit(EXIT_FAILURE);
  41.     }
  42.  
  43.     // Accept a client connections
  44.     int comm_fd = accept(server_fd, (struct sockaddr *)&clientAddr;, &clientAddrLen;);
  45.  
  46.     if (comm_fd < 0)
  47.     {
  48.         perror("\nCouldn't accept");
  49.         exit(EXIT_FAILURE);
  50.     }
  51.  
  52.     char messageReceived[100];
  53.  
  54.     while (1)
  55.     {
  56.         bzero(messageReceived, 100);
  57.        
  58.         // Receive data from the client
  59.         recv(comm_fd, messageReceived, 100, 0);
  60.  
  61.         printf("\nMessage from client:\n%s", messageReceived);
  62.  
  63.         // Send the received data back to the client
  64.         // send(comm_fd, messageReceived, strlen(messageReceived), 0);
  65.         int numberOfWords = 0, numberOfAlphabets = 0;
  66.         for(int i = 0; messageReceived[i] != '\0'; i++)
  67.         {
  68.             if(messageReceived[i] == ' ')
  69.             {
  70.             numberOfWords++;
  71.             }
  72.             else if((messageReceived[i] >= 'A' && messageReceived[i] <= 'Z') || (messageReceived[i] >= 'a' && messageReceived[i] <= 'z')) {
  73.             numberOfAlphabets++;
  74.             }
  75.         }
  76.         numberOfWords++;
  77.         char buff[100];
  78.         sprintf(buff, "Number of alphabets: %d, Number of words: %d\n", numberOfAlphabets, numberOfWords);
  79.         send(comm_fd, buff, strlen(buff), 0);
  80.        
  81.         if(strncmp(messageReceived, "Exit", 4) == 0) {
  82.             printf("\nServer exiting...\n");
  83.             break;
  84.         }
  85.     }
  86.     // Close the client connection
  87.     close(comm_fd);
  88.     close(server_fd);
  89. }
  90.  
  91.  
  92.  
  93.  
  94. Client
  95.  
  96. #include <arpa/inet.h>
  97. #include <stdio.h>
  98. #include <stdlib.h>
  99. #include <string.h>
  100. #include <sys/socket.h>
  101. #include <unistd.h>
  102. #define PORT 8080
  103.  
  104. int main()
  105. {
  106.     struct sockaddr_in serverAddr;
  107.  
  108.     // Create a socket
  109.     int client_fd = socket(AF_INET, SOCK_STREAM, 0);
  110.  
  111.     if (client_fd < 0)
  112.     {
  113.         perror("\nCouldn't create socket");
  114.         exit(EXIT_FAILURE);
  115.     }
  116.  
  117.     // Initialize the server address structure
  118.     serverAddr.sin_family = AF_INET;
  119.     serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  120.     serverAddr.sin_port = htons(PORT);
  121.  
  122.     // Establish a connection to the server
  123.     if (connect(client_fd, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) < 0)
  124.     {
  125.         perror("\nCouldn't connect");
  126.         exit(EXIT_FAILURE);
  127.     }
  128.  
  129.     char messageSent[100];
  130.     char messageReceived[100];
  131.  
  132.     while (1)
  133.     {
  134.         bzero(messageSent, 100);
  135.         bzero(messageReceived, 100);
  136.  
  137.         // Read a line of text from the user
  138.         printf("\nEnter message:\n");
  139.         fgets(messageSent, 100, stdin);
  140.  
  141.         // Send the user's input to the server
  142.         send(client_fd, messageSent, sizeof(messageSent), 0);
  143.        
  144.         // Receive the response from the server
  145.         recv(client_fd, messageReceived, 100, 0);
  146.  
  147.         // Print the server's response
  148.         printf("\nMessage from server:\n%s", messageReceived);
  149.  
  150.         if (strncmp(messageSent, "Exit", 4) == 0)
  151.         {
  152.             printf("\nClient exiting...\n");
  153.             break;
  154.         }
  155.     }
  156.  
  157.     close(client_fd);
  158. }