Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <cstdlib>
- #include <unistd.h>
- #include <cstring>
- #include <arpa/inet.h>
- #include <iostream>
- #include <vector>
- int main()
- {
- auto tmpBuffer = new char[8];
- sockaddr_in myAddr = {AF_INET, htons(1234), INADDR_ANY};
- auto sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
- bind(sock, (sockaddr*) &myAddr, sizeof(myAddr));
- const int one = 1;
- setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
- listen(sock, 1);
- sockaddr_in inputAddr{};
- socklen_t t = sizeof(inputAddr);
- while(true)
- {
- auto descriptor = accept(sock, (sockaddr *) &inputAddr, &t);
- std::cout << "IP: " << inet_ntoa(inputAddr.sin_addr) << std::endl;
- std::cout << "Port: " << ntohs(inputAddr.sin_port) << std::endl;
- while((read(descriptor, tmpBuffer, 8)) > 0)
- std::cout << tmpBuffer << std::endl;
- close(descriptor);
- }
- close(sock);
- free(tmpBuffer);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement