-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMySocket.cpp
More file actions
159 lines (138 loc) · 3.17 KB
/
Copy pathCMySocket.cpp
File metadata and controls
159 lines (138 loc) · 3.17 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
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
//
#include "CMySocket.h"
int MySocket::TcpConnect(string strIP,unsigned int nPort)
{
int sockfd;
struct sockaddr_in dst_addr;
sockfd = socket(AF_INET,SOCK_STREAM,0);
dst_addr.sin_family = AF_INET;
dst_addr.sin_port = htons(nPort);
dst_addr.sin_addr.s_addr = inet_addr(strIP.c_str());
if (connect(sockfd,(struct sockaddr *)&dst_addr,sizeof(dst_addr)) == -1)
{
//connect error
ERROR_EXIT("connect fail!");
}
DEBUG_PRINT("connect success\n");
return sockfd;
}
int MySocket::TcpListen(unsigned int nPort)
{
int listenfd;
//socklen_t len;
struct sockaddr_in servaddr;
listenfd = socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(nPort);
if (bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) == -1)
{
//bind error
ERROR_EXIT("bind fail!");
}
if (listen(listenfd,LISTENQ) == -1)
{
//listen error
ERROR_EXIT("listen fail!");
}
DEBUG_PRINT("listen success\n");
return listenfd;
}
int MySocket::TcpAccept(int listenfd)
{
struct sockaddr_in cliaddr;
socklen_t clilen;
int accfd;
//cliaddr����Զ˵�������Ϣ
if ((accfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen)) == -1)
{
//accept error
}
return accfd;
}
int MySocket::TcpSend(int sockfd, char *ptr, int size)
{
char szDst[MAX_SEND_BUF];
memset(szDst,'\0',MAX_SEND_BUF);
int nLengh = setHeadLengh(szDst,ptr,size);
if (write(sockfd, szDst, nLengh) != nLengh)
{
//write error
printf("write error[%s]\n",ptr);
//exit(-1);
}
printf("send datta success [%s]\n",szDst);
return ERROR_NO_ERROR;
}
int MySocket::tcpRecv(int sockfd, char *ptr, int size)
{
ssize_t n,nCount = 0;
//char buf[MAXLINE];
//memset(ptr,'\0',size);
char *pCur = ptr;
printf("%d byte need to read\n",size);
while (size > 0)
{
printf("-\n");
if ((n = read(sockfd,pCur,size)) > 0)
{
printf("recv:[%d] byte\n",n);
printf("recv:[%s]\n",pCur);
size = size -n;
pCur = pCur + n;
nCount += n;
//printf("recv %d byte\n",n);
}
}
return nCount;
}
int MySocket::TcpRecv(int sockfd, char *ptr, int size)
{
printf("begin TcpRecv\n");
char szHead[HEAD_SIZE+1];
memset(szHead,'\0',HEAD_SIZE+1);
int n = tcpRecv(sockfd,szHead,int(HEAD_SIZE));
printf("sz head [%s]\n",szHead);
int nLengh = 0;
for (int i = HEAD_SIZE-1;i>=0;--i)
{
nLengh = nLengh * MODE_NUM + int(szHead[i] - '0');
}
printf("head len %d\n",nLengh);
n = tcpRecv(sockfd,ptr,nLengh-1);
DEBUG_PRINT("recv [%s]\n",ptr);
return n;
}
int MySocket::TcpClose(int sockfd)
{
return close(sockfd);
}
int MySocket::setHeadLengh(char *dst,const char *src,const int srcSize)
{
printf("comput head lengh:\n");
int nLengh = srcSize;
int nMode;
for (int n=0;n<HEAD_SIZE;++n)
{
nMode = 0;
if (nLengh != 0)
{
nMode = nLengh % MODE_NUM;
nLengh /= MODE_NUM;
}
dst[n] = nMode + '0';
//printf("--->%c\n",dst[n]);
}
dst[HEAD_SIZE] = '\n';
printf("\n");
printf("dst len [%d]\n",strlen(dst));
strncat(dst,src,srcSize);
printf("after set head [%s]\n",dst);
//printf("dst[0]")
// for (int i = 0;i<HEAD_SIZE;++i)
// {
// printf("dst[%d] -->%d\n",i,dst[i]);
// }
return nLengh + HEAD_SIZE;
}