Advertisement
Guest User

server

a guest
Feb 13th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <stdlib.h>
  4. #include <netinet/in.h>
  5. #include <string.h>
  6. #include <pthread.h>
  7.  
  8. int valread;
  9. int PORTudp,PORTtcp;
  10. struct sockaddr_in address;
  11.  
  12. typedef struct _thread_data_t {
  13.   int tid;
  14. } thread_data_t;
  15.  
  16.  
  17. void *newInstanceTCP(void *vargp){
  18.       thread_data_t *data = (thread_data_t *)vargp;
  19.       int new_socket,server_fd;
  20.       char buffer[1024] = {0};
  21.       int opt = 1;      
  22.       int addrlen = sizeof(address);
  23.       char *hello = "Hello from server\n";
  24.           // Creating socket file descriptor
  25.       if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
  26.       perror("socket failed");
  27.       exit(EXIT_FAILURE);
  28.     }      
  29.         // Forcefully attaching socket to the port
  30.       if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
  31.              &opt, sizeof(opt)))
  32.     {
  33.       perror("setsockopt");
  34.       exit(EXIT_FAILURE);
  35.     }
  36.       address.sin_family = AF_INET;
  37.       address.sin_addr.s_addr = INADDR_ANY;
  38.       address.sin_port = htons( PORTtcp );
  39.      
  40.       // Forcefully attaching socket to the port
  41.       if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0){
  42.         perror("bind failed");
  43.         exit(EXIT_FAILURE);
  44.       }
  45.       if (listen(server_fd, 3) < 0){
  46.         perror("listen");
  47.         exit(EXIT_FAILURE);
  48.       }
  49.       if ((new_socket = accept(server_fd, (struct sockaddr *)&address,(socklen_t*)&addrlen))<0){
  50.         perror("accept");
  51.         exit(EXIT_FAILURE);
  52.       }
  53.       while(1){
  54.     valread = read(new_socket , buffer, 1024);
  55.     printf("%s\n",buffer );
  56.     memset(buffer,0,strlen(buffer));
  57.     send(new_socket , hello , strlen(hello) , 0 );
  58.     printf("Hello message sent. Thread ID:%d \n",data->tid);
  59.       }
  60. }
  61. void *newInstanceUDP(void *vargp){
  62.       thread_data_t *data = (thread_data_t *)vargp;
  63.       int new_socket,server_fd;
  64.       char buffer[1024] = {0};
  65.       int opt = 1;      
  66.       int addrlen = sizeof(address);
  67.       char *hello = "Hello from server";    
  68.       struct sockaddr_in clientaddr;
  69.       struct hostent *hostp;
  70.       char *hostaddrp;
  71.      
  72.           // Creating socket file descriptor
  73.       if ((server_fd = socket(AF_INET, SOCK_DGRAM, 0)) == 0){
  74.       perror("socket failed");
  75.       exit(EXIT_FAILURE);
  76.     }      
  77.         // Forcefully attaching socket to the port
  78.       if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
  79.              &opt, sizeof(opt)))
  80.     {
  81.       perror("setsockopt");
  82.       exit(EXIT_FAILURE);
  83.     }
  84.       address.sin_family = AF_INET;
  85.       address.sin_addr.s_addr = htonl(INADDR_ANY);
  86.       address.sin_port = htons((unsigned short)PORTudp );
  87.      
  88.       // Forcefully attaching socket to the port
  89.       if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0){
  90.         perror("bind failed");
  91.         exit(EXIT_FAILURE);
  92.       }
  93.      
  94.       int clientsize = sizeof(clientaddr);
  95.       int buffersize=sizeof(buffer);
  96.       int hellosize= sizeof(hello);
  97.       while(1){
  98.     if(recvfrom(server_fd, buffer, sizeof(buffer), 0, (struct sockaddr *) &clientaddr, &clientsize)<0){
  99.       error("receive failed");
  100.       exit(EXIT_FAILURE);
  101.     }
  102.  
  103.     printf("From Client: %s\n",buffer );
  104.     memset(buffer,0,strlen(buffer));
  105.     sendto(server_fd , hello , hellosize , 0, &clientaddr, clientsize );
  106.     printf("Hello message sent. Thread ID:%d \n",data->tid);
  107.         }
  108. }
  109.  
  110.  
  111. int main(int argc, char const *argv[])
  112. {
  113.  
  114.     PORTtcp = atoi(argv[1]);
  115.     PORTudp = atoi(argv[2]);
  116.     pthread_t tid[200];
  117.     thread_data_t thr_data[200];
  118.  
  119.  
  120.     for(int i=0;i<100;i++){
  121.       thr_data[i].tid=i;
  122.       pthread_create(&tid[i], NULL, newInstanceTCP, &thr_data[i]);
  123.     }
  124.     for(int i=100;i<200;i++){
  125.       thr_data[i].tid=i;
  126.       pthread_create(&tid[i], NULL, newInstanceUDP, &thr_data[i]);
  127.     }
  128.     while(1){
  129.     }
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement