-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_server.cpp
More file actions
53 lines (44 loc) · 1.42 KB
/
Copy paththread_server.cpp
File metadata and controls
53 lines (44 loc) · 1.42 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
43
44
45
46
47
48
49
50
51
52
53
#include <cstdio>
#include<sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <thread>
void handle_client(int c_fd){
char rbuf[64] = {};
int n = read(c_fd,&rbuf,sizeof(rbuf));
if(n<0){
perror("Couldn't read data from the client\n");
}
printf("Client says: %s\n",rbuf);
char wbuf[64] = "Hello, from the server";
n = write(c_fd,&wbuf,sizeof(wbuf));
if(n<0){
perror("Couldn't write data to the socket\n");
}
}
static int on = 1;
int main(){
int fd = socket(AF_INET, SOCK_STREAM,0);
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR, &on,sizeof(on));
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
addr.sin_addr.s_addr = htonl(0);
int n = bind(fd,(const struct sockaddr*)&addr,sizeof(addr));
if(n){
perror("Couldn't bind.\n");
}
n = listen(fd,10);
if(n){
perror("Couldn't start to listen.\n");
}
printf("Server is Listening!!\n");
fflush(stdout);
while(true){
struct sockaddr_in c_addr = {};
socklen_t len = sizeof(c_addr);
int c_fd = accept(fd,(struct sockaddr*)&c_addr, &len);
std::thread my_thread(handle_client,c_fd);
my_thread.detach();
}
}