-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_request.cpp
More file actions
42 lines (36 loc) · 1.04 KB
/
Copy pathclient_request.cpp
File metadata and controls
42 lines (36 loc) · 1.04 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 <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include "format.cpp"
#include <string>
int talk(int fd,std::string msg){
write_to(fd,msg);
char rbuf[MAX_BUF] = {};
read_from(fd,rbuf);
printf("Server says: %s\n",rbuf);
return 0;
}
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("Couldn't connect to the server.\n");
}
n = talk(fd,"first hello, from client side.");
if(n){
perror("Unalbe to talk to the server.");
goto end;
}
n = talk(fd,"second hello, from client side.");
if(n){
perror("Unalbe to talk to the server.");
goto end;
}
end:
close(fd);
}