// Client.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Client.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
if (!AfxSocketInit(&wsaData))
{
AfxMessageBox(CString("Error Unit"));
return GetLastError();
}
CSocket ClientSocket;
UINT port = 12345;
ClientSocket.Create();
SOCKADDR_IN sin;
LPHOSTENT hostEnt;
hostEnt = gethostbyname("localhost");
if(!hostEnt)
{
printf("Unable to collect gethostbyname\n");
WSACleanup();
return 1;
}
sin.sin_family = PF_INET;
sin.sin_port = htons(port);
sin.sin_addr = *((LPIN_ADDR)*hostEnt->h_addr_list);
ClientSocket.Connect((SOCKADDR*)(&sin), sizeof(sin));
char MessageBuffer[1024] = {0};
std::cout << "Input: ";
gets_s(MessageBuffer, sizeof(char)*1024);
ClientSocket.Send(MessageBuffer, 1024);
char ReceiveBuffer[1024] = {0};
ClientSocket.Receive(ReceiveBuffer, sizeof(char)*1024, 0);
std::cout << "Message from server: " << ReceiveBuffer << std::endl;
ClientSocket.Close();
WSACleanup();
_getch();
}
}
else
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}
return nRetCode;
}
// Server.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Server.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
UINT __cdecl Server(LPVOID);
// The one and only application object
unsigned int ClientCount = 0;
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
if (!AfxSocketInit(&wsaData))
{
AfxMessageBox(CString("Error Unit"));
return GetLastError();
}
CSocket ServerSocket;
UINT port = 12345;
ServerSocket.Create(port);
SOCKADDR_IN sin;
sin.sin_family = PF_INET;
sin.sin_port = htons(port);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
ServerSocket.Bind((SOCKADDR*)(&sin), sizeof(sin));
ServerSocket.Listen(3);
while (TRUE)
{
CWinThread* thread = new CWinThread();
thread = AfxBeginThread(&Server, (LPVOID)ServerSocket.m_hSocket);
::WaitForSingleObject(thread->m_hThread, INFINITE);
}
ServerSocket.Close();
WSACleanup();
_getch();
}
}
else
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}
return nRetCode;
}
UINT __cdecl Server(LPVOID pParam)
{
CSocket ClientSocket;
CSocket ServerSocket;
ServerSocket.Attach((SOCKET)pParam);
ServerSocket.Accept(ClientSocket, NULL, NULL);
char MessageBuffer[1024] = {0};
ClientSocket.Receive(MessageBuffer, sizeof(char)*1024, 0);
std::cout << "Message from #" << ++ClientCount << " client: " << MessageBuffer << std::endl;
ClientSocket.Send(MessageBuffer, sizeof(char)*1024, 0);
ClientSocket.Close();
AfxEndThread(0, TRUE);
return 0;
}