forked from mengning/nezha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketwrapper.c
207 lines (197 loc) · 6.46 KB
/
socketwrapper.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/********************************************************************/
/* Copyright (C) SSE-USTC, 2012 */
/* */
/* FILE NAME : socketwraper.c */
/* PRINCIPAL AUTHOR : Mengning */
/* SUBSYSTEM NAME : network */
/* MODULE NAME : socketwraper */
/* LANGUAGE : C */
/* TARGET ENVIRONMENT : Linux */
/* DATE OF FIRST RELEASE : 2012/12/18 */
/* DESCRIPTION : the interface to Linux system(socket) */
/********************************************************************/
/*
* Revision log:
*
* Created by Mengning,2012/12/18
*
*/
#include"socketwrapper.h"
#include<stdio.h>
#include<stdlib.h>
#include<arpa/inet.h> /* internet socket */
#include<string.h>
int sockfd = -1;
/* private macro */
#define PrepareSocket(addr,port) \
struct sockaddr_in serveraddr; \
struct sockaddr_in clientaddr; \
socklen_t addr_len = sizeof(struct sockaddr); \
serveraddr.sin_family = AF_INET; \
serveraddr.sin_port = htons(port); \
serveraddr.sin_addr.s_addr = inet_addr(addr); \
memset(&serveraddr.sin_zero, 0, 8); \
sockfd = socket(PF_INET,SOCK_STREAM,0); \
if(sockfd == -1) \
{ \
fprintf(stderr,"Socket Error,%s:%d\n", \
__FILE__,__LINE__); \
close(sockfd); \
return -1; \
}
#define InitServer() \
int ret = bind( sockfd, \
(struct sockaddr *)&serveraddr, \
sizeof(struct sockaddr)); \
if(ret == -1) \
{ \
fprintf(stderr,"Bind Error,%s:%d\n", \
__FILE__,__LINE__); \
close(sockfd); \
exit(-1); \
} \
listen(sockfd,MAX_CONNECT_QUEUE);
#define InitClient() \
int ret = connect(sockfd, \
(struct sockaddr *)&serveraddr, \
sizeof(struct sockaddr)); \
if(ret == -1) \
{ \
fprintf(stderr,"Connect Error,%s:%d\n", \
__FILE__,__LINE__); \
exit(-1); \
}
/*
* InitServer
* input : addr - server ip address
* input : port - server port to open
* output : None
* in/out : None
* return : if SUCCESS return 0
* : if FAILURE return (-1)
*/
int InitializeNetService(char * addr,short int port)
{
PrepareSocket(addr,port);
InitServer();
return 0;
}
int ShutdownNetService()
{
if(sockfd == -1)
{
return -1;
}
close(sockfd);
return 0;
}
/*
* OpenRemoteService - Only used in Client side,it connects Server.
* input : addr - server ip address
* input : port - server port to open
* output : None
* in/out : None
* return : if SUCCESS return tServiceHandler
* : if FAILURE return (-1)
*/
tServiceHandler OpenRemoteService(char * addr,short int port)
{
PrepareSocket(addr,port);
InitClient();
return (tServiceHandler)sockfd;
}
/*
* CloseRemoteService - Only used in Client side,it terminate connection.
* input : h - it MUST BE what OpenRemoteService returns.
* output : None
* in/out : None
* return : if SUCCESS return 0
* : if FAILURE return (-1)
*/
int CloseRemoteService(tServiceHandler h)
{
if(h <= 0)
{
return -1;
}
close(h);
return 0;
}
/*
* ServiceStart - Only used in Sever side,when client connects it return.
* input : None
* output : None
* in/out : None
* return : if SUCCESS return tServiceHandler
* : if FAILURE return (-1)
*/
tServiceHandler ServiceStart()
{
struct sockaddr_in clientaddr;
socklen_t addr_len = sizeof(struct sockaddr);
int newfd = accept( sockfd,(struct sockaddr *)&clientaddr,&addr_len);
if(newfd == -1)
{
fprintf(stderr,"Accept Error,%s:%d\n", __FILE__,__LINE__);
}
return (tServiceHandler)newfd;
}
/*
* ServiceStop - Only used in Sever side,when client connects it return.
* input : h - it MUST BE what ServiceStart OR returns.
* output : None
* in/out : None
* return : if SUCCESS return 0
* : if FAILURE return (-1)
*/
int ServiceStop(tServiceHandler h)
{
if(h <= 0)
{
return -1;
}
close(h);
return 0;
}
/*
* SendData - send data to peer side
* input : h - it MUST BE what ServiceStart or OpenRemoteService returns.
* input : pBuf - point to data
* input : BufSize - the length of data bytes
* output : None
* in/out : None
* return : if SUCCESS return BufSize sended
* : if FAILURE return (-1)
* : if Connection Terminals return 0
*/
int SendData(tServiceHandler h,char * pBuf,int BufSize)
{
int ret = send(h,pBuf,BufSize,0);
if(ret < 0 || ret != BufSize)
{
fprintf(stderr,"Send Error,%s:%d\n",__FILE__,__LINE__);
return -1;
}
return ret;
}
/*
* SendData - send data to peer side
* input : h - it MUST BE what ServiceStart or OpenRemoteService returns.
* in/out : pBuf - point to memory that will store recved data
* in/out : pBufSize - the max length of data to recv
* return : if SUCCESS return BufSize recved
* : if FAILURE return (-1)
* : if Connection Terminals return 0
*/
int RecvData(tServiceHandler h,char * pBuf,int *pBufSize)
{
int ret = recv(h,pBuf,*pBufSize,0);
if(ret < 0)
{
fprintf(stderr,"Recv Error,%s:%d\n",__FILE__,__LINE__);
*pBufSize = 0;
return -1;
}
*pBufSize = ret;
return ret;
}