-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocket.cpp
More file actions
234 lines (200 loc) · 5.98 KB
/
Socket.cpp
File metadata and controls
234 lines (200 loc) · 5.98 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// Socket.cpp: implementation of the CMySocket class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "Socket.h"
#define MAX_LEN 256
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMySocket::CMySocket()
{
}
CMySocket::~CMySocket()
{
}
/////////////////////////////////////////////////////////////////////////////
//
// socketErrorExit
//
// Description
// Print supplied error message and exit the program.
//
// Parameters
// szError - Error message text
//
void CMySocket::socketErrorExit(char *szError)
{
printf("Socket error: %s\n", szError);
}
/////////////////////////////////////////////////////////////////////////////
//
// DoSocketListen
//
// Description
// Code to create, bind and listen to socket; Originally from [email protected]
//
// Parameters
// portnum - local port to listen for connections
//
// Return
// Socket created on success, -1 on error.
//
#define MAXHOSTNAME 1024
SOCKET CMySocket::DoSocketListen(unsigned short portnum)
{
char myname[MAXHOSTNAME+1];
SOCKET s;
struct sockaddr_in sa;
struct hostent *hp;
memset(&sa, 0, sizeof(struct sockaddr_in)); /* clear our address */
gethostname(myname, MAXHOSTNAME); /* who are we? */
hp= gethostbyname(myname); /* get our address info */
if (hp == NULL) /* we don't exist !? */
return(-1);
sa.sin_family= hp->h_addrtype; /* this is our host address */
sa.sin_port= htons(portnum); /* this is our port number */
if ((s= socket(AF_INET, SOCK_STREAM, 0)) < 0) /* create socket */
return(-1);
if (bind(s,(struct sockaddr *)&sa,sizeof(struct sockaddr_in)) < 0) {
closesocket(s);
return(-1); /* bind address to socket */
}
listen(s, SOMAXCONN); /* max # of queued connects */
return(s);
}
/////////////////////////////////////////////////////////////////////////////
//
// DoSocketAccept
//
// Description
// Performs an accept() on the supplied socket.
//
// Parameters
// s - listen()'ing socket to call accept() on.
//
// Return
// Returns the accept()'d socket on success, -1 on error.
//
SOCKET CMySocket::DoSocketAccept(SOCKET s)
{
SOCKET t; /* socket of connection */
int namelen = 0;
if ((t = accept(s,NULL,NULL)) < 0) /* accept connection if there is one */
return(-1);
/*
memset(&connectedIP, '\000', sizeof(connectedIP));
namelen = sizeof(connectedIP);
int ret = getpeername(t, (struct sockaddr *)&connectedIP, &namelen);
if (ret == SOCKET_ERROR) {
int error = WSAGetLastError();
}
*/
// MessageBox(NULL, inet_ntoa(connectedIP.sin_addr), "Connected To", MB_OK);
return(t);
}
/////////////////////////////////////////////////////////////////////////////
//
// DoSocketConnect
//
// Description
// Performs a generic socket() and connect()
//
// Parameters
// hostname - host to connect() to.
// portnum - port number for connect().
//
SOCKET CMySocket::DoSocketConnect(char *hostname, unsigned short portnum)
{
struct sockaddr_in sa;
struct hostent *hp;
SOCKET s;
if ((hp= gethostbyname(hostname)) == NULL) { /* do we know the host's */
#ifdef WIN32
SetLastError(WSAECONNREFUSED);
#else
fprintf(stderr, "cannot find host %s", hostname);
#endif
return(-1); /* no */
}
memset(&sa,0,sizeof(sa));
memcpy((char *)&sa.sin_addr,hp->h_addr,hp->h_length); /* set address */
sa.sin_family= hp->h_addrtype;
sa.sin_port= htons((u_short)portnum);
if ((s= socket(hp->h_addrtype,SOCK_STREAM,0)) < 0) /* get socket */
return(-1);
int optval = 10000;
setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (const char *)&optval, sizeof(optval));
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char *)&optval, sizeof(optval));
if (connect(s,(struct sockaddr *)&sa,sizeof sa) < 0) { /* connect */
#ifdef WIN32
long err = WSAGetLastError();
#endif
closesocket(s);
return(-1);
}
return(s);
}
/////////////////////////////////////////////////////////////////////////////
//
// initWinsockLib
//
// Description
// Boiler-plate Winsock setup function
//
// Parameters
//
void CMySocket::initWinsockLib(void)
{
#ifdef WIN32
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
socketErrorExit("cannot find winsock.dll");
exit(1);
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
/* Tell the user that we couldn't find a useable */
/* winsock.dll. */
socketErrorExit("winsock.dll is an old version");
exit(2);
}
#endif
}
/////////////////////////////////////////////////////////////////////////////
//
// CheckSocketError
//
// Description
// Checks the iError input for SOCKET_ERROR, if an error exists, print
// the supplied szMessage and exit the program
//
// Parameters
//
void CMySocket::CheckSocketError(int iError, char *szMessage)
{
char szErrMessage[MAX_LEN];
if (iError == SOCKET_ERROR) {
sprintf(szErrMessage, "%s\n", szMessage);
socketErrorExit(szErrMessage);
return;
}
}