-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
34 lines (28 loc) · 896 Bytes
/
Copy pathclient.cpp
File metadata and controls
34 lines (28 loc) · 896 Bytes
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
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <mutex>
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");
}
char wbuf[64] = "Hello, the client here.";
n = write(fd,&wbuf,sizeof(wbuf));
if(n<0){
perror("Couldn't write to the server.");
}
char rbuf[64] = {};
n = read(fd,&rbuf,sizeof(rbuf));
if(n<0){
perror("Couldn't read from the server.");
}
printf("Server says: %s\n",rbuf);
close(fd);
}