killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Mon Dec 15, 2008 2:33 am Post subject: [c]winsock2 very basic TCP server |
|
|
As of right now, I'm learning WS2. The reason for this is because the project I'm working on requires communication with a server. The project is going to be done in all c and using nothing but the win32 API. For those that know a few things about WS2, could you please comment on if I'm doing something wrong or inefficient? Even though it's basic, you can't really do anything if the basics are messed up since it's the basics that makes things run.
*Code has been removed and or changed because of size. Also, things like parsing and code that doesn't have anything to do with WS2 has been removed.*
| Code: |
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <process.h>
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
void _cdecl ServerThread(LPVOID lpParam);
void _cdecl ClientThread(LPVOID lpParam);
void _cdecl CheckThread(LPVOID lpParam);
int connections = 0;
BOOL isAccepting = FALSE;
BOOL endThread = FALSE;
int main(void)
{
HANDLE hServerThread;
hServerThread = (HANDLE)_beginthread(ServerThread, 0, 0);
if (hServerThread == NULL)
printf("There was an error creating the thread. Program will now terminate.\n");
else
{
printf("EOF to exit\n");
while(!endThread)
{
if (getchar() == EOF)
endThread = TRUE;
}
WaitForSingleObject(hServerThread, INFINITE);
}
return 0;
}
void _cdecl ServerThread(LPVOID lpParam)
{
int iResult = 0;
char ip[16] = "192.168.1.1";
unsigned short port = 92888;
int MAX_CONNECTIONS = 255;
const char fileName[] = "serverinfo.srv";
FILE* ipRead;
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in addrin;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
endThread = TRUE;
printf("There was an error initializing ws2_32.dll\n");
_endthread();
}
if (wsaData.wVersion != MAKEWORD(2,2))
{
endThread = TRUE;
printf("Wrong version of ws2_32.dll. Version 2.2 is needed.\n");
_endthread();
}
ipRead = fopen(fileName, "r");
if (ipRead == NULL)
{
printf("%s was not found.\n", fileName);
printf("Creating file...\n");
ipRead = fopen(&fileName, "w");
fprintf(ipRead, "%s %hd %d", ip, port, MAX_CONNECTIONS);
fclose(ipRead);
printf("File created.\n");
}
else
{
fscanf(ipRead, "%s %hd %d", ip, &port, &MAX_CONNECTIONS);
fclose(ipRead);
}
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET)
{
endThread = TRUE;
printf("Error Code: %d\n", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
_endthread();
}
addrin.sin_addr.s_addr = inet_addr(ip);
addrin.sin_family = AF_INET;
addrin.sin_port = htons(port);
iResult = bind(serverSocket, (struct sockaddr*)&addrin, sizeof(addrin));
if (iResult == SOCKET_ERROR)
{
endThread = TRUE;
printf("Error Code: %d\n", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
_endthread();
}
iResult = listen(serverSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR)
{
endThread = TRUE;
printf("Error Code: %d\n", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
_endthread();
}
_beginthread(CheckThread, 0, (LPVOID)serverSocket);
while (endThread != TRUE)
{
while (connections != MAX_CONNECTIONS && endThread != TRUE)
{
isAccepting = TRUE;
clientSocket = accept(serverSocket, NULL, 0);
if (clientSocket == INVALID_SOCKET)
{
isAccepting = FALSE;
endThread = TRUE;
printf("Error Code: %d\n", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
_endthread();
}
isAccepting = FALSE;
connections++;
_beginthread(ClientThread,0,(LPVOID)clientSocket);
}
Sleep(1000);
}
closesocket(serverSocket);
WSACleanup();
_endthread();
}
void _cdecl ClientThread(LPVOID lpParam)
{
int iResult = 0;
char buff[255] = { 0 };
SOCKET clientSocket = (SOCKET)lpParam;
while(1)
{
iResult = recv(clientSocket, buff, sizeof(buff), 0);
if (iResult == SOCKET_ERROR)
{
connections--;
break;
}
if (!strcmp(buff, "QUIT"))
{
strcpy(buff, "BYE");
send(clientSocket, buff, strlen(buff) + 1, 0);
connections--;
break;
}
else
{
strcpy(buff, "Send QUIT :-S\r\n");
send(clientSocket, buff, strlen(buff) + 1, 0);
}
}
closesocket(clientSocket);
_endthread();
}
void _cdecl CheckThread(LPVOID lpParam)
{
SOCKET socket = (SOCKET)lpParam;
while (!endThread)
Sleep(1000);
if (isAccepting)
{
closesocket(socket);
}
_endthread();
}
|
|
|