Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <cstring>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- using namespace std;
- int main() {
- int socketId = socket(AF_INET, SOCK_STREAM, 0);
- sockaddr_in sa = {AF_INET, htons(8888), INADDR_ANY};
- const int reuseProp = 1;
- setsockopt(socketId, SOL_SOCKET, SO_REUSEADDR, &reuseProp, sizeof(reuseProp));
- if (bind(socketId, (sockaddr*) &sa, sizeof(sa)) != 0) {
- perror("An error occured");
- }
- int client;
- listen(socketId, 10);
- while (true) {
- sockaddr_in connection;
- socklen_t conSize = sizeof(connection);
- client = accept(socketId, (sockaddr*) &connection, &conSize);
- printf("%s:%d\n", inet_ntoa(connection.sin_addr), htons(connection.sin_port));
- }
- // char buffer[256] = "Sieci Komputerowe II";
- // write(connection, buffer, strlen(buffer));
- shutdown(client, SHUT_RDWR);
- close(socketId);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement