Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmet-tezel authored Dec 12, 2020
1 parent 1fa56a7 commit b45ad3c
Show file tree
Hide file tree
Showing 10 changed files with 725 additions and 0 deletions.
31 changes: 31 additions & 0 deletions SocketClient/SocketClient.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30717.126
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SocketClient", "SocketClient\SocketClient.vcxproj", "{7D219526-4120-4C7C-BC32-07FC3AA998E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7D219526-4120-4C7C-BC32-07FC3AA998E1}.Debug|x64.ActiveCfg = Debug|x64
{7D219526-4120-4C7C-BC32-07FC3AA998E1}.Debug|x64.Build.0 = Debug|x64
{7D219526-4120-4C7C-BC32-07FC3AA998E1}.Debug|x86.ActiveCfg = Debug|Win32
{7D219526-4120-4C7C-BC32-07FC3AA998E1}.Debug|x86.Build.0 = Debug|Win32
{7D219526-4120-4C7C-BC32-07FC3AA998E1}.Release|x64.ActiveCfg = Release|x64
{7D219526-4120-4C7C-BC32-07FC3AA998E1}.Release|x64.Build.0 = Release|x64
{7D219526-4120-4C7C-BC32-07FC3AA998E1}.Release|x86.ActiveCfg = Release|Win32
{7D219526-4120-4C7C-BC32-07FC3AA998E1}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {130D2970-1500-4AA0-BABA-C304BD33A46F}
EndGlobalSection
EndGlobal
147 changes: 147 additions & 0 deletions SocketClient/SocketClient/SocketClient.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{7d219526-4120-4c7c-bc32-07fc3aa998e1}</ProjectGuid>
<RootNamespace>SocketClient</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
22 changes: 22 additions & 0 deletions SocketClient/SocketClient/SocketClient.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions SocketClient/SocketClient/SocketClient.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
182 changes: 182 additions & 0 deletions SocketClient/SocketClient/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* This software is owned by Mehmet Tezel.
* You can change and use as you wish.
* (Provided that the source is specified)
* https://github.com/TzLcorp
* TezeL Corporation
*/

#include <Windows.h>
#include <iostream>
#include <cstdio>

#pragma comment(lib,"ws2_32.lib") // Winsock Library

using namespace std;

#define MANAGE_OPERATION_0 0 // If you want to do operation define new variables and use client.

void textcolor(int color); // Changes console text color.

typedef struct _CLIENTDATA
{
byte Operation; // Manage Operation from client.
int id = 0; // Your ID.
int msgCounter = 0; // Counts the messages on the server for user client.
char msg[128] = { '\0' }; // The reason it is null is for sending empty data initially.
char userName[16] = { '\0' }; // Your username.
char reply[100] = { '\0' }; // If the user interacts with the server (this is up to you) the server responds to the user.
bool isJoined = false; // For someone who joins the server.

}CLIENTDATA, *PCLIENTDATA; // This structure manages all data.

/* The codes I wrote in the while loop
* are for a healthy communication with the server.
* If you want, you can delete those codes and create
* your own client and server from 0.
*/

int main() {

CLIENTDATA data;
PCLIENTDATA p_data;
SOCKET Socket;
hostent* host;
int tryConnect = 1;
int userID;

WSADATA wd;
sockaddr_in server;

if (WSAStartup(MAKEWORD(2, 2), &wd) != 0)
{
printf("\nError: Unable to initialize Winsock.\n");
return 1;
}

char ipAddress[16];
int port;
cout << "Enter IP: ";
cin >> ipAddress;
cout << "Enter Port: ";
cin >> port;

host = gethostbyname(ipAddress);

if (!host)
{
printf("\nError: Unable to resolve host.\n");

WSACleanup();
return -1;
}

printf("Host: %s\n", ipAddress);

server.sin_family = AF_INET;
server.sin_addr = *((LPIN_ADDR)*host->h_addr_list);
server.sin_port = htons(port);

p_data = (PCLIENTDATA)VirtualAlloc(NULL, port, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); // Converts port packages to a "pointer" structure variable.

int userNameHandler = 0;

while (true)
{
label:
if (userNameHandler <= 0) {
cout << "Enter username: ";
cin >> data.userName;
userNameHandler += 2;
if (strlen(data.userName) >= 16)
{
printf("\nYou can enter max 16 characters!\n");
userNameHandler = 0;
goto label;
}
}

Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create socket.

if (Socket == INVALID_SOCKET)
{
printf("\nError: Unable to create socket.\n");
}

if (connect(Socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR && tryConnect <= 3) // Connect to the server.
{
printf("\nCould not connect to the server.\n");
Sleep(2000);
printf("\nRetrying...(%d)\n", tryConnect);
Sleep(2000);
tryConnect += 1;
data.isJoined = false;
//closesocket(Socket);
continue;
}

if (tryConnect >= 4 && data.isJoined == false)
{
printf("\nCould not connect to the server, please contact your server administrator.\n");
closesocket(Socket);
system("PAUSE");
return -1;
}

if (send(Socket, (char*)&data, sizeof(data), 0) == SOCKET_ERROR) // Send data to server.
{
printf("\nError: Unable to send data to remote computer.\n");
closesocket(Socket);
continue;
}

recv(Socket, (char*)p_data, sizeof(CLIENTDATA), 0); // Receive data from server.

if (data.isJoined == false) {

userID = p_data->id;
data.isJoined = true;

cout << "\n\n["; textcolor(10); cout << "+"; textcolor(7); cout << "] "; textcolor(10); cout << "You are connected!\n" << endl; textcolor(7);
printf("ID: %d\n", userID);

tryConnect = 1;
}

memset(p_data, 0, sizeof(CLIENTDATA));

memset(&data.msg, 0, sizeof(data.msg));

if (data.isJoined == true) {
cout << "\n>> ";
scanf_s("\r%[^\n]c", data.msg);
if (strlen(data.msg) >= 128)
{
data.msg[0] = '\0';
printf("\nYou can enter only 128 characters! (Kicked for server health.)\n\n");
data.isJoined = false;
userNameHandler = 0;
closesocket(Socket);
continue;
}
}
}

closesocket(Socket);
WSACleanup();

return 0;
}

void textcolor(int color)
{
static int BACKGROUND;

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

GetConsoleScreenBufferInfo(h, &csbiInfo);

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
color + (BACKGROUND << 4));
}
Loading

0 comments on commit b45ad3c

Please sign in to comment.