diff --git a/myapp.sln b/myapp.sln new file mode 100644 index 0000000..2a4fd82 --- /dev/null +++ b/myapp.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32210.238 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "myapp", "myapp\myapp.vcxproj", "{B6A80389-FB76-4FE6-B5E6-143D763033E0}" +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 + {B6A80389-FB76-4FE6-B5E6-143D763033E0}.Debug|x64.ActiveCfg = Debug|x64 + {B6A80389-FB76-4FE6-B5E6-143D763033E0}.Debug|x64.Build.0 = Debug|x64 + {B6A80389-FB76-4FE6-B5E6-143D763033E0}.Debug|x86.ActiveCfg = Debug|Win32 + {B6A80389-FB76-4FE6-B5E6-143D763033E0}.Debug|x86.Build.0 = Debug|Win32 + {B6A80389-FB76-4FE6-B5E6-143D763033E0}.Release|x64.ActiveCfg = Release|x64 + {B6A80389-FB76-4FE6-B5E6-143D763033E0}.Release|x64.Build.0 = Release|x64 + {B6A80389-FB76-4FE6-B5E6-143D763033E0}.Release|x86.ActiveCfg = Release|Win32 + {B6A80389-FB76-4FE6-B5E6-143D763033E0}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8015EEC7-8AA9-4CF5-B6FC-3270ADB84058} + EndGlobalSection +EndGlobal diff --git a/myapp/myapp.c b/myapp/myapp.c new file mode 100644 index 0000000..3d38b52 --- /dev/null +++ b/myapp/myapp.c @@ -0,0 +1,293 @@ +//#define WIN32_LEAN_AND_MEAN +// +//#include +//#include +//#include +//#include +//#include +// +//// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib +//#pragma comment (lib, "Ws2_32.lib") +//#pragma comment (lib, "Mswsock.lib") +//#pragma comment (lib, "AdvApi32.lib") +// +//#define DEFAULT_BUFLEN 512 +//#define DEFAULT_PORT "27015" +// +//int __cdecl main(int argc, char** argv) +//{ +// WSADATA wsaData; +// SOCKET ConnectSocket = INVALID_SOCKET; +// struct addrinfo* result = NULL, +// * ptr = NULL, +// hints; +// const char* sendbuf = "this is a test"; +// char recvbuf[DEFAULT_BUFLEN]; +// int iResult; +// int recvbuflen = DEFAULT_BUFLEN; +// +// // Validate the parameters +// if (argc != 2) { +// printf("usage: %s server-name\n", argv[0]); +// return 1; +// } +// +// // Initialize Winsock +// iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); +// if (iResult != 0) { +// printf("WSAStartup failed with error: %d\n", iResult); +// return 1; +// } +// +// ZeroMemory(&hints, sizeof(hints)); +// hints.ai_family = AF_UNSPEC; +// hints.ai_socktype = SOCK_STREAM; +// hints.ai_protocol = IPPROTO_TCP; +// +// // Resolve the server address and port +// iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result); +// if (iResult != 0) { +// printf("getaddrinfo failed with error: %d\n", iResult); +// WSACleanup(); +// +// return 1; +// } +// +// // Attempt to connect to an address until one succeeds +// for (ptr = result; ptr != NULL; ptr = ptr->ai_next) { +// +// // Create a SOCKET for connecting to server +// ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); +// +// if (ConnectSocket == INVALID_SOCKET) { +// printf("socket failed with error: %ld\n", WSAGetLastError()); +// WSACleanup(); +// return 1; +// } +// +// // Connect to server. +// iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); +// +// if (iResult == SOCKET_ERROR) { +// closesocket(ConnectSocket); +// ConnectSocket = INVALID_SOCKET; +// continue; +// } +// +// break; +// } +// +// freeaddrinfo(result); +// +// if (ConnectSocket == INVALID_SOCKET) { +// printf("Unable to connect to server!\n"); +// WSACleanup(); +// +// return 1; +// } +// +// // Send an initial buffer +// iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0); +// if (iResult == SOCKET_ERROR) { +// printf("send failed with error: %d\n", WSAGetLastError()); +// closesocket(ConnectSocket); +// WSACleanup(); +// +// return 1; +// } +// +// printf("Bytes Sent: %ld\n", iResult); +// +// // shutdown the connection since no more data will be sent +// iResult = shutdown(ConnectSocket, SD_SEND); +// if (iResult == SOCKET_ERROR) { +// printf("shutdown failed with error: %d\n", WSAGetLastError()); +// closesocket(ConnectSocket); +// WSACleanup(); +// +// return 1; +// } +// +// // Receive until the peer closes the connection +// do { +// iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); +// +// if (iResult > 0) +// printf("Bytes received: %d\n", iResult); +// else if (iResult == 0) +// printf("Connection closed\n"); +// else +// printf("recv failed with error: %d\n", WSAGetLastError()); +// +// } while (iResult > 0); +// +// // cleanup +// closesocket(ConnectSocket); +// WSACleanup(); +// +// return 0; +//} + +#undef UNICODE + +#define WIN32_LEAN_AND_MEAN + +#include +#include +#include +#include +#include + +// Need to link with Ws2_32.lib +#pragma comment (lib, "Ws2_32.lib") +// #pragma comment (lib, "Mswsock.lib") + +#define DEFAULT_BUFLEN 512 +#define DEFAULT_PORT "27015" + +int receiv(iResult, ClientSocket, recvbuf, recvbuflen) { + int iSendResult; + + do { + iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); + + if (iResult > 0) { + printf("Bytes received: %d\n", iResult); + + // Echo the buffer back to the sender + iSendResult = send(ClientSocket, recvbuf, iResult, 0); + if (iSendResult == SOCKET_ERROR) { + printf("send failed with error: %d\n", WSAGetLastError()); + closesocket(ClientSocket); + WSACleanup(); + + return 1; + } + printf("Bytes sent: %d\n", iSendResult); + } + else if (iResult == 0) + printf("Connection closing...\n"); + else { + printf("recv failed with error: %d\n", WSAGetLastError()); + closesocket(ClientSocket); + WSACleanup(); + + return 1; + } + + } while (iResult > 0); +} + +int __cdecl main(void) +{ + WSADATA wsaData; + int iResult; + + SOCKET ListenSocket = INVALID_SOCKET; + SOCKET ClientSocket = INVALID_SOCKET; + + struct addrinfo* result = NULL; + struct addrinfo hints; + + char recvbuf[DEFAULT_BUFLEN]; + int recvbuflen = DEFAULT_BUFLEN; + + // Initialize Winsock + iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (iResult != 0) { + printf("WSAStartup failed with error: %d\n", iResult); + + return 1; + } + + ZeroMemory(&hints, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_flags = AI_PASSIVE; + + // Resolve the server address and port + iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); + if (iResult != 0) { + printf("getaddrinfo failed with error: %d\n", iResult); + WSACleanup(); + + return 1; + } + + // Create a SOCKET for the server to listen for client connections. + ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); + if (ListenSocket == INVALID_SOCKET) { + printf("socket failed with error: %ld\n", WSAGetLastError()); + freeaddrinfo(result); + WSACleanup(); + + return 1; + } + + // Setup the TCP listening socket + iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); + if (iResult == SOCKET_ERROR) { + printf("bind failed with error: %d\n", WSAGetLastError()); + freeaddrinfo(result); + closesocket(ListenSocket); + WSACleanup(); + + return 1; + } + + freeaddrinfo(result); + + iResult = listen(ListenSocket, SOMAXCONN); + if (iResult == SOCKET_ERROR) { + printf("listen failed with error: %d\n", WSAGetLastError()); + closesocket(ListenSocket); + WSACleanup(); + + return 1; + } + + // Accept a client socket + + ClientSocket = accept(ListenSocket, NULL, NULL); + + HANDLE hThread; + DWORD threadID; + + hThread = CreateThread(NULL, + 0, + receiv(iResult, ClientSocket, recvbuf, recvbuflen), + NULL, + 0, + &threadID); + + if (ClientSocket == INVALID_SOCKET) { + printf("accept failed with error: %d\n", WSAGetLastError()); + closesocket(ListenSocket); + WSACleanup(); + + return 1; + } + + // No longer need server socket + // closesocket(ListenSocket); + + // Receive until the peer shuts down the connection + + // shutdown the connection since we're done + iResult = shutdown(ClientSocket, SD_SEND); + if (iResult == SOCKET_ERROR) { + printf("shutdown failed with error: %d\n", WSAGetLastError()); + closesocket(ClientSocket); + WSACleanup(); + + return 1; + } + + // cleanup + closesocket(ClientSocket); + WSACleanup(); + CloseHandle(hThread); + + return 0; +} \ No newline at end of file diff --git a/myapp/myapp.vcxproj b/myapp/myapp.vcxproj new file mode 100644 index 0000000..c97d2d4 --- /dev/null +++ b/myapp/myapp.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {b6a80389-fb76-4fe6-b5e6-143d763033e0} + myapp + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/myapp/myapp.vcxproj.filters b/myapp/myapp.vcxproj.filters new file mode 100644 index 0000000..33402ac --- /dev/null +++ b/myapp/myapp.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/myapp/myapp.vcxproj.user b/myapp/myapp.vcxproj.user new file mode 100644 index 0000000..06f6092 --- /dev/null +++ b/myapp/myapp.vcxproj.user @@ -0,0 +1,7 @@ + + + + 10.202.0.13 + WindowsLocalDebugger + + \ No newline at end of file diff --git a/myapp/x64/Debug/myapp.exe.recipe b/myapp/x64/Debug/myapp.exe.recipe new file mode 100644 index 0000000..963d7e1 --- /dev/null +++ b/myapp/x64/Debug/myapp.exe.recipe @@ -0,0 +1,11 @@ + + + + + C:\Users\class\Desktop\myapp\x64\Debug\myapp.exe + + + + + + \ No newline at end of file diff --git a/myapp/x64/Debug/myapp.ilk b/myapp/x64/Debug/myapp.ilk new file mode 100644 index 0000000..d356142 Binary files /dev/null and b/myapp/x64/Debug/myapp.ilk differ diff --git a/myapp/x64/Debug/myapp.log b/myapp/x64/Debug/myapp.log new file mode 100644 index 0000000..8ecd26f --- /dev/null +++ b/myapp/x64/Debug/myapp.log @@ -0,0 +1,9 @@ + myapp.c +C:\Users\class\Desktop\myapp\myapp\myapp.c(152,45): warning C4047: функция: "char *" отличается по уровням косвенного обращения от "int" +C:\Users\class\Desktop\myapp\myapp\myapp.c(152,38): warning C4024: recv: различные типы для формального и фактического параметров 2 +C:\Users\class\Desktop\myapp\myapp\myapp.c(158,53): warning C4047: функция: "const char *" отличается по уровням косвенного обращения от "int" +C:\Users\class\Desktop\myapp\myapp\myapp.c(158,46): warning C4024: send: различные типы для формального и фактического параметров 2 +C:\Users\class\Desktop\myapp\myapp\myapp.c(259,59): warning C4047: функция: "LPTHREAD_START_ROUTINE" отличается по уровням косвенного обращения от "int" +C:\Users\class\Desktop\myapp\myapp\myapp.c(259,15): warning C4024: CreateThread: различные типы для формального и фактического параметров 3 +C:\Users\class\Desktop\myapp\myapp\myapp.c(179): warning C4715: receiv: значение возвращается не при всех путях выполнения + myapp.vcxproj -> C:\Users\class\Desktop\myapp\x64\Debug\myapp.exe diff --git a/myapp/x64/Debug/myapp.obj b/myapp/x64/Debug/myapp.obj new file mode 100644 index 0000000..a036814 Binary files /dev/null and b/myapp/x64/Debug/myapp.obj differ diff --git a/myapp/x64/Debug/myapp.tlog/CL.command.1.tlog b/myapp/x64/Debug/myapp.tlog/CL.command.1.tlog new file mode 100644 index 0000000..4669b35 Binary files /dev/null and b/myapp/x64/Debug/myapp.tlog/CL.command.1.tlog differ diff --git a/myapp/x64/Debug/myapp.tlog/CL.read.1.tlog b/myapp/x64/Debug/myapp.tlog/CL.read.1.tlog new file mode 100644 index 0000000..4721fa1 Binary files /dev/null and b/myapp/x64/Debug/myapp.tlog/CL.read.1.tlog differ diff --git a/myapp/x64/Debug/myapp.tlog/CL.write.1.tlog b/myapp/x64/Debug/myapp.tlog/CL.write.1.tlog new file mode 100644 index 0000000..8470cc1 Binary files /dev/null and b/myapp/x64/Debug/myapp.tlog/CL.write.1.tlog differ diff --git a/myapp/x64/Debug/myapp.tlog/link.command.1.tlog b/myapp/x64/Debug/myapp.tlog/link.command.1.tlog new file mode 100644 index 0000000..d7dd330 Binary files /dev/null and b/myapp/x64/Debug/myapp.tlog/link.command.1.tlog differ diff --git a/myapp/x64/Debug/myapp.tlog/link.read.1.tlog b/myapp/x64/Debug/myapp.tlog/link.read.1.tlog new file mode 100644 index 0000000..e45092a Binary files /dev/null and b/myapp/x64/Debug/myapp.tlog/link.read.1.tlog differ diff --git a/myapp/x64/Debug/myapp.tlog/link.write.1.tlog b/myapp/x64/Debug/myapp.tlog/link.write.1.tlog new file mode 100644 index 0000000..2ba26ff Binary files /dev/null and b/myapp/x64/Debug/myapp.tlog/link.write.1.tlog differ diff --git a/myapp/x64/Debug/myapp.tlog/myapp.lastbuildstate b/myapp/x64/Debug/myapp.tlog/myapp.lastbuildstate new file mode 100644 index 0000000..58e9445 --- /dev/null +++ b/myapp/x64/Debug/myapp.tlog/myapp.lastbuildstate @@ -0,0 +1,2 @@ +PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.31.31103:TargetPlatformVersion=10.0.19041.0: +Debug|x64|C:\Users\class\Desktop\myapp\| diff --git a/myapp/x64/Debug/vc143.idb b/myapp/x64/Debug/vc143.idb new file mode 100644 index 0000000..c03b31d Binary files /dev/null and b/myapp/x64/Debug/vc143.idb differ diff --git a/myapp/x64/Debug/vc143.pdb b/myapp/x64/Debug/vc143.pdb new file mode 100644 index 0000000..dea7838 Binary files /dev/null and b/myapp/x64/Debug/vc143.pdb differ diff --git a/x64/Debug/myapp.exe b/x64/Debug/myapp.exe new file mode 100644 index 0000000..e536fb3 Binary files /dev/null and b/x64/Debug/myapp.exe differ diff --git a/x64/Debug/myapp.pdb b/x64/Debug/myapp.pdb new file mode 100644 index 0000000..b1537fc Binary files /dev/null and b/x64/Debug/myapp.pdb differ