-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysocket.h
169 lines (155 loc) · 4.43 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef MYSOCKET_H_INCLUDED
#define MYSOCKET_H_INCLUDED
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstdlib>
#include <cerrno>
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
struct TcpSocket{
int sockfd;
struct sockaddr_in servaddr;
int port;
TcpSocket(){
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1){
perror("can't create socket\n");
}
}
TcpSocket(int _sockfd,struct sockaddr_in _addr){
sockfd = _sockfd;
servaddr = _addr;
}
int connectToServ(const char* ip,int port){
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = inet_addr(ip);
if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1){
perror("can't connect to server!\n");
return -1;
}
return 0;
}
int send(const void* buf,int len){
return ::send(sockfd,buf,len,0);
}
int recv(void* buf,int len){
return ::recv(sockfd,buf,len,0);
}
int sendString(const char* str){
return send(str,strlen(str));
}
string readLine(int* res_code){
string res;
const int BUF_SIZE=128;
static char buf[BUF_SIZE+1];
while(1){
memset(buf,0,sizeof(buf));
int len = recv(buf,BUF_SIZE);
if(len>0){
res += string(buf);
//cout<<"part:"<<res<<endl;
if(strstr(buf,"\r\n")!=NULL){
*res_code=0;
//printf("Response:%s\n",res.c_str());
return res;
}
} else {
*res_code = -1;
return res;
}
}
}
int close(){
return ::close(sockfd);
}
int sendFile(const char* file_name){
const int READ_BUF_SIZE=1024;
char buf[READ_BUF_SIZE];
FILE* file = fopen(file_name,"r");
if(file==NULL){
return -1;
}
int read_size = 0;
int total_size = 0;
while((read_size=fread(buf,1,READ_BUF_SIZE,file))>0){
send(buf,read_size);
total_size+=read_size;
}
return total_size;
}
int recvFile(const char* file_name){
const int READ_BUF_LEN=1024;
char buf[READ_BUF_LEN];
FILE* file = fopen(file_name,"w");
if(file==NULL){
return -1;
}
int recv_len = 0;
int total_size = 0;
while((recv_len = recv(buf,READ_BUF_LEN))>0){
fwrite(buf,1,recv_len,file);
total_size += recv_len;
}
fclose(file);
return total_size;
}
};
struct TcpServer : public TcpSocket{
int bindAndListen(int port){
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))!=0){
perror("bind failed\n");
return -1;
}
if(listen(sockfd,5)!=0){
perror("listen failed\n");
return -1;
}
//printf("server ip:%s",inet_ntoa(servaddr.sin_addr));
return 0;
}
TcpSocket getOneConn(){
while(true){
int connfd;
struct sockaddr_in connaddr;
socklen_t addrsz=sizeof(connaddr);
connfd = accept(sockfd,(sockaddr*)&connaddr,&addrsz);
if(connfd==-1){
perror("invalid connection\n");
continue;
}
printf("new connection\n");
return TcpSocket(connfd,connaddr);
}
}
TcpSocket getConn(){
while(true){
int connfd;
struct sockaddr_in connaddr;
socklen_t addrsz=sizeof(connaddr);
connfd = accept(sockfd,(sockaddr*)&connaddr,&addrsz);
if(connfd==-1){
perror("invalid connection\n");
continue;
}
if(fork()==0){
//in child
printf("new connection\n");
return TcpSocket(connfd,connaddr);
}
//else
//in parent blocked in accept again!
}
}
};
#endif // MYSOCKET_H_INCLUDED