Created
February 19, 2026 17:49
-
-
Save vlaleli/04538f3ae49c398927a2ab35fecc8f37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| #include <cstring> | |
| #include <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <arpa/inet.h> | |
| #include <unistd.h> | |
| #include <signal.h> | |
| using namespace std; | |
| int main() | |
| { | |
| signal(SIGPIPE, SIG_IGN); | |
| const int PORT = 54000; | |
| int sock = socket(AF_INET, SOCK_STREAM, 0); | |
| if (sock < 0) | |
| { | |
| cerr << "socket error\n"; | |
| return 1; | |
| } | |
| sockaddr_in hint{}; | |
| hint.sin_family = AF_INET; | |
| hint.sin_port = htons(PORT); | |
| inet_pton(AF_INET, "127.0.0.1", &hint.sin_addr); | |
| if (connect(sock, (sockaddr*)&hint, sizeof(hint)) < 0) | |
| { | |
| cerr << "connect error\n"; | |
| close(sock); | |
| return 1; | |
| } | |
| char buf[256]; | |
| string input; | |
| while (true) | |
| { | |
| cout << "Enter integer: "; | |
| getline(cin, input); | |
| if (input.empty()) | |
| continue; | |
| send(sock, input.c_str(), input.size(), MSG_NOSIGNAL); | |
| memset(buf, 0, sizeof(buf)); | |
| ssize_t bytesReceived = recv(sock, buf, sizeof(buf) - 1, 0); | |
| if (bytesReceived <= 0) | |
| break; | |
| cout << "Server replied: " << buf << endl; | |
| } | |
| close(sock); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| #include <cstring> | |
| #include <cstdlib> | |
| #include <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <netinet/in.h> | |
| #include <unistd.h> | |
| #include <signal.h> | |
| using namespace std; | |
| int main() | |
| { | |
| signal(SIGPIPE, SIG_IGN); | |
| const int PORT = 54000; | |
| int serverSocket = socket(AF_INET, SOCK_STREAM, 0); | |
| if (serverSocket < 0) | |
| { | |
| cerr << "socket error\n"; | |
| return 1; | |
| } | |
| int opt = 1; | |
| setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
| sockaddr_in hint{}; | |
| hint.sin_family = AF_INET; | |
| hint.sin_port = htons(PORT); | |
| hint.sin_addr.s_addr = INADDR_ANY; | |
| if (::bind(serverSocket, (sockaddr*)&hint, (socklen_t)sizeof(hint)) < 0) | |
| { | |
| cerr << "bind error\n"; | |
| close(serverSocket); | |
| return 1; | |
| } | |
| if (listen(serverSocket, SOMAXCONN) < 0) | |
| { | |
| cerr << "listen error\n"; | |
| close(serverSocket); | |
| return 1; | |
| } | |
| cerr << "Server started. Waiting for client...\n"; | |
| sockaddr_in client{}; | |
| socklen_t clientSize = sizeof(client); | |
| int clientSocket = accept(serverSocket, (sockaddr*)&client, &clientSize); | |
| if (clientSocket < 0) | |
| { | |
| cerr << "accept error\n"; | |
| close(serverSocket); | |
| return 1; | |
| } | |
| char buf[256]; | |
| while (true) | |
| { | |
| memset(buf, 0, sizeof(buf)); | |
| ssize_t bytesReceived = recv(clientSocket, buf, sizeof(buf) - 1, 0); | |
| if (bytesReceived <= 0) | |
| break; | |
| long value = strtol(buf, nullptr, 10); | |
| long result = value + 1; | |
| string out = to_string(result); | |
| send(clientSocket, out.c_str(), out.size(), 0); | |
| } | |
| close(clientSocket); | |
| close(serverSocket); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment