Archived
1
0
Fork 0

Add client

This commit is contained in:
Simon V. Lejel 2024-02-10 13:35:26 +01:00
parent 810fb50ddd
commit 3b782e664a
Signed by: sl
GPG key ID: 6544A0430A2CFFAD

42
src/client/main.cc Normal file
View file

@ -0,0 +1,42 @@
#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 {
char * content;
string username;
};
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");
while (true) {
Message message;
cout << "Type message to server: ";
cin >> message.content;
cout << endl;
message.username = "Client 1";
if (strcmp(message.content, "exit") == 0) {
break;
}
cout << "Sending message to server as user: " << message.username << endl;
send(clientSocket, message.content, strlen(message.content), 0);
}
close(clientSocket);
return 0;
}