Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <string.h>
- #include <string>
- #include <unistd.h>
- int xYes = 1; // Needed For setsockopt ...
- //xIp = ip; // Set The IP Var To The Passed Argument
- xPort = port; // Set The Port Var To The Passed Argument
- xSock = socket(AF_INET, SOCK_STREAM, 0); // Create a Socket
- if (xSock < 0)
- {
- xLyLastError.assign("SOCKET CREATION FAILED");
- }
- memset(&xAddr, 0, sizeof(sockaddr_in)); // Fill The Memory WIth Zeors
- xAddr.sin_family = AF_INET; // This is For IPv4 , IPv6 Takes AF_INET6 But Its Not Supported For Now
- //inet_pton(AF_INET, xIp, &xAddr.sin_addr.s_addr); // Assing The IP To The Adress Struct
- xAddr.sin_addr.s_addr = INADDR_ANY; // Uncomment This If U Want The Server To Listen On All IPs It Has
- xAddr.sin_port = htons(xPort); // Connection Port
- // If We Didnt Close The Socket Last Time On The Same Port It Wont Work Without This ...
- setsockopt(xSock, SOL_SOCKET, SO_REUSEADDR, &xYes, sizeof(int));
- int x;
- x = bind(xSock, (sockaddr*)&xAddr, sizeof(sockaddr_in)); //xAddr Type is sockaddr_inbut we need sockaddr so we just use Casting
- if(x < 0)
- {
- exit(-1);
- }
- listen(xSock, xMaxC); // Listen For Connections
- socklen_t cSize = sizeof(sockaddr_in);
- xSock2 = accept(xSock, (sockaddr*)&xAddr2, &cSize); // Accept The Incomming Connection
- if(xSock2 < 0)
- {
- exit(-1);
- }
- send(xSock,"MMMMSSSGG", strlen("MMMMSSSGG"), 0);
- exit(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement