Created
March 5, 2026 07:59
-
-
Save nicolekorch/dc1b257c632ffe789967664ba9399ec4 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 <thread> | |
| #include <string> | |
| #include <cstring> | |
| #include <ctime> | |
| #include <unistd.h> | |
| #include <arpa/inet.h> | |
| #include <sys/socket.h> | |
| using namespace std; | |
| string get_time() { | |
| time_t now = time(0); | |
| tm *ltm = localtime(&now); | |
| char buf[64]; | |
| sprintf(buf,"%02d:%02d:%02d", ltm->tm_hour, ltm->tm_min, ltm->tm_sec); | |
| return string(buf); | |
| } | |
| string get_date() { | |
| time_t now = time(0); | |
| tm *ltm = localtime(&now); | |
| char buf[64]; | |
| sprintf(buf,"%02d-%02d-%04d", ltm->tm_mday, ltm->tm_mon+1, 1900+ltm->tm_year); | |
| return string(buf); | |
| } | |
| string get_weather(string city) { | |
| return "Weather in " + city + ": +10C"; | |
| } | |
| string get_euro() { | |
| return "EUR = 4.30 PLN"; | |
| } | |
| string get_btc() { | |
| return "BTC = 60000 USD"; | |
| } | |
| void handle_client(int client_socket) { | |
| char buffer[1024]; | |
| while(true) { | |
| memset(buffer,0,sizeof(buffer)); | |
| int bytes = recv(client_socket, buffer, sizeof(buffer),0); | |
| if(bytes <= 0) break; | |
| string request(buffer); | |
| string response; | |
| if(request == "time") | |
| response = get_time(); | |
| else if(request == "date") | |
| response = get_date(); | |
| else if(request.find("weather") == 0) { | |
| string city = request.substr(8); | |
| response = get_weather(city); | |
| } | |
| else if(request == "euro") | |
| response = get_euro(); | |
| else if(request == "btc") | |
| response = get_btc(); | |
| else | |
| response = "Unknown command"; | |
| send(client_socket,response.c_str(),response.size(),0); | |
| } | |
| close(client_socket); | |
| } | |
| int main() { | |
| int server_fd = socket(AF_INET, SOCK_STREAM, 0); | |
| sockaddr_in address; | |
| address.sin_family = AF_INET; | |
| address.sin_port = htons(8080); | |
| address.sin_addr.s_addr = INADDR_ANY; | |
| bind(server_fd,(sockaddr*)&address,sizeof(address)); | |
| listen(server_fd,5); | |
| cout<<"Server started...\n"; | |
| while(true) { | |
| socklen_t addrlen = sizeof(address); | |
| int client_socket = accept(server_fd,(sockaddr*)&address,&addrlen); | |
| thread t(handle_client,client_socket); | |
| t.detach(); | |
| } | |
| close(server_fd); | |
| } | |
| #include <iostream> | |
| #include <string> | |
| #include <unistd.h> | |
| #include <arpa/inet.h> | |
| #include <sys/socket.h> | |
| using namespace std; | |
| int main() { | |
| int sock = socket(AF_INET,SOCK_STREAM,0); | |
| sockaddr_in server; | |
| server.sin_family = AF_INET; | |
| server.sin_port = htons(8080); | |
| inet_pton(AF_INET,"127.0.0.1",&server.sin_addr); | |
| connect(sock,(sockaddr*)&server,sizeof(server)); | |
| while(true) { | |
| string command; | |
| cout<<"Enter command: "; | |
| getline(cin,command); | |
| send(sock,command.c_str(),command.size(),0); | |
| char buffer[1024]; | |
| int bytes = recv(sock,buffer,sizeof(buffer),0); | |
| buffer[bytes] = 0; | |
| cout<<"Server: "<<buffer<<endl; | |
| } | |
| close(sock); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ю