SHARE
TWEET

Untitled

a guest Dec 6th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <sys/socket.h>
  4. #include <stdlib.h>
  5. #include <netinet/in.h>
  6. #include <string.h>
  7. #define PORT 8080
  8. int main(int argc, char const *argv[])
  9. {
  10.     int server_fd, new_socket, valread,valread2,valread3,valread4;
  11.     struct sockaddr_in address;
  12.    char buffer[1024] = {0};
  13.     int opt = 1;
  14.     int addrlen = sizeof(address);
  15.     int my_int,my_int2,my_int3,my_int4,result1,result2;
  16.        
  17.     // Creating socket file descriptor
  18.     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
  19.     {
  20.         perror("socket failed");
  21.         exit(EXIT_FAILURE);
  22.     }
  23.        
  24.     // Forcefully attaching socket to the port 8080
  25.     if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt)))
  26.     {
  27.         perror("setsockopt");
  28.         exit(EXIT_FAILURE);
  29.     }
  30.     address.sin_family = AF_INET;
  31.     address.sin_addr.s_addr = INADDR_ANY;
  32.     address.sin_port = htons( PORT );
  33.        
  34.     // Forcefully attaching socket to the port 8080
  35.     if (bind(server_fd, (struct sockaddr *)&address,  
  36.                                  sizeof(address))<0)
  37.     {
  38.         perror("bind failed");
  39.         exit(EXIT_FAILURE);
  40.     }
  41.     if (listen(server_fd, 3) < 0)
  42.     {
  43.         perror("listen");
  44.         exit(EXIT_FAILURE);
  45.     }
  46.     if ((new_socket = accept(server_fd, (struct sockaddr *)&address,  
  47.                        (socklen_t*)&addrlen))<0)
  48.     {
  49.         perror("accept");
  50.         exit(EXIT_FAILURE);
  51.     }
  52.         valread = recv(new_socket, &my_int, sizeof(my_int), 0);
  53.    
  54.     valread2 = recv(new_socket, &my_int2, sizeof(my_int), 0);
  55.    
  56.     valread3 = recv(new_socket, &my_int3, sizeof(my_int), 0);
  57.    
  58.     valread4 = recv(new_socket, &my_int4, sizeof(my_int), 0);
  59.    
  60.    
  61.    
  62.     result1 = my_int+ my_int3;
  63.     result2 = my_int2+ my_int4;
  64.     send(new_socket, &result1, sizeof(result1), 0);
  65.  
  66.    
  67.     return 0;
  68. }
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