Advertisement
Guest User

server

a guest
Oct 25th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <stdio.h>
  4. #include <netinet/in.h>
  5. #include <netinet/ip.h>
  6. #include <string.h>
  7.  
  8. int main() {
  9.   int s, ss, l;
  10.   struct sockaddr_in server, server2, client;   //socketul pentru prima conecxiune, a 2-a si clinet
  11.  
  12.   s = socket(AF_INET, SOCK_STREAM, 0);
  13.   if (s < 0) {
  14.     printf("Eroare la crearea socketului server\n");
  15.     return 1;
  16.   }
  17.  
  18.   memset(&server, 0, sizeof(server));
  19.   server.sin_port = htons(1234);
  20.   server.sin_family = AF_INET;
  21.   server.sin_addr.s_addr = INADDR_ANY;
  22.  
  23.   if (bind(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
  24.     printf("Eroare la bind\n");
  25.     return 1;
  26.   }     //bind pe prima conexiune
  27.  
  28.   ss = socket(AF_INET, SOCK_STREAM, 0);
  29.   if(ss < 0){
  30.       printf("Eroare la crearea noului socket server\n");
  31.       return 1;
  32.   }
  33.  
  34.   memset(&server2, 0, sizeof(server2));     //initializare socket server 2
  35.   server2.sin_port = htons(2003);       //cu portul triis
  36.   server2.sin_family = AF_INET;
  37.   server2.sin_addr.s_addr = INADDR_ANY;
  38.  
  39.   if(bind(ss, (struct sockaddr *) &server2, sizeof(server2)) < 0){
  40.       printf("Eroare la bind 2\n");
  41.       return 1;     //bind pe al 2-lea socket
  42.   }
  43.  
  44.   listen(s, 5); //listen pe scketul 1
  45.   listen(ss, 5);//listen pe socketul 2
  46.  
  47.  
  48.   l = sizeof(client);
  49. memset(&client, 0, sizeof(client));   //curatare client
  50.  
  51.   while (1) {
  52.     int cc, c;
  53.     c = accept(s, (struct sockaddr *) &client, &l);
  54.     printf("S-a conectat un client.\n");
  55.     // deservirea clientului
  56.     short int port = 2003;
  57.     port = htons(port);
  58.     send(c, &port, sizeof(port), 0);
  59.     short int k=9;
  60.     k=htons(k);
  61.     send(c,&k,sizeof(k),0);
  62.  
  63.     char a[9]="127.0.0.1";
  64.  
  65.     for(int i=0;i<9;i++)
  66.         send(c,&a[i],sizeof(a[i]),MSG_WAITALL);
  67.  
  68.  
  69.     //trimterea noului port
  70.  
  71.  
  72.     memset(&client, 0, sizeof(client));
  73.     l = sizeof(client);
  74.     cc = accept(ss, (struct sockaddr*) &client, &l); //conectarea la clinet prin noul port
  75.  
  76.     printf("Te-ai conectat din nou :)!\n");
  77.     int num_char;
  78.     recv(cc, &num_char, sizeof(num_char), MSG_WAITALL);
  79.     printf("Numarul de barosi este: %d\n", ntohl(num_char));
  80.     num_char = ntohl(num_char);     //primi numarul de caractere de la client prin portul nou
  81.  
  82.     char* str = (char*)malloc(num_char*sizeof(char));
  83.     for(int i = 0; i<num_char; i++){
  84.         char a;
  85.         recv(cc, &a, sizeof(a), MSG_WAITALL);   //citim sirul de caractere
  86.         str[i] = a;
  87.     }
  88.  
  89.     for(int i = 0; i<num_char; i++){
  90.         str[i] = 'B';       //cream un not sir
  91.  
  92.     int to_send = htonl(num_char);  //trimitem inapoi lungimea
  93.     send(cc, &to_send, sizeof(to_send), 0);
  94.     for(int i = 0; i<num_char; i++){
  95.         send(cc, &str[i], sizeof(str[i]), 0);   //trimitem inapoi sirul
  96.     }
  97.     close(c);
  98.     close(cc);
  99.     // sfarsitul deservirii clientului;
  100.   }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement