- Set svn:eol-style to native

- Set svn:ignore on directories
Thanks to w3seek for pointing this out.

svn path=/trunk/; revision=18132
This commit is contained in:
Ged Murphy 2005-09-27 23:37:05 +00:00
parent 89effd5ee8
commit 5d41a8d313
8 changed files with 391 additions and 392 deletions

View file

@ -1,104 +1,104 @@
#include <stdio.h> #include <stdio.h>
#include <winsock2.h> #include <winsock2.h>
#include <tchar.h> #include <tchar.h>
#include "chargen.h" #include "chargen.h"
#include "../skelserver/skelserver.h" #include "../skelserver/skelserver.h"
DWORD WINAPI ChargenHandler(VOID* Sock_) DWORD WINAPI ChargenHandler(VOID* Sock_)
{ {
DWORD Retval = 0; DWORD Retval = 0;
SOCKET Sock = (SOCKET)Sock_; SOCKET Sock = (SOCKET)Sock_;
if (!GenerateChars(Sock)) { if (!GenerateChars(Sock)) {
_tprintf(_T("Echo incoming packets failed\n")); _tprintf(_T("Echo incoming packets failed\n"));
Retval = 3; Retval = 3;
} }
_tprintf(_T("Shutting connection down...\n")); _tprintf(_T("Shutting connection down...\n"));
if (ShutdownConnection(Sock)) { if (ShutdownConnection(Sock)) {
_tprintf(_T("Connection is down.\n")); _tprintf(_T("Connection is down.\n"));
} }
else else
{ {
_tprintf(_T("Connection shutdown failed\n")); _tprintf(_T("Connection shutdown failed\n"));
Retval = 3; Retval = 3;
} }
return Retval; return Retval;
} }
BOOL GenerateChars(SOCKET Sock) BOOL GenerateChars(SOCKET Sock)
{ {
int i, int i,
charIndex, /* internal loop */ charIndex, /* internal loop */
loopIndex; /* line loop */ loopIndex; /* line loop */
char ring[END-START]; char ring[END-START];
char *endring; char *endring;
/* fill ring with printable characters */ /* fill ring with printable characters */
for (charIndex=0, i=START; i<=END; charIndex++, i++) for (charIndex=0, i=START; i<=END; charIndex++, i++)
ring[charIndex] = i; ring[charIndex] = i;
/* establish the end character in the ring */ /* establish the end character in the ring */
endring = &ring[charIndex]; endring = &ring[charIndex];
/* where we will start output from */ /* where we will start output from */
loopIndex = 0; loopIndex = 0;
while (1) while (1)
{ {
/* if the loop index is equal to number of chars previously /* if the loop index is equal to number of chars previously
* printed, start the loop from the beginning */ * printed, start the loop from the beginning */
if (loopIndex == END-START) if (loopIndex == END-START)
loopIndex = 0; loopIndex = 0;
/* start printing from char controled by loopIndex */ /* start printing from char controled by loopIndex */
charIndex = loopIndex; charIndex = loopIndex;
for (i=0; i<LINESIZ; i++) for (i=0; i<LINESIZ; i++)
{ {
SendChar(Sock, ring[charIndex]); SendChar(Sock, ring[charIndex]);
/* if current char equal last char, reset */ /* if current char equal last char, reset */
if (ring[charIndex] == *endring) if (ring[charIndex] == *endring)
charIndex = 0; charIndex = 0;
else else
charIndex++; charIndex++;
} }
SendChar(Sock, L'\r'); SendChar(Sock, L'\r');
SendChar(Sock, L'\n'); SendChar(Sock, L'\n');
/* increment loop index to start printing from next char in ring */ /* increment loop index to start printing from next char in ring */
loopIndex++; loopIndex++;
} }
return 0; return 0;
} }
BOOL SendChar(SOCKET Sock, TCHAR c) BOOL SendChar(SOCKET Sock, TCHAR c)
{ {
INT Temp; INT RetVal;
INT SentBytes; INT SentBytes;
SentBytes = 0; SentBytes = 0;
Temp = send(Sock, &c, sizeof(TCHAR), 0); RetVal = send(Sock, &c, sizeof(TCHAR), 0);
if (Temp > 0) { if (RetVal > 0) {
SentBytes += Temp; SentBytes += RetVal;
} }
else if (Temp == SOCKET_ERROR) { else if (RetVal == SOCKET_ERROR) {
return FALSE; return FALSE;
} }
else else
{ {
/* Client closed connection before we could reply to /* Client closed connection before we could reply to
all the data it sent, so quit early. */ all the data it sent, so quit early. */
_tprintf(_T("Peer unexpectedly dropped connection!\n")); _tprintf(_T("Peer unexpectedly dropped connection!\n"));
return FALSE; return FALSE;
} }
_tprintf(("Connection closed by peer.\n")); _tprintf(("Connection closed by peer.\n"));
return TRUE; return TRUE;
} }

View file

@ -1,8 +1,8 @@
#define START 32 #define START 32
#define END 126 #define END 126
#define LINESIZ 72 #define LINESIZ 72
#define BUF 1024 #define BUF 1024
DWORD WINAPI ChargenHandler(VOID* Sock_); DWORD WINAPI ChargenHandler(VOID* Sock_);
BOOL GenerateChars(SOCKET Sock); BOOL GenerateChars(SOCKET Sock);
BOOL SendChar(SOCKET Sock, CHAR c); BOOL SendChar(SOCKET Sock, CHAR c);

View file

@ -1,72 +1,71 @@
#include <stdio.h> #include <stdio.h>
#include <winsock2.h> #include <winsock2.h>
#include <tchar.h> #include <tchar.h>
#include "echo.h" #include "echo.h"
#include "../skelserver/skelserver.h" #include "../skelserver/skelserver.h"
// Handles the incoming data by reflecting it back to the sender. DWORD WINAPI EchoHandler(VOID* Sock_)
DWORD WINAPI EchoHandler(VOID* Sock_) {
{ DWORD Retval = 0;
DWORD Retval = 0; SOCKET Sock = (SOCKET)Sock_;
SOCKET Sock = (SOCKET)Sock_;
if (!EchoIncomingPackets(Sock)) {
if (!EchoIncomingPackets(Sock)) { _tprintf(_T("Echo incoming packets failed\n"));
_tprintf(_T("Echo incoming packets failed\n")); Retval = 3;
Retval = 3; }
}
_tprintf(_T("Shutting connection down...\n"));
_tprintf(_T("Shutting connection down...\n")); if (ShutdownConnection(Sock)) {
if (ShutdownConnection(Sock)) { _tprintf(_T("Connection is down.\n"));
_tprintf(_T("Connection is down.\n")); }
} else
else {
{ _tprintf(_T("Connection shutdown failed\n"));
_tprintf(_T("Connection shutdown failed\n")); Retval = 3;
Retval = 3; }
}
return Retval;
return Retval; }
}
BOOL EchoIncomingPackets(SOCKET Sock)
BOOL EchoIncomingPackets(SOCKET Sock) {
{ TCHAR ReadBuffer[BUF];
TCHAR ReadBuffer[BUF]; INT Temp;
INT Temp; INT ReadBytes;
INT ReadBytes; INT SentBytes;
INT SentBytes;
do {
do { ReadBytes = recv(Sock, ReadBuffer, BUF, 0);
ReadBytes = recv(Sock, ReadBuffer, BUF, 0); if (ReadBytes > 0) {
if (ReadBytes > 0) { _tprintf(_T("Received %d bytes from client\n"), ReadBytes);
_tprintf(_T("Received %d bytes from client\n"), ReadBytes);
SentBytes = 0;
SentBytes = 0; while (SentBytes < ReadBytes) {
while (SentBytes < ReadBytes) { Temp = send(Sock, ReadBuffer + SentBytes,
Temp = send(Sock, ReadBuffer + SentBytes, ReadBytes - SentBytes, 0);
ReadBytes - SentBytes, 0); if (Temp > 0) {
if (Temp > 0) { _tprintf(_T("Sent %d bytes back to client\n"), Temp);
_tprintf(_T("Sent %d bytes back to client\n"), Temp); SentBytes += Temp;
SentBytes += Temp; }
} else if (Temp == SOCKET_ERROR) {
else if (Temp == SOCKET_ERROR) { return FALSE;
return FALSE; }
} else {
else { /* Client closed connection before we could reply to
/* Client closed connection before we could reply to all the data it sent, so quit early. */
// all the data it sent, so quit early. */ _tprintf(_T("Peer unexpectedly dropped connection!\n"));
_tprintf(_T("Peer unexpectedly dropped connection!\n")); return FALSE;
return FALSE; }
} }
} }
} else if (ReadBytes == SOCKET_ERROR) {
else if (ReadBytes == SOCKET_ERROR) { return FALSE;
return FALSE; }
} } while (ReadBytes != 0);
} while (ReadBytes != 0);
_tprintf(("Connection closed by peer.\n"));
_tprintf(("Connection closed by peer.\n")); return TRUE;
return TRUE; }
}

View file

@ -1,4 +1,4 @@
#define BUF 1024 #define BUF 1024
DWORD WINAPI EchoHandler(VOID* Sock_); DWORD WINAPI EchoHandler(VOID* Sock_);
BOOL EchoIncomingPackets(SOCKET Sock); BOOL EchoIncomingPackets(SOCKET Sock);

View file

@ -1,127 +1,127 @@
#include <stdio.h> #include <stdio.h>
#include <winsock2.h> #include <winsock2.h>
#include <tchar.h> #include <tchar.h>
#include "../tcpsvcs.h" #include "../tcpsvcs.h"
#include "skelserver.h" #include "skelserver.h"
DWORD WINAPI StartServer(LPVOID lpParam) DWORD WINAPI StartServer(LPVOID lpParam)
{ {
const TCHAR* HostIP = "127.0.0.1"; const TCHAR* HostIP = "127.0.0.1";
DWORD RetVal; DWORD RetVal;
WSADATA wsaData; WSADATA wsaData;
PMYDATA pData; PMYDATA pData;
pData = (PMYDATA)lpParam; pData = (PMYDATA)lpParam;
if ((RetVal = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0) if ((RetVal = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0)
{ {
_tprintf(_T("WSAStartup() failed : %lu\n"), RetVal); _tprintf(_T("WSAStartup() failed : %lu\n"), RetVal);
return -1; return -1;
} }
SOCKET ListeningSocket = SetUpListener(HostIP, htons(pData->Port)); SOCKET ListeningSocket = SetUpListener(HostIP, htons(pData->Port));
if (ListeningSocket == INVALID_SOCKET) if (ListeningSocket == INVALID_SOCKET)
{ {
_tprintf(_T("error setting up socket\n")); _tprintf(_T("error setting up socket\n"));
return 3; return 3;
} }
printf("Waiting for connections...\n"); printf("Waiting for connections...\n");
while (1) while (1)
{ {
AcceptConnections(ListeningSocket, pData->Service); AcceptConnections(ListeningSocket, pData->Service);
printf("Acceptor restarting...\n"); printf("Acceptor restarting...\n");
} }
WSACleanup(); WSACleanup();
return 0; return 0;
} }
SOCKET SetUpListener(const char* ServAddr, int Port) SOCKET SetUpListener(const char* ServAddr, int Port)
{ {
SOCKET Sock; SOCKET Sock;
SOCKADDR_IN Server; SOCKADDR_IN Server;
DWORD InterfaceAddr = inet_addr(ServAddr); DWORD InterfaceAddr = inet_addr(ServAddr);
if (InterfaceAddr != INADDR_NONE) if (InterfaceAddr != INADDR_NONE)
{ {
Sock = socket(AF_INET, SOCK_STREAM, 0); Sock = socket(AF_INET, SOCK_STREAM, 0);
if (Sock != INVALID_SOCKET) if (Sock != INVALID_SOCKET)
{ {
Server.sin_family = AF_INET; Server.sin_family = AF_INET;
Server.sin_addr.s_addr = InterfaceAddr; Server.sin_addr.s_addr = InterfaceAddr;
Server.sin_port = Port; Server.sin_port = Port;
if (bind(Sock, (SOCKADDR*)&Server, sizeof(SOCKADDR_IN)) != SOCKET_ERROR) if (bind(Sock, (SOCKADDR*)&Server, sizeof(SOCKADDR_IN)) != SOCKET_ERROR)
{ {
listen(Sock, SOMAXCONN); listen(Sock, SOMAXCONN);
return Sock; return Sock;
} }
else else
printf("bind() failed\n"); printf("bind() failed\n");
} }
} }
return INVALID_SOCKET; return INVALID_SOCKET;
} }
VOID AcceptConnections(SOCKET ListeningSocket, LPTHREAD_START_ROUTINE Service) VOID AcceptConnections(SOCKET ListeningSocket, LPTHREAD_START_ROUTINE Service)
{ {
SOCKADDR_IN Client; SOCKADDR_IN Client;
SOCKET Sock; SOCKET Sock;
INT nAddrSize = sizeof(Client); INT nAddrSize = sizeof(Client);
DWORD ThreadID; DWORD ThreadID;
while (1) while (1)
{ {
Sock = accept(ListeningSocket, (SOCKADDR*)&Client, &nAddrSize); Sock = accept(ListeningSocket, (SOCKADDR*)&Client, &nAddrSize);
if (Sock != INVALID_SOCKET) if (Sock != INVALID_SOCKET)
{ {
_tprintf(_T("Accepted connection from %s:%d\n"), _tprintf(_T("Accepted connection from %s:%d\n"),
inet_ntoa(Client.sin_addr), ntohs(Client.sin_port)); inet_ntoa(Client.sin_addr), ntohs(Client.sin_port));
CreateThread(0, 0, Service, (void*)Sock, 0, &ThreadID); CreateThread(0, 0, Service, (void*)Sock, 0, &ThreadID);
} }
else else
{ {
_tprintf(_T("accept() failed\n")); _tprintf(_T("accept() failed\n"));
return; return;
} }
} }
} }
BOOL ShutdownConnection(SOCKET Sock) BOOL ShutdownConnection(SOCKET Sock)
{ {
/* Disallow any further data sends. This will tell the other side /* Disallow any further data sends. This will tell the other side
that we want to go away now. If we skip this step, we don't that we want to go away now. If we skip this step, we don't
shut the connection down nicely. */ shut the connection down nicely. */
if (shutdown(Sock, SD_SEND) == SOCKET_ERROR) if (shutdown(Sock, SD_SEND) == SOCKET_ERROR)
return FALSE; return FALSE;
/* Receive any extra data still sitting on the socket. After all /* Receive any extra data still sitting on the socket. After all
data is received, this call will block until the remote host data is received, this call will block until the remote host
acknowledges the TCP control packet sent by the shutdown above. acknowledges the TCP control packet sent by the shutdown above.
Then we'll get a 0 back from recv, signalling that the remote Then we'll get a 0 back from recv, signalling that the remote
host has closed its side of the connection. */ host has closed its side of the connection. */
while (1) while (1)
{ {
char ReadBuffer[BUF]; char ReadBuffer[BUF];
int NewBytes = recv(Sock, ReadBuffer, BUF, 0); int NewBytes = recv(Sock, ReadBuffer, BUF, 0);
if (NewBytes == SOCKET_ERROR) if (NewBytes == SOCKET_ERROR)
return FALSE; return FALSE;
else if (NewBytes != 0) else if (NewBytes != 0)
_tprintf(_T("FYI, received %d unexpected bytes during shutdown\n"), NewBytes); _tprintf(_T("FYI, received %d unexpected bytes during shutdown\n"), NewBytes);
else else
break; break;
} }
/* Close the socket. */ /* Close the socket. */
if (closesocket(Sock) == SOCKET_ERROR) if (closesocket(Sock) == SOCKET_ERROR)
return FALSE; return FALSE;
return TRUE; return TRUE;
} }

View file

@ -1,7 +1,7 @@
#define BUF 1024 #define BUF 1024
DWORD WINAPI StartServer(LPVOID lpParam); DWORD WINAPI StartServer(LPVOID lpParam);
SOCKET SetUpListener(const char* ServAddr, int Port); SOCKET SetUpListener(const char* ServAddr, int Port);
VOID AcceptConnections(SOCKET ListeningSocket, LPTHREAD_START_ROUTINE Service); VOID AcceptConnections(SOCKET ListeningSocket, LPTHREAD_START_ROUTINE Service);
BOOL EchoIncomingPackets(SOCKET sd); BOOL EchoIncomingPackets(SOCKET sd);
BOOL ShutdownConnection(SOCKET Sock); BOOL ShutdownConnection(SOCKET Sock);

View file

@ -1,57 +1,57 @@
#include <stdio.h> #include <stdio.h>
#include <winsock2.h> #include <winsock2.h>
#include <tchar.h> #include <tchar.h>
#include "tcpsvcs.h" #include "tcpsvcs.h"
#include "skelserver/skelserver.h" #include "skelserver/skelserver.h"
#include "echo/echo.h" #include "echo/echo.h"
#include "chargen/chargen.h" #include "chargen/chargen.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
PMYDATA pData[MAX_THREADS]; PMYDATA pData[MAX_THREADS];
DWORD dwThreadId[MAX_THREADS]; DWORD dwThreadId[MAX_THREADS];
HANDLE hThread[MAX_THREADS]; HANDLE hThread[MAX_THREADS];
INT i; INT i;
/* Create MAX_THREADS worker threads. */ /* Create MAX_THREADS worker threads. */
for( i=0; i<MAX_THREADS; i++ ) for( i=0; i<MAX_THREADS; i++ )
{ {
/* Allocate memory for thread data. */ /* Allocate memory for thread data. */
pData[i] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA)); pData[i] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA));
if( pData == NULL ) if( pData == NULL )
ExitProcess(2); ExitProcess(2);
/* Generate unique data for each thread. */ /* Generate unique data for each thread. */
pData[0]->Port = ECHO_PORT; pData[0]->Port = ECHO_PORT;
pData[0]->Service = EchoHandler; pData[0]->Service = EchoHandler;
pData[1]->Port = CHARGEN_PORT; pData[1]->Port = CHARGEN_PORT;
pData[1]->Service = ChargenHandler; pData[1]->Service = ChargenHandler;
hThread[i] = CreateThread( hThread[i] = CreateThread(
NULL, // default security attributes NULL, // default security attributes
0, // use default stack size 0, // use default stack size
StartServer, // thread function StartServer, // thread function
pData[i], // argument to thread function pData[i], // argument to thread function
0, // use default creation flags 0, // use default creation flags
&dwThreadId[i]); // returns the thread identifier &dwThreadId[i]); // returns the thread identifier
/* Check the return value for success. */ /* Check the return value for success. */
if (hThread[i] == NULL) if (hThread[i] == NULL)
{ {
ExitProcess(i); ExitProcess(i);
} }
} }
/* Wait until all threads have terminated. */ /* Wait until all threads have terminated. */
WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE); WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE);
/* Close all thread handles upon completion. */ /* Close all thread handles upon completion. */
for(i=0; i<MAX_THREADS; i++) for(i=0; i<MAX_THREADS; i++)
{ {
CloseHandle(hThread[i]); CloseHandle(hThread[i]);
} }
return 0; return 0;
} }

View file

@ -1,13 +1,13 @@
#define ECHO_PORT 7 #define ECHO_PORT 7
#define CHARGEN_PORT 19 #define CHARGEN_PORT 19
#define DAYTIME_PORT 13 #define DAYTIME_PORT 13
#define DISCARD_PORT 9 #define DISCARD_PORT 9
#define QOTD_PORT 17 #define QOTD_PORT 17
#define MAX_THREADS 2 #define MAX_THREADS 2
#define BUF_SIZE 255 #define BUF_SIZE 255
typedef struct _MyData { typedef struct _MyData {
INT Port; INT Port;
LPTHREAD_START_ROUTINE Service; LPTHREAD_START_ROUTINE Service;
} MYDATA, *PMYDATA; } MYDATA, *PMYDATA;