[working] wysyla i odbiera dane

This commit is contained in:
2019-12-26 15:11:55 +01:00
parent fc65d4b146
commit dcfb945c0b
38 changed files with 442 additions and 18 deletions

View File

@@ -1,2 +1,3 @@
 Socket.cpp
C:\Users\bartool\MyData\MyCode\Cpp\VisualStudio_lap\LanChat\SocketLibrary\Socket.cpp(239,27): warning C4018: '<': signed/unsigned mismatch
SocketLibrary.vcxproj -> C:\Users\bartool\MyData\MyCode\Cpp\VisualStudio_lap\LanChat\Debug\SocketLibrary.lib

View File

@@ -0,0 +1,79 @@
#include "Endpoint.h"
//#include <WS2tcpip.h>
#include "Log.h"
#include <assert.h>
using namespace SocketLibrary;
Endpoint::Endpoint(const char* ip, unsigned short port)
{
this->_port = port;
in_addr addr;
int result = inet_pton(AF_INET, ip, &addr);
if (result == 1)
{
if (addr.S_un.S_addr != INADDR_NONE)
{
_ip_string = ip;
_hostname = ip;
_ipversion = IPVersion::IPv4;
_socketaddr.sin_family = AF_INET;
_socketaddr.sin_port = htons(_port);
memcpy(&_socketaddr.sin_addr, &addr, sizeof(in_addr));
return;
}
}
else
{
std::string invalidIP = ip;
Log::PrintError("To nie jest prawidlowy adres IP. [" + invalidIP + "]", result);
}
}
Endpoint::Endpoint(sockaddr* addr)
{
assert(addr->sa_family == AF_INET);
sockaddr_in* newaddr = (sockaddr_in*)addr;
_ipversion = IPVersion::IPv4;
_port = ntohs(newaddr->sin_port);
_ip_string.resize(16);
inet_ntop(AF_INET, &newaddr->sin_addr, &_ip_string[0], 16);
_hostname = _ip_string;
}
IPVersion Endpoint::GetIpVersion()
{
return _ipversion;
}
std::string Endpoint::GetHostName()
{
return _hostname;
}
std::string Endpoint::GetIpString()
{
return _ip_string;
}
unsigned short Endpoint::GetPort()
{
return _port;
}
sockaddr_in Endpoint::GetSocketaddrIP()
{
return _socketaddr;
}
void SocketLibrary::Endpoint::Print()
{
Log::Print("Hostname: " + _hostname);
Log::Print("IP: " + _ip_string);
Log::Print("Port: " + std::to_string(_port));
Log::Print("Family: " + std::to_string(_socketaddr.sin_family));
}

26
SocketLibrary/Endpoint.h Normal file
View File

@@ -0,0 +1,26 @@
#pragma once
#include <string>
#include <WS2tcpip.h>
#include "ipVersion.h"
namespace SocketLibrary
{
class Endpoint
{
public:
Endpoint(const char* ip, unsigned short port);
Endpoint(sockaddr* addr);
IPVersion GetIpVersion();
std::string GetHostName();
std::string GetIpString();
unsigned short GetPort();
sockaddr_in GetSocketaddrIP();
void Print();
private:
IPVersion _ipversion = IPVersion::Unknown;
std::string _hostname = "";
std::string _ip_string = "";
unsigned short _port = 0;
sockaddr_in _socketaddr = {};
};
}

12
SocketLibrary/Protocol.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
namespace SocketLibrary
{
enum class Protocol
{
Unknown,
TCP,
UDP
};
}

View File

@@ -4,13 +4,18 @@
namespace SocketLibrary
{
Socket::Socket(IPVersion ipversion, SOCKET socket)
:_ipversion(ipversion),_socket(socket)
Socket::Socket(IPVersion ipversion, Protocol protocol, SOCKET socket)
:_ipversion(ipversion), _socket(socket), _protocol(protocol)
{
assert(_ipversion == IPVersion::IPv4);
}
Socket::Socket(Protocol protocol)
:_ipversion(IPVersion::IPv4), _protocol(protocol), _socket(INVALID_SOCKET)
{
assert(_ipversion == IPVersion::IPv4);
}
Result Socket::Create()
{
@@ -18,24 +23,60 @@ namespace SocketLibrary
if (_socket != INVALID_SOCKET)
{
Log::PrintError("Gniazdo juz istnieje.", -1);
return Result::Fail;
}
else
{
Log::Print("Gniazdo nie istnieje. Tworze gniazdo...");
}
switch (_protocol)
{
case SocketLibrary::Protocol::Unknown:
Log::PrintError("Nie mozna utworzyc gniazda o nieznamym typie.", -1);
return Result::Fail;
break;
case SocketLibrary::Protocol::TCP:
_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
break;
case SocketLibrary::Protocol::UDP:
_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
break;
default:
break;
}
_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (_socket == INVALID_SOCKET)
{
int error = WSAGetLastError();
Log::PrintError("Nie udalo sie utworzyc gniazda", error);
return Result::Fail;
}
Log::Print("Gniazdo zostalo utworzone.");
if (SetSocketOption(SocketOption::TCP_NoDelay, TRUE) == Result::Success)
else
{
Log::Print("NoDelay zostalo wlaczone na gniezdzie.");
Log::Print("Gniazdo zostalo utworzone. [SOCKET:" + std::to_string(_socket) + "]");
}
switch (_protocol)
{
case SocketLibrary::Protocol::TCP:
if (SetSocketOption(SocketOption::TCP_NoDelay, TRUE) == Result::Success)
{
Log::Print("NoDelay zostalo wlaczone na gniezdzie TCP.");
}
break;
case SocketLibrary::Protocol::UDP:
if (SetSocketOption(SocketOption::UDP_Broadcast, TRUE) == Result::Success)
{
Log::Print("UDP_Broadcast zostalo wlaczone na gniezdzie UDP.");
}
break;
default:
break;
}
return Result::Success;
}
@@ -46,6 +87,10 @@ namespace SocketLibrary
Log::PrintError("Niemozna zamknac nieistniejacego gniazda.", -1);
return Result::Fail;
}
else
{
Log::Print("Zamykam gniazdo [SOCKET:" + std::to_string(_socket) + "]");
}
int result = closesocket(_socket);
if (result != 0)
@@ -54,11 +99,161 @@ namespace SocketLibrary
Log::PrintError("Nie udalo sie zamknac gniazda.", error);
return Result::Fail;
}
else
{
Log::Print("Gniazdo zostalo zamkniete.");
}
_socket = INVALID_SOCKET;
return Result::Success;
}
Result Socket::Bind(Endpoint endpoint)
{
sockaddr_in addr = endpoint.GetSocketaddrIP();
int result = bind(_socket, (sockaddr*)(&addr), sizeof(sockaddr_in));
if (result != 0)
{
int error = WSAGetLastError();
Log::PrintError("Nie udalo sie powiazac gniazda z portem.", error);
return Result::Fail;
}
else
{
Log::Print("Udalo sie zwiazac gniazdo z wybranym portem. [PORT]:" + std::to_string(endpoint.GetPort())
+ " [SOCKET]:" + std::to_string(_socket));
}
return Result::Success;
}
Result Socket::BindAndListen(Endpoint endpoint, int waitConnection)
{
if (Bind(endpoint) != Result::Success)
{
return Result::Fail;
}
int result = listen(_socket, waitConnection);
if (result != 0)
{
int error = WSAGetLastError();
Log::PrintError("Nie udalo sie ustawic gniazda w tryb nasluchu", error);
return Result::Fail;
}
else
{
Log::Print("Gniazdo jest od teraz w trybie nasluchu.");
}
return Result::Success;
}
Result Socket::Accpet(Socket& outSocket)
{
sockaddr_in addr = {};
int len = sizeof(sockaddr_in);
SOCKET acceptedConnection = accept(_socket, (sockaddr*)(&addr), &len);
if (acceptedConnection == INVALID_SOCKET)
{
int error = WSAGetLastError();
Log::PrintError("Wystapil problem przy akceptacji polaczenia.", error);
return Result::Fail;
}
else
{
Log::Print("Zaakceptowano nowe poleczenie [SOCKET:" + std::to_string(acceptedConnection) + "]");
}
Endpoint newConnectionEndpoint((sockaddr*)&addr);
newConnectionEndpoint.Print();
outSocket = Socket(IPVersion::IPv4, Protocol::TCP, acceptedConnection);
return Result::Success;
}
Result Socket::Connect(Endpoint endpoint)
{
sockaddr_in addr = endpoint.GetSocketaddrIP();
int result = connect(_socket, (sockaddr*)(&addr), sizeof(sockaddr_in));
if (result != 0)
{
int error = WSAGetLastError();
Log::PrintError("Nie mozna nawiazac polaczenia z " + endpoint.GetIpString(), error);
return Result::Fail;
}
else
{
Log::Print("Nawiazano nowe polaczenie z " + endpoint.GetIpString());
}
return Result::Success;
}
Result Socket::SendAll(const void* data, int numberOfBytes)
{
int totalBytesSent = 0;
uint32_t numberBytesToSend = numberOfBytes;
numberBytesToSend = htonl(numberBytesToSend);
Result result = Send(&numberBytesToSend, sizeof(uint32_t), totalBytesSent);
if (totalBytesSent != sizeof(uint32_t))
{
Log::PrintError("Nie mozna wyslac dlugosci strumienia danych.", -1);
return Result::Fail;
}
totalBytesSent = 0;
while (totalBytesSent < numberOfBytes)
{
int bytesLeft = numberOfBytes - totalBytesSent;
int bytesSent = 0;
char* bufferOffset = (char*)data + totalBytesSent;
result = Send(bufferOffset, bytesLeft, bytesSent);
if (result == Result::Fail)
{
Log::PrintError("Wyslalem " + std::to_string(totalBytesSent) + " z " + std::to_string(numberOfBytes) + " bajtow.", -1);
return Result::Fail;
}
totalBytesSent += bytesSent;
}
return Result::Success;
}
Result Socket::ReciveAll(std::string& output, int& numberOfBytes)
{
int totalBytesRecive = 0;
uint32_t numberBytesToRecive = 0;
Result result = Recive(&numberBytesToRecive, sizeof(uint32_t), totalBytesRecive);
if (totalBytesRecive != sizeof(uint32_t))
{
Log::PrintError("Nie mozna odebrac dlugosci strumienia dancyh.", -1);
Result::Fail;
}
totalBytesRecive = 0;
numberBytesToRecive = ntohl(numberBytesToRecive);
output.resize(numberBytesToRecive + 1);
while (totalBytesRecive < numberBytesToRecive)
{
int bytesLeft = numberBytesToRecive - totalBytesRecive;
int bytesRecive = 0;
char* bufferOffset = &output[0] + totalBytesRecive;
Result result = Recive(bufferOffset, bytesLeft, bytesRecive);
if (result == Result::Fail)
{
Log::PrintError("Odebralem " + std::to_string(totalBytesRecive) + " z " + std::to_string(numberOfBytes) + " bajtow.", -1);
return Result::Fail;
}
totalBytesRecive += bytesRecive;
}
numberOfBytes = totalBytesRecive;
return Result::Success;
}
SOCKET Socket::GetSocket()
{
return _socket;
@@ -68,6 +263,40 @@ namespace SocketLibrary
{
return _ipversion;
}
Result Socket::Send(const void* data, int size, int& bytesSent)
{
bytesSent = send(_socket, (const char*)data, size, NULL);
if (bytesSent == SOCKET_ERROR)
{
int error = WSAGetLastError();
Log::PrintError("Wystapil problem podczas wysylania dancyh.", error);
return Result::Fail;
}
return Result::Success;
}
Result Socket::Recive(void* buffor, int size, int& bytesRecived)
{
bytesRecived = recv(_socket, (char*)buffor, size, NULL);
if (bytesRecived == 0)
{
Log::PrintError("Polaczenie zostalo zamkniete.", -1);
// TODO - zamknac gniazdo
return Result::Fail;
}
if (bytesRecived == SOCKET_ERROR)
{
int error = WSAGetLastError();
Log::PrintError("Wystapil problem podczas odbierania dancyh.", error);
return Result::Fail;
}
return Result::Success;
}
Result Socket::SetSocketOption(SocketOption option, BOOL value)
{
int result = 0;
@@ -76,6 +305,9 @@ namespace SocketLibrary
case SocketOption::TCP_NoDelay:
result = setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&value, sizeof(value));
break;
case SocketOption::UDP_Broadcast:
result = setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (const char*)&value, sizeof(value));
break;
default:
return Result::Fail;
}

View File

@@ -2,22 +2,34 @@
#include <WinSock2.h>
#include "ipVersion.h"
#include "Protocol.h"
#include "Result.h"
#include "SocketOption.h"
#include "Endpoint.h"
namespace SocketLibrary
{
class Socket
{
public:
Socket(IPVersion ipversion = IPVersion::IPv4, SOCKET socket = INVALID_SOCKET); //(..., SocketHandle handle)
Socket(IPVersion ipversion = IPVersion::IPv4, Protocol protocol = Protocol::TCP, SOCKET socket = INVALID_SOCKET); //(..., SocketHandle handle)
Socket(Protocol protocol);
Result Create();
Result Close();
Result Bind(Endpoint endpoint);
Result BindAndListen(Endpoint endpoint, int waitConnection = 5);
Result Accpet(Socket& outSocket);
Result Connect(Endpoint endpoint);
Result SendAll(const void * data, int numberOfBytes);
Result ReciveAll(std::string& output, int& numberOfBytes);
SOCKET GetSocket(); //SocketHandle GetHandle()
IPVersion GetIpVersion();
private:
Result Send(const void * data, int size, int& bytesSent);
Result Recive(void * buffor, int size, int & bytesRecived);
Result SetSocketOption(SocketOption option, BOOL value);
IPVersion _ipversion = IPVersion::IPv4;
Protocol _protocol = Protocol::TCP;
SOCKET _socket = INVALID_SOCKET;
};

View File

@@ -142,15 +142,18 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Endpoint.h" />
<ClInclude Include="ipVersion.h" />
<ClInclude Include="Log.h" />
<ClInclude Include="Network.h" />
<ClInclude Include="Protocol.h" />
<ClInclude Include="Result.h" />
<ClInclude Include="Socket.h" />
<ClInclude Include="SocketLibrary.h" />
<ClInclude Include="SocketOption.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Endpoint.cpp" />
<ClCompile Include="Log.cpp" />
<ClCompile Include="Network.cpp" />
<ClCompile Include="Socket.cpp" />

View File

@@ -36,6 +36,12 @@
<ClInclude Include="SocketOption.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Endpoint.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Protocol.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Network.cpp">
@@ -47,5 +53,8 @@
<ClCompile Include="Socket.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Endpoint.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -5,5 +5,6 @@ namespace SocketLibrary
enum class SocketOption
{
TCP_NoDelay,
UDP_Broadcast,
};
}