-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
112 lines (93 loc) · 2.87 KB
/
server.cpp
File metadata and controls
112 lines (93 loc) · 2.87 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* server.cpp -- simple server using sockets
* @Author Dane
*/
#include "server.h"
void help(){
std::cout << "USAGE: \n ./server [port]" << std::endl;
}
void error(char *msg){
std::cout << "---ERROR---"<< std::endl;
std::wcout << msg << std::endl;
exit(1);
}
int main(int argc, char *argv[]){
//check usage
if(argc !=2){
help();
exit(1);
}
char hostname[] = HOSTNAME;
int status=0; //used to check status of getaddrinfo
int port_num; //port number
int socket_fd; //the file descryptor for the socket we will use
int socket_fd_client; //file descryptor of the client
char buf_in[256]; //buffer that holds incoming
char buf_out[256]; //buffer that holds outgoing
struct sockaddr_in addr_serv; //servers address struct
struct sockaddr_in addr_client; //clients address struct
struct addrinfo addr_info; //the address info
struct addrinfo *addr_results; //the results of the address info
socklen_t size_addr;
int connected = 0;
std::string input;
//grab port parameter
port_num = atoi(argv[1]);
//look up address and get the correct information
memset(&addr_info, 0, sizeof(addr_info));//set the struct values to 0
addr_info.ai_family = AF_UNSPEC; //unspecified address version ip4 or ip6
addr_info.ai_socktype = SOCK_STREAM; //type of socket to use this case is tcp
if(LOCAL){
addr_info.ai_flags = AI_PASSIVE; //use this machines ip
//get check that we got addr_info results
if((status=getaddrinfo(NULL,argv[1], &addr_info, &addr_results)) != 0){
error("could not get addrinfo");
}
}
//get the results of addrinfo
else if((status = getaddrinfo(hostname, argv[1], &addr_info, &addr_results) ) != 0){
error("could not get addrinfo");
}
//setup socket
//using internet addressing, tcp stream, and protocol
socket_fd = socket(addr_results->ai_family, addr_results->ai_socktype, addr_results->ai_protocol);
//check to see if it worked
if(socket_fd<0){
error("socket_fd not created");
}
//bind to a specific port
if((status =bind(socket_fd, addr_results->ai_addr, addr_results->ai_addrlen))==-1){
error("could not bind port");
}
//connect to our socket
//if((status = connect(socket_fd, addr_results->ai_addr,addr_results->ai_addrlen))==-1){
// error("could not connect")
//}
//listen to our socket
listen(socket_fd, BACKLOG);
while(1){
//accept recieved connections
size_addr = sizeof addr_client;
socket_fd_client = accept(socket_fd, (struct sockaddr *)&addr_client,&size_addr);
if(socket_fd_client == -1){
continue;
}
//someone connected
std::cout << "server got a connection" << std::endl;
if((status = send(socket_fd_client, "you have connected\n",19,0)) == -1){
error("error sending");
}
connected = 1;
while(connected){
std::cin >> input;
if(input.compare("exit")==0){
break;
}
else if((status = send(socket_fd_client, input.c_str() ,strlen(input.c_str()),0)) == -1){
error("error sending");
}
}
break;
}
return 0;
}