-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysocket.cpp
More file actions
87 lines (76 loc) · 2.07 KB
/
mysocket.cpp
File metadata and controls
87 lines (76 loc) · 2.07 KB
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
#include "mysocket.h"
MySocket::MySocket(int arg_skfd, struct sockaddr * arg_sa):skfd(arg_skfd),BUFFSIZE(100){
bcopy((char *)arg_sa, (char *)&sa, sizeof(struct sockaddr));
buffer = new char[BUFFSIZE];
}
MySocket::MySocket(int arg_skfd):skfd(arg_skfd),BUFFSIZE(100){
buffer = new char[BUFFSIZE];
}
int MySocket::myread(string& rbuff){
int res=0;
int n=0;
char buffer[BUFFSIZE];
do{
n = read(skfd, buffer, BUFFSIZE);
res+=n;
rbuff.append(buffer, n);
}while(n==BUFFSIZE);
return res;
}
int MySocket::mywrite(char* wbuff, int len){
if(len>100){
cout<<"len > 100"<<endl;
}else{
}
int res=0;
int n=1;
int bsize = len;
int writesize = 0;
int flag;
//res = getline(skfd, rbuff); getline 参数是一个fstream类, 然而skfd是int! 有什么通过int构建stream类的函数吗?
do{
flag = res+BUFFSIZE >bsize;
if(flag){
writesize = bsize-res;
}else{
writesize = BUFFSIZE;
}
//cout<<bsize<<";"<<res<<endl;
//cout<<writesize<<endl;
//memcpy(buffer,wbuff+res,writesize);
write(skfd, wbuff+res, writesize);
res+=writesize;
n++;
}while(!flag);
return res;
}
int MySocket::mywrite(string& wbuff){
int res=0;
int n=1;
int bsize = wbuff.size();
const char * wbuff_str = wbuff.c_str();
int writesize = 0;
int flag;
//res = getline(skfd, rbuff); getline 参数是一个fstream类, 然而skfd是int! 有什么通过int构建stream类的函数吗?
do{
flag = res+BUFFSIZE >bsize;
if(flag){
writesize = bsize-res;
}else{
writesize = BUFFSIZE;
}
if(writesize>BUFFSIZE){
cout<<bsize<<";"<<res<<";"<<endl;
}
//cout<<writesize<<endl;
memcpy(buffer,wbuff_str+res,writesize);
write(skfd, buffer, writesize);
res+=writesize;
n++;
}while(!flag);
return res;
}
MySocket::~MySocket(){
close(skfd);
delete[] buffer;//?? free in valid next size;
}