-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat_server.cpp
87 lines (77 loc) · 2.21 KB
/
chat_server.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
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
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
/*struct in_addr {
unsigned long s_addr;
};
*/
/*struct sockaddr_in {
short int sin_family;
unsigned short int sin_port;
struct in_addr sin_addr;
unsigned char sin_zero[8];
};*/
int main(){
int domain = AF_INET;
int type = SOCK_STREAM;
int protocol = 0;
int status = 0;
int file_descriptor = 0;
int port_number = 5010;
struct sockaddr_in server_socket, client_socket;
socklen_t length_client;
char buffer[1024];
bzero((char *) &server_socket, sizeof(server_socket));
bzero((char *) &client_socket, sizeof(client_socket));
//initialising socket stucture
server_socket.sin_family = AF_INET;
server_socket.sin_addr.s_addr = inet_addr("127.0.0.1");
server_socket.sin_port = htons(port_number);
length_client = sizeof(client_socket);
cout<<"Welcome"<<server_socket.sin_addr.s_addr<<" "<<server_socket.sin_port<<endl;
file_descriptor = socket(domain,type,protocol);
if(file_descriptor < 0){
cout<<"Error, failed to create sockets"<<endl;
}
else{
status = bind(file_descriptor, (struct sockaddr *)&server_socket, sizeof(server_socket));
if(status < 0){
cout<<"Error, failed to bind to address"<<endl;
}
else{
status = listen(file_descriptor, 10);
if(status != 0){
cout<<"Error, failed to get to listen mode"<<endl;
}
else{
cout<<"Server has started....Please make requests to connect."<<endl;
file_descriptor = accept(file_descriptor, (struct sockaddr *)&client_socket, &length_client);
if(file_descriptor<0){
cout<<"Error, failed to create dedicated sockets for client"<<endl;
}
else{
cout<<"HEY! CONNECTED SUCCESSFULLY"<<endl;
while(1){
bzero(buffer,256);
int n = read(file_descriptor,buffer,255);
cout<<buffer<<endl;
if(n<0){
cout<<"ERROR reading from socket"<<endl;
}
bzero(buffer,256);
cin.getline(buffer, '\n');
n = write(file_descriptor,buffer,12);
if(n<0){
cout<<"Error writing to port"<<endl;
}
}
}
}
}
}
}