Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <string>
- #include <arpa/inet.h>
- #include <sys/epoll.h>
- #include <fcntl.h>
- #include <errno.h>
- //#define IP "127.0.0.1"
- #define IP "192.168.10.11"
- #define PORT 3310
- #define BUFFER_SIZE 1024
- #define MAX_EVENTS 1
- using namespace std;
- int setNonBlocking(int fd) {
- int flags = fcntl(fd, F_GETFL, 0);
- if (flags == -1) return -1;
- return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
- }
- int main() {
- int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
- if (clientSocket == -1) {
- cerr << "Failed to create socket" << endl;
- return 1;
- }
- sockaddr_in serverAddress;
- serverAddress.sin_family = AF_INET;
- serverAddress.sin_port = htons(PORT);
- serverAddress.sin_addr.s_addr = inet_addr(IP);
- if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) {
- if (errno != EINPROGRESS) {
- cerr << "Connection failed to " << IP << ":" << PORT << endl;
- close(clientSocket);
- return 1;
- }
- }
- if (setNonBlocking(clientSocket) == -1) {
- cerr << "Failed to set socket to non-blocking" << endl;
- close(clientSocket);
- return 1;
- }
- int epollfd = epoll_create1(0);
- if (epollfd == -1) {
- cerr << "Failed to create epoll file descriptor" << endl;
- close(clientSocket);
- return 1;
- }
- struct epoll_event ev, events[MAX_EVENTS];
- ev.events = EPOLLOUT | EPOLLIN;
- ev.data.fd = clientSocket;
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, clientSocket, &ev) == -1) {
- cerr << "Failed to add file descriptor to epoll" << endl;
- close(epollfd);
- close(clientSocket);
- return 1;
- }
- const char* message = "PING\n";
- bool messageSent = false;
- char buffer[BUFFER_SIZE] = {0};
- int bytesReceived = 0;
- while (true) {
- int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
- if (nfds == -1) {
- cerr << "epoll_wait failed" << endl;
- break;
- }
- for (int n = 0; n < nfds; ++n) {
- if (events[n].events & EPOLLOUT) {
- if (!messageSent) {
- int bytesSent = send(clientSocket, message, strlen(message), 0);
- if (bytesSent == -1) {
- cerr << "Failed to send message" << endl;
- goto cleanup;
- }
- cout << "Message sent: " << message;
- messageSent = true;
- ev.events = EPOLLIN;
- epoll_ctl(epollfd, EPOLL_CTL_MOD, clientSocket, &ev);
- }
- } else if (events[n].events & EPOLLIN) {
- bytesReceived = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0);
- if (bytesReceived == -1) {
- if (errno != EAGAIN && errno != EWOULDBLOCK) {
- cerr << "Failed to receive response" << endl;
- goto cleanup;
- }
- } else if (bytesReceived == 0) {
- cout << "Server closed the connection" << endl;
- goto cleanup;
- } else {
- cout << "Response received: " << buffer << " , Bytes Rx: " << bytesReceived << endl;
- goto cleanup;
- }
- }
- }
- }
- cleanup:
- close(epollfd);
- close(clientSocket);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement