-
Notifications
You must be signed in to change notification settings - Fork 0
/
servertest.cpp
46 lines (43 loc) · 1.27 KB
/
servertest.cpp
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
#include <iostream> //cout, cin, cerr
#include <sys/socket.h> // socket, setsockopt, getsockname, bind, connect, listen, accept, send, recv
#include <unistd.h> // close
#include <netdb.h> // getprotobyname, gethostbyname, freeaddrinfo, getaddrinfo
#include <arpa/inet.h> // htonl, htons, ntohs, ntohl, inet_addr, inet_ntoa,
#include <signal.h> // signal
#include <fcntl.h> // lseek, fstat, fcntl
#include <poll.h> // poll
#include <netinet/in.h> // struct sockaddr_in
#include <vector>
#include <map>
#include <functional>
#include <algorithm> // std::transform
#include <cctype> // std::toupper
#include <string>
#include <cstring>
int main()
{
int socfd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in saddr, clientAddr;
saddr.sin_family = AF_INET;
saddr.sinserverPort = htons(6667);
saddr.sin_addr.s_addr = INADDR_ANY;
if (bind(socfd, (struct sockaddr *)&saddr, sizeof(saddr)) == -1)
{
std::cerr << "Error binding server socket." << std::endl;
return 1;
}
listen(socfd, 10);
int cfd;
socklen_t clientAddrSize = sizeof(clientAddr);
char buffer[1024];
cfd = accept(socfd, NULL, NULL);
std::string msg = "MERHABA!\n";
send(cfd, msg.c_str(), sizeof(msg), 0);
while (1)
{
recv(cfd, buffer, sizeof(buffer), 0);
std::cout << buffer << "\n";
}
close(socfd);
return (0);
}