-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSocket.h
89 lines (81 loc) · 3.22 KB
/
Socket.h
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
/************************************************************
* File description: Socket class header file. *
* contains socket properties as members, declaration on *
* methods, and const numbers using by all classes which *
* inherit from Socket class *
************************************************************/
#ifndef SOCKET_H_
#define SOCKET_H_
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
//return values to each function if error happened
#define CORRECT 0
#define ERROR_SOCKET 1
#define ERROR_BIND 2
#define ERROR_LISTEN 3
#define ERROR_CONNECT 4
#define ERROR_SEND 5
#define ERROR_RECIVE 6
#define ERROR_ACCEPT 7
#define CONNECTION_CLOSED 8
#define IP "127.0.0.1"
class Socket {
protected:
//true is the socket is for a server, false if for a client
bool isServer;
//the socket descriptor return from sock()
int socketDescriptor;
//ip adress
string ip_address;
int backLog;
//port number
int port_number;
public:
/***********************************************************************
* function name: Socket *
* The Input: none *
* The output: none *
* The Function operation: creating new Socket object *
***********************************************************************/
Socket();
Socket(string ip);
/***********************************************************************
* function name: ~Socket *
* The Input: none *
* The output: none *
* The Function operation: default destructor *
***********************************************************************/
virtual ~Socket();
/***********************************************************************
* function name: initialize *
* The Input: none *
* The output: int number representing the return status *
* The Function operation: initialize the Socket object and getting a *
* socket descriptor. pure virtual method *
***********************************************************************/
virtual int initialize() = 0;
/***********************************************************************
* function name: sendData *
* The Input: string representing the data to send *
* The output: int number representing the return status *
* The Function operation: sending the input data to the socket *
* who connect to this socket. pure virtual method *
***********************************************************************/
virtual int sendData(string data, int) = 0;
/***********************************************************************
* function name: recive ` *
* The Input: none *
* The output: int number representing the return status *
* The Function operation: getting data from the other socket and print *
* the data *
***********************************************************************/
virtual int reciveData(char* buffer, int size, int) = 0;
virtual int acceptSock() = 0;
int upto;
};
#endif /* SOCKET_H_ */