-
Notifications
You must be signed in to change notification settings - Fork 0
/
easysock.hpp
123 lines (122 loc) · 3.06 KB
/
easysock.hpp
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
113
114
115
116
117
118
119
120
121
122
123
//LINUX ONLY, WORKING ON WINDOWS VERSION
#ifndef __EASYSOCK_HPP__
#define __EASYSOCK_HPP__
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <vector>
#include <unistd.h>
namespace bstd{
class eserver{
public:
int sockfd, portno, n;
socklen_t clilen;
std::vector<unsigned int> newsockfd;
char *buffer;
int bs;
struct sockaddr_in serv_addr, cli_addr;
void err(char *msg){
printf("%s",msg);
}
eserver(int buffersize, int port){
buffer = new char[buffersize];
bs = buffersize;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
err((char*)"ERROR opening socket\n");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = port;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
err((char*)"ERROR on binding\n");
}
void Listen(){
listen(sockfd,5);
}
int Accept(){
clilen = sizeof(cli_addr);
newsockfd.push_back(accept(sockfd, (struct sockaddr *) &cli_addr, &clilen));
return newsockfd.size()-1;
}
char * Read(int index,int amt){
bzero(buffer,bs);
n = read(newsockfd[index],buffer,amt);
if(n<0){
err((char*)"ERROR reading from socket.\n");
buffer = (char*)"";
}
return buffer;
}
void Send(int index,char * msg){
n = write(newsockfd[index],msg,strlen(msg));
if (n < 0)
err((char*)"ERROR writing to socket\n");
}
void Send(int index,char * msg, int len){
n = write(newsockfd[index],msg,len);
if (n < 0)
err((char*)"ERROR writing to socket\n");
}
void Close(){
close(sockfd);
}
~eserver(){
delete[] buffer;
}
};
class eclient{
public:
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
int bs;
char *buffer;
void err(char *msg){
printf("%s",msg);
}
eclient(int buffersize, int portno, char* hostname){
bs = buffersize;
buffer = new char[buffersize];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
server = gethostbyname(hostname);
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
}
int Connect(){
if (connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0){
err((char*)"ERROR connecting\n");
return -1;
}
return 0;
}
void Send(char * msg){
n = write(sockfd,msg,strlen(msg));
if (n < 0)
err((char*)"ERROR writing to socket\n");
}
void Send(char * msg, int len){
n = write(sockfd,msg,len);
if (n < 0)
err((char*)"ERROR writing to socket\n");
}
char * Read(int amt){
bzero(buffer,bs);
n = read(sockfd,buffer,amt);
if (n < 0)
err((char*)"ERROR reading from socket\n");
return buffer;
}
~eclient(){
delete[] buffer;
}
};
}
#endif