56 lines
1.3 KiB
C++
56 lines
1.3 KiB
C++
#include <arpa/inet.h>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
using std::cout, std::cin, std::endl, std::string;
|
|
|
|
int main() {
|
|
|
|
struct Message {
|
|
string content;
|
|
string username;
|
|
|
|
string toString() {
|
|
return "User '" + username + "' wrote:\n" + content;
|
|
}
|
|
};
|
|
|
|
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
sockaddr_in serverAdress;
|
|
serverAdress.sin_family = AF_INET;
|
|
serverAdress.sin_port = htons(8080);
|
|
serverAdress.sin_addr.s_addr = inet_addr("127.0.0.50");
|
|
|
|
int connectStatus = connect(clientSocket, (struct sockaddr *)&serverAdress,
|
|
sizeof(serverAdress));
|
|
if (connectStatus != 0) {
|
|
cout << "Failed to connect";
|
|
return connectStatus;
|
|
} else {
|
|
cout << "CONNECTED!" << endl;
|
|
}
|
|
|
|
while (true) {
|
|
Message message;
|
|
cout << "Type message to server: ";
|
|
cin >> message.content;
|
|
|
|
if (strcmp(message.content, "exit") == 0) {
|
|
break;
|
|
}
|
|
|
|
cout << endl;
|
|
message.username = "Client 1";
|
|
|
|
send(clientSocket, message.toString().data(),
|
|
strlen(message.toString().data()), 0);
|
|
}
|
|
|
|
close(clientSocket);
|
|
|
|
return 0;
|
|
}
|