Compare commits
No commits in common. "cfb7478ab948714643511b3e6fb6a3fe93048795" and "3b782e664acc1f665b5bec0a6a994e53532cd528" have entirely different histories.
cfb7478ab9
...
3b782e664a
2 changed files with 16 additions and 30 deletions
|
@ -2,7 +2,6 @@
|
|||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <netinet/in.h>
|
||||
#include <ostream>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -11,12 +10,8 @@ using std::cout, std::cin, std::endl, std::string;
|
|||
int main() {
|
||||
|
||||
struct Message {
|
||||
string content;
|
||||
char * content;
|
||||
string username;
|
||||
|
||||
string toString() {
|
||||
return "User '" + username + "' wrote:\n" + content;
|
||||
}
|
||||
};
|
||||
|
||||
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
@ -26,30 +21,19 @@ int main() {
|
|||
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 (message.content == "exit") {
|
||||
cout << "BYE!" << endl;
|
||||
break;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
message.username = "Client 1";
|
||||
|
||||
send(clientSocket, message.toString().data(),
|
||||
strlen(message.toString().data()), 0);
|
||||
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);
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#include <arpa/inet.h>
|
||||
#include <iostream>
|
||||
#include <netinet/in.h>
|
||||
#include <ostream>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -9,7 +8,12 @@ using std::cout, std::endl, std::string;
|
|||
|
||||
int main() {
|
||||
|
||||
cout << "Initializing server" << endl;
|
||||
struct Message {
|
||||
string content;
|
||||
string username;
|
||||
};
|
||||
|
||||
cout << "Initializing server";
|
||||
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (serverSocket == -1) {
|
||||
// TODO error
|
||||
|
@ -36,11 +40,9 @@ int main() {
|
|||
// TODO error
|
||||
}
|
||||
|
||||
while (true) {
|
||||
char buffer[1024] = { 0 };
|
||||
recv(clientSocket, buffer, sizeof(buffer), 0);
|
||||
cout << buffer << endl;
|
||||
}
|
||||
cout << "Message from client " << buffer << endl;
|
||||
|
||||
close(serverSocket);
|
||||
|
||||
|
|
Reference in a new issue