-
Notifications
You must be signed in to change notification settings - Fork 1
/
TcpSocket.h
127 lines (120 loc) · 3.43 KB
/
TcpSocket.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
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
124
125
126
127
#ifndef __TCPSOCKET_H__
#define __TCPSOCKET_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <string>
class TcpSocket
{
protected:
struct SocketData
{
int socketDescriptor;
struct sockaddr_in address;
};
SocketData mySocketData;
public:
TcpSocket(int port)
{
if((mySocketData.socketDescriptor=socket(AF_INET,SOCK_STREAM,0))<0)
throw "socket";
memset(&(mySocketData.address),0x00,sizeof(struct sockaddr_in));
mySocketData.address.sin_family=AF_INET;
mySocketData.address.sin_port=htons(port);
}
~TcpSocket()
{
close(mySocketData.socketDescriptor);
}
void send(int& socketDescriptor,std::string msg,int key)
{
if(::send(socketDescriptor, cryption(msg,key).c_str(), msg.length()+1,0)<0)
throw "send";
}
std::string receive(int& socketDescriptor,int key)
{
std::string ret;
char tmp;
int status;
while(1)
{
if((status=recv(socketDescriptor,&tmp,1,0))<0)
throw "recv";
else if(status==0)
throw "recv(connection end)";
if(tmp=='\0')
break;
ret.push_back(tmp);
}
return cryption(ret,key);
}
std::string cryption(std::string str,int key)
{
for(int i=0;i<str.length();i++)
str.at(i)^=key;
return str;
}
};
class ServerTcpSocket : public TcpSocket
{
private:
SocketData& serverSocketData=mySocketData;
SocketData clientSocketData;
public:
ServerTcpSocket(int port=6974) : TcpSocket(port)
{
serverSocketData.address.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(serverSocketData.socketDescriptor,(struct sockaddr*)&(serverSocketData.address),sizeof(struct sockaddr_in))<0)
throw "bind";
if(listen(serverSocketData.socketDescriptor,0)<0)
throw "listen";
}
int accept()
{
socklen_t addressLength=sizeof(struct sockaddr_in);
clientSocketData.socketDescriptor=::accept(serverSocketData.socketDescriptor,(struct sockaddr*)&(clientSocketData.address),&addressLength);
return clientSocketData.socketDescriptor;
}
ServerTcpSocket& send(std::string msg,int key=0)
{
TcpSocket::send(clientSocketData.socketDescriptor,msg,key);
return *this;
}
std::string receive(int key=0)
{
return TcpSocket::receive(clientSocketData.socketDescriptor,key);
}
void closeAccept()
{
close(clientSocketData.socketDescriptor);
}
};
class ClientTcpSocket : public TcpSocket
{
private:
SocketData& clientSocketData=mySocketData;
public:
ClientTcpSocket(std::string IP="127.0.0.1",int port=6974) : TcpSocket(port)
{
clientSocketData.address.sin_addr.s_addr=inet_addr(IP.c_str());
if(connect(clientSocketData.socketDescriptor,(struct sockaddr*)&clientSocketData.address,sizeof(struct sockaddr_in))<0)
throw "connect";
}
ClientTcpSocket& send(std::string msg,int key=0)
{
TcpSocket::send(clientSocketData.socketDescriptor,msg,key);
return *this;
}
std::string receive(int key=0)
{
return TcpSocket::receive(clientSocketData.socketDescriptor,key);
}
};
#endif