-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMySocket.h
78 lines (65 loc) · 1.96 KB
/
MySocket.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
#ifndef MYSOCKET_H
#define MYSOCKET_H
#define ENOT_CONNECTED -1
#define EBROKEN_PIPE -2
#define ECONN_CLOSED -3
#define ESOCKET_ERROR -4
#include "MySocketException.h"
#include <string>
class MySocket {
public:
/*
* this is the constructor. It accepts a string representation of
* and ip address ("192.168.0.1") or domain name ("www.cs.uiuc.edu")
* and connects. Will throw an HostNotFound exception if the attepted
* connection fails. MySocket uses the TCP protocol.
*
* @param inetAddr either ip address, or the domain name
* @param port the port to connect to
*/
MySocket(const char *inetAddr, int port);
/*
* this constructor will generally not be used except for by ServerSockets
*/
MySocket(int socketFileDesc);
/*
* default constructor, makes sure the state is properly specified
*/
MySocket(void);
~MySocket(void);
/*
* reads the open socket. See the read system call
*
* @param buffer buffer of length len, where the data will be stored
* @param len the length of the buffer
*
* @return if there is no error, the number of bytes read in.
* ECONN_CLOSED - connection was closed
* EBROKEN_PIPE - broken pipe
* ENOT_CONNECTED - a connection was never established
*/
int read(void *buffer, int len);
/*
* writes to the open socket, see the write system call.
*
* @param buffer the buffer where the data is stored
* @param len the length of the buffer
*
* @return if there is no error, the number of bytes wrote.
* ECONN_CLOSED - connection was closed
* EBROKEN_PIPE - broken pipe
* ENOT_CONNECTED - a connection was never established
*/
int write(const void *buffer, int len);
bool write_bytes(std::string buffer);
bool write_bytes(const void *buffer, int len);
/*
* a helper function so select can be used
*/
int getFd(void) { return sockFd; }
void close(void);
protected:
int sockFd;
void brokenPipe(int sigNo);
};
#endif