Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <cstring>
- //#include <sys/types.h>
- //#include <unistd.h>
- #define PORT 6666
- void err(const char *s)
- {
- perror(s);
- exit(1);
- }
- using namespace std;
- int main()
- {
- int sock_fd,new_sock;
- struct sockaddr_in s_addr;
- // int opt = 1;
- int addrlen = sizeof(s_addr);
- char buffer[256] = {0};
- char *hi = (char*)"Witamy na serwerze!";
- sock_fd = socket(AF_INET, SOCK_STREAM, 0); // tworzymy gniazdo
- if (sock_fd < 1) err("Blad otwierania gniazda!");
- // t = setsockopt(sock_fd,SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
- // if (t !=0 ) err("Error on sockopt")
- s_addr.sin_family = AF_INET; //ipv4
- s_addr.sin_addr.s_addr = INADDR_ANY; // aktualny adres IP
- s_addr.sin_port = htons(PORT); //na sieciowy porzadek bajtow
- if (bind(sock_fd, (struct sockaddr *)&s_addr,sizeof(s_addr)) < 0)
- err("Blad podpinania portu");
- listen(sock_fd,3); //kolejka ustawiona na 3
- new_sock = accept(sock_fd, (struct sockaddr *)&s_addr,(socklen_t*)&addrlen);
- if (new_sock < 0) err("Blad akceptowania polaczen");
- send(new_sock, hi, strlen(hi),0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement