-
Notifications
You must be signed in to change notification settings - Fork 42
/
SocketWrapper.c
executable file
·181 lines (145 loc) · 3.95 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
/**********************************************************************
* File: SocketWrapper.cpp
* Purpose: Provides TCP/IP communication using sockets.
* Author: Kevin Dinkel
**********************************************************************/
/***************************************************
* Section: Includes
**************************************************/
#include "SocketWrapper.h"
/***************************************************
* Function: socketCreate()
* Purpose: Creates a socket for IPv4, TCP/IP.
* Returns: The socket_desc on success, -1 on failure.
**************************************************/
int socketCreate()
{
int socket_desc;
int optval = 1;
/*
// Options:
// 1) Address Family: AF_INET (IPv4)
// 2) Type: SOCK_STREAM (TCP), SOCK_DGRAM (UDP)
// 3) Protocol: IPPROTO_IP = 0 (IP Protocol)
*/
if( (socket_desc = socket(AF_INET , SOCK_STREAM , IPPROTO_IP)) < 0 )
{
return errno;
}
/* Make sure we can reuse this port a second time on a crash: */
setsockopt(socket_desc, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
return socket_desc;
}
int socketBind(int socket_desc, int port)
{
struct sockaddr_in server;
server.sin_addr.s_addr = htonl(INADDR_ANY); /* connect to any ip address associated with me, the server */
server.sin_family = AF_INET;
server.sin_port = htons( port ); /* convert port number to standard network format */
if(bind(socket_desc, (struct sockaddr*)&server, sizeof(server)) < 0)
{
return errno;
}
return 0;
}
int socketListen(int socket_desc, int max_num_clients)
{
if(listen(socket_desc, max_num_clients) < 0)
{
return errno;
}
return 0;
}
int socketAccept(int socket_desc)
{
int connected_socket_desc;
if( (connected_socket_desc = accept(socket_desc, (struct sockaddr*)NULL, NULL)) < 0 )
{
return errno;
}
return connected_socket_desc;
}
int socketConnect(int socket_desc, char* ip_address, int port)
{
struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr(ip_address); /* convert ip string to long */
server.sin_family = AF_INET;
server.sin_port = htons( port ); /* convert port number to standard network format */
if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
{
return errno;
}
return 0;
}
int socketDisconnect(int socket_desc)
{
return close(socket_desc);
}
int socketPoll(int socket_desc, int usecs)
{
/*
// Wait for data in receive buffer or until time out before returning
// Return number of bytes recieved when receive buffer has data
// Return 0 on a timeoue
// Return -1 on error
*/
int ret;
struct pollfd pfd;
struct timespec req;
pfd.fd = socket_desc;
pfd.events = POLLIN;
req.tv_sec = usecs / 1000000;
req.tv_nsec = (usecs % 1000000) * 1000L;
if( (ret = ppoll(&pfd, 1, &req, NULL)) <= 0 )
{
return ret;
}
return ret;
}
int socketSend(int socket_desc, char* message, int length)
{
if( length < 0 )
length = strlen(message);
if( send(socket_desc , message, length, 0) < 0)
{
return errno;
}
return 0;
}
int socketSend_nowait(int socket_desc, char* message, int length)
{
if( length < 0 )
length = strlen(message);
if( send(socket_desc, message, length, MSG_DONTWAIT) < 0)
{
return errno;
}
return 0;
}
int socketReceive(int socket_desc, char* server_reply, int length)
{
int ret;
if( (ret = recv(socket_desc, server_reply , length , 0)) <= 0) /* len of zero means connection closed */
{
return errno;
}
return ret;
}
int socketReceive_nowait(int socket_desc, char* server_reply, int length)
{
int ret;
if( (ret = recv(socket_desc, server_reply , length , MSG_DONTWAIT)) <= 0) /* len of zero means connection closed */
{
return errno;
}
return ret;
}
int socketReceive_timeout(int socket_desc, char* server_reply, int length, int usecs)
{
int ret;
if( (ret = socketPoll(socket_desc, usecs)) <= 0 )
{
return errno;
}
return socketReceive_nowait( socket_desc, server_reply, length);
}