-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_client.cpp
More file actions
42 lines (37 loc) · 1.11 KB
/
Copy pathinteractive_client.cpp
File metadata and controls
42 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdlib>
#include <string>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <iostream>
#include "format.cpp"
#define MAX_BUF 4096
using std::string;
int main(){
int fd = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
int n = connect(fd,(const struct sockaddr*)&addr,sizeof(addr));
if(n){
perror("Unable to connect.");
exit(1);
}
while(true){
string str;
std::cout<<"Enter you message:";
getline(std::cin,str);
write_to(fd,str.data());
if(str != "exit"){
char rbuf[MAX_BUF] = {};
read_from(fd,rbuf);
printf("Server says: %s\n",rbuf);
}
else{
std::cout<<"connection closed\n";
break;
}
}
}