-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
103 lines (84 loc) · 2.21 KB
/
server.cpp
File metadata and controls
103 lines (84 loc) · 2.21 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
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main() {
int client, server;
int portNum = 1500;
bool isExit = false;
int bufsize = 1024;
char buffer[bufsize];
struct sockaddr_in server_addr;
socketlen_t size;
//init socket
client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0) {
cout << "Error establishing connection" << endl;
exit(1);
}
cout << "Server socket connection created..." << endl;
server_addr.sin_family = AF_INET;
server.addr.sin_addr.s_addr = htons(INADDER_ANY);
server.addr.sin_port = htons(portNum);
//binding socket
if (bind (client, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0 {
cout << "error binding socket" << endl;
}
size = sizeof(server_addr);
cout << "Looking for clients.." << endl;
//listening socket
listen(client, 1);
//accept client
server = accept (client, (struct) sockaddr*)&server_addr, &size);
if (server < 0) {
cout << "Error on accepting" << endl;
exit(1);
}
while (server > 0) {
strcpy(buffer, "Server connected...\n");
send(server, buffer, buffersize, 0);
cout << "Connected with client..." << endl;
cout << "Enter # to end connection" << endl;
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << "buffer" << " ";
if (*buffer == '#') {
*buffer = '*';
isExit = true;
}
} while (*buffer !='*');
do {
cout << "\nServer: ";
do {
cin << buffer;
send(server, buffer, bufsize, 0);
if (*buffer == "#") {
send(server, buffer, bufsize, 0);
*buffer = "*";
isExit = true;
}
} while (buffer !="*");
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if( *buffer == '#') {
*buffer == '*';
isExit = true;
}
} while (*buffer != '*');
} while (!isExit);
cout << " Connecting terminated" << endl;
cout << "Goodbye..." << endl;
isExit = false;
exit(1);
}
close(client);
return 0;
}