Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6. #include <stdlib.h>
  7.  
  8. #define MAX 8000
  9. #define PORTA 666
  10. #define MAX_CON 100
  11.  
  12. int main(void) {
  13.  
  14. char buff[MAX];
  15. struct sockaddr_in server;
  16. struct sockaddr_in client;
  17. int sd, temp_sd;
  18. int address_size;
  19. unsigned short port = 666;
  20.  
  21. if ( (sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  22. printf("Errore nella creazione del server!\n");
  23.  
  24. server.sin_family = AF_INET;
  25. server.sin_port = htons(port);
  26. server.sin_addr.s_addr = INADDR_ANY;
  27.  
  28. if (bind(sd, (struct sockaddr *)&server, sizeof(server)) < 0)
  29. printf("Errore nella chiamata di sistema BIND!\n");
  30.  
  31. listen (sd, MAX_CON);
  32.  
  33. while(1) {
  34.  
  35. if ((temp_sd= accept(sd, (struct sockaddr *)&client, &address_size)) < 0)
  36. printf("Errore nella chiamata ACCEPT\n");
  37.  
  38. while(1) {
  39. recv(temp_sd, buff, sizeof(buff), 0);
  40. printf("C>%s\nS>", buff);
  41. scanf("%s", buff);
  42. send(temp_sd, buff, strlen(buff), 0);
  43. }
  44.  
  45.  
  46. close(temp_sd);
  47. }
  48.  
  49. return EXIT_SUCCESS;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement