Advertisement
zielo

Untitled

Jun 26th, 2018
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <cstring>
  5. //#include <sys/types.h>
  6. //#include <unistd.h>
  7.  
  8. #define PORT 6666
  9.  
  10. void err(const char *s)
  11. {
  12.     perror(s);
  13.     exit(1);
  14. }
  15.  
  16. using namespace std;
  17.  
  18. int main()
  19. {
  20.     int sock_fd,new_sock;
  21.     struct sockaddr_in s_addr;
  22. //    int opt = 1;
  23.     int addrlen = sizeof(s_addr);
  24.     char buffer[256] = {0};
  25.     char *hi = (char*)"Witamy na serwerze!";
  26.     sock_fd = socket(AF_INET, SOCK_STREAM, 0); // tworzymy gniazdo
  27.     if (sock_fd < 1) err("Blad otwierania gniazda!");
  28.   //  t = setsockopt(sock_fd,SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
  29.   //  if (t !=0 ) err("Error on sockopt")
  30.     s_addr.sin_family = AF_INET; //ipv4
  31.     s_addr.sin_addr.s_addr = INADDR_ANY; // aktualny adres IP
  32.     s_addr.sin_port = htons(PORT); //na sieciowy porzadek bajtow
  33.     if (bind(sock_fd, (struct sockaddr *)&s_addr,sizeof(s_addr)) < 0)
  34.         err("Blad podpinania portu");
  35.     listen(sock_fd,3); //kolejka ustawiona na 3
  36.     new_sock = accept(sock_fd, (struct sockaddr *)&s_addr,(socklen_t*)&addrlen);
  37.     if (new_sock < 0) err("Blad akceptowania polaczen");
  38.     send(new_sock, hi, strlen(hi),0);
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement