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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/*************************************************************************************************\
*
* SOCKETS.C
*
* This file contains routines used for establishing Sockets connections.
*
\*************************************************************************************************/
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include "gutils.h"
/* ---- Local variables and #defines ----
*/
WSADATA WSAData;
#define MAX_PENDING_CONNECTS 4 /* The backlog allowed for listen() */
static PCHAR DBG_WSAERRORTEXT = "%s failed at line %d in %s: Error %d\n";
#define WSAERROR(func) \
// ERROR(( DBG_WSAERRORTEXT, func, __LINE__, __FILE__, WSAGetLastError() ))
/* Error message macro:
*/
#ifdef SOCKETS
#undef SOCKETS
#endif
#define SOCKETS( args ) DBGMSG( DBG_SOCKETS, args )
/* ---- Local function prototypes ----
*/
/* ---- Function definitions ----
*/
/****************************************************************************\
*
* FUNCTION: FillAddr(HWND, PSOCKADDR_IN, LPSTR)
*
* PURPOSE: Retrieves the IP address and port number.
*
* COMMENTS:
* This function is called in two conditions.
* 1.) When a client is preparing to call connect(), or
* 2.) When a server host is going to call bind(), listen() and
* accept().
* In both situations, a SOCKADDR_IN structure is filled.
* However, different fields are filled depending on the condition.
*
* ASSUMPTION:
* bConnect determines if the socket address is being set up for a listen()
* (bConnect == TRUE) or a connect() (bConnect == FALSE)
*
*
*\***************************************************************************/
BOOL
FillAddr(
HWND hWnd,
PSOCKADDR_IN psin,
LPSTR pServerName
)
{
DWORD dwSize;
PHOSTENT phe;
char szTemp[200];
CHAR szBuff[80];
psin->sin_family = AF_INET;
/*
** If we are setting up for a listen() call (pServerName == NULL),
** fill servent with our address.
*/
if (!pServerName)
{
/*
** Retrieve my ip address. Assuming the hosts file in
** in %systemroot%/system/drivers/etc/hosts contains my computer name.
*/
dwSize = sizeof(szBuff);
GetComputerName(szBuff, &dwSize);
CharLowerBuff( szBuff, dwSize );
}
/* gethostbyname() fails if the remote name is in upper-case characters!
*/
else
{
strcpy( szBuff, pServerName );
CharLowerBuff( szBuff, strlen( szBuff ) );
}
phe = gethostbyname(szBuff);
if (phe == NULL) {
wsprintf( szTemp, "%d is the error. Make sure '%s' is"
" listed in the hosts file.", WSAGetLastError(), szBuff );
MessageBox(hWnd, szTemp, "gethostbyname() failed.", MB_OK);
return FALSE;
}
memcpy((char FAR *)&(psin->sin_addr), phe->h_addr, phe->h_length);
return TRUE;
}
/* SocketConnect
*
* The counterpart to SocketListen.
* Creates a socket and initializes it with the supplied TCP/IP
* port address, then connects to a listening server.
* The returned socket can be used to send() and recv() data.
*
* Parameters: TCPPort - The port to use.
* pSocket - A pointer to a SOCKET, which will be filled in
* if the call succeeds.
*
* Returns: TRUE if successful.
*
*
* Created 16 November 1993 (andrewbe)
*
*/
BOOL SocketConnect( LPSTR pstrServerName, u_short TCPPort, SOCKET *pSocket )
{
SOCKET Socket;
SOCKADDR_IN dest_sin; /* DESTination Socket INternet */
/* Create a socket:
*/
Socket = socket( AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
WSAERROR( "socket()");
return FALSE;
}
if (!FillAddr( NULL, &dest_sin, pstrServerName ) )
{
return FALSE;
}
dest_sin.sin_port = htons( TCPPort );
/* Someone must be listen()ing for this to succeed:
*/
if (connect( Socket, (PSOCKADDR)&dest_sin, sizeof( dest_sin)) == SOCKET_ERROR)
{
closesocket( Socket );
WSAERROR("connect()");
MessageBox(NULL,
"ERROR: Could not connect the socket. "
"It may be that the hardcoded Sleep() value "
"on the caller's side is not long enough.",
"Video Conferencing Prototype", MB_OK);
return FALSE;
}
*pSocket = Socket;
return TRUE;
}
/* SocketListen
*
* The counterpart to SocketConnect.
* Creates a socket and initializes it with the supplied TCP/IP
* port address, then listens for a connecting client.
* The returned socket can be used to send() and recv() data.
*
* Parameters: TCPPort - The port to use.
* pSocket - A pointer to a SOCKET, which will be filled in
* if the call succeeds.
*
* Returns: TRUE if successful.
*
*
* Created 16 November 1993 (andrewbe)
*
*/
BOOL SocketListen( u_short TCPPort, SOCKET *pSocket )
{
SOCKET Socket;
SOCKADDR_IN local_sin; /* Local socket - internet style */
SOCKADDR_IN acc_sin; /* Accept socket address - internet style */
int acc_sin_len; /* Accept socket address length */
/* Create a socket:
*/
Socket = socket( AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
WSAERROR( "socket()");
return FALSE;
}
/*
** Retrieve the IP address and TCP Port number
*/
if (!FillAddr(NULL, &local_sin, NULL ))
{
return FALSE;
}
/*
** Associate an address with a socket. (bind)
*/
local_sin.sin_port = htons( TCPPort );
if (bind( Socket, (struct sockaddr FAR *)&local_sin, sizeof(local_sin)) == SOCKET_ERROR)
{
WSAERROR( "bind()" );
return FALSE;
}
if (listen( Socket, MAX_PENDING_CONNECTS ) == SOCKET_ERROR)
{
WSAERROR( "listen()" );
return FALSE;
}
acc_sin_len = sizeof(acc_sin);
Socket = accept( Socket, (struct sockaddr *)&acc_sin, (int *)&acc_sin_len );
if (Socket == INVALID_SOCKET)
{
WSAERROR( "accept()" );
return FALSE;
}
*pSocket = Socket;
return TRUE;
}
|