Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <unistd.h>
  9. using namespace std;
  10.  
  11. void errore(...){
  12.     cerr<<"Errore!"<<endl;
  13.     exit(1);
  14. }
  15.  
  16. int main(){
  17.     const int port = 4444;
  18.  
  19.     int sockfd;
  20.     struct sockaddr_in serverAddr;
  21.  
  22.     int newSocket;
  23.     struct sockaddr_in newAddr;
  24.  
  25.     socklen_t addr_size;
  26.  
  27.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  28.     if(sockfd == -1)  errore();
  29.     cout<<"[+]Server Socket Creata.\n";
  30.  
  31.     serverAddr.sin_family = AF_INET;
  32.     serverAddr.sin_port = htons(port);
  33.     serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  34.  
  35.    
  36.     if((bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr))) == -1)
  37.         errore();
  38.  
  39.     cout<<"[+]Bind sulla Port numero "<<port<<endl;
  40.  
  41.  
  42.     if((listen(sockfd, 5)) == -1)  errore();
  43.     cout<<"[+]Sto ascoltanto...\n";
  44.  
  45.     newSocket = accept(sockfd, (struct sockaddr*)&newAddr, &addr_size);
  46.     if(newSocket == -1)  errore();
  47.     cout<<"Connessione accetta";
  48.     cout<<"[+]Sto chiudendo la connessione\n";
  49.     close(sockfd);
  50.     close(newSocket);
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement