diff --git a/Client/Debug/Client.log b/Client/Debug/Client.log index a66f366..4bcbbc6 100644 --- a/Client/Debug/Client.log +++ b/Client/Debug/Client.log @@ -1,2 +1 @@ - client.cpp - Client.vcxproj -> C:\Users\bartool\MyData\MyCode\Cpp\VisualStudio_lap\LanChat\Debug\Client.exe + Client.vcxproj -> C:\Users\bartool\MyData\MyCode\Cpp\VisualStudio_lap\LanChat\Debug\Client.exe diff --git a/Client/Debug/Client.tlog/CL.read.1.tlog b/Client/Debug/Client.tlog/CL.read.1.tlog index 979b98e..c6b1fcf 100644 Binary files a/Client/Debug/Client.tlog/CL.read.1.tlog and b/Client/Debug/Client.tlog/CL.read.1.tlog differ diff --git a/Client/Debug/Client.tlog/link.read.1.tlog b/Client/Debug/Client.tlog/link.read.1.tlog index 9e55b39..bdf266c 100644 Binary files a/Client/Debug/Client.tlog/link.read.1.tlog and b/Client/Debug/Client.tlog/link.read.1.tlog differ diff --git a/Client/Debug/Client.tlog/link.write.1.tlog b/Client/Debug/Client.tlog/link.write.1.tlog index b49fa63..323d114 100644 Binary files a/Client/Debug/Client.tlog/link.write.1.tlog and b/Client/Debug/Client.tlog/link.write.1.tlog differ diff --git a/Client/Debug/vc142.idb b/Client/Debug/vc142.idb index e2b70ed..08bee09 100644 Binary files a/Client/Debug/vc142.idb and b/Client/Debug/vc142.idb differ diff --git a/Client/Debug/vc142.pdb b/Client/Debug/vc142.pdb index 3a544ab..b7b1281 100644 Binary files a/Client/Debug/vc142.pdb and b/Client/Debug/vc142.pdb differ diff --git a/Client/client.cpp b/Client/client.cpp index 6fd031f..bb67e2c 100644 --- a/Client/client.cpp +++ b/Client/client.cpp @@ -1,5 +1,7 @@ //Client #include +//#include +#include using namespace SocketLibrary; @@ -8,10 +10,33 @@ int main() if (Network::Initialize() == true) { Log::Print("[CLIENT] Winsok zostal zainicjolizowany. Mozemy dzialac."); + Socket socket; + if (socket.Create() == Result::Success) + { + Log::Print("[CLIENT] Utworzylem socket."); + if (socket.Connect(Endpoint("127.0.0.1", 54000)) == Result::Success) + { + Log::Print("[CLIENT] Polaczylem sie serverem."); + std::string outString = "Wysylam z klienta."; + char buffer[256]; + strcpy_s(buffer, "witam z klienta!\0"); + + while (true) + { + socket.SendAll(outString.data(), outString.size()); + //socket.SendAll(buffer, 256); + std::cout << "wyslalem 256 bajtow" << std::endl; + Sleep(1000); + } + } + + socket.Close(); + } } Network::Shutdown(); + system("pause"); return 0; } diff --git a/Debug/Client.exe b/Debug/Client.exe index d8de0c6..3c1f8eb 100644 Binary files a/Debug/Client.exe and b/Debug/Client.exe differ diff --git a/Debug/Client.ilk b/Debug/Client.ilk index 3195d27..9a9efa7 100644 Binary files a/Debug/Client.ilk and b/Debug/Client.ilk differ diff --git a/Debug/Client.pdb b/Debug/Client.pdb index 6ad9766..ee45f96 100644 Binary files a/Debug/Client.pdb and b/Debug/Client.pdb differ diff --git a/Debug/Server.exe b/Debug/Server.exe index 9a20791..e7c6ef8 100644 Binary files a/Debug/Server.exe and b/Debug/Server.exe differ diff --git a/Debug/Server.ilk b/Debug/Server.ilk index 276a71c..0498863 100644 Binary files a/Debug/Server.ilk and b/Debug/Server.ilk differ diff --git a/Debug/Server.pdb b/Debug/Server.pdb index 0b9b346..abeb6dd 100644 Binary files a/Debug/Server.pdb and b/Debug/Server.pdb differ diff --git a/Debug/SocketLibrary.lib b/Debug/SocketLibrary.lib index c82d6ee..48eb32f 100644 Binary files a/Debug/SocketLibrary.lib and b/Debug/SocketLibrary.lib differ diff --git a/Debug/SocketLibrary.pdb b/Debug/SocketLibrary.pdb index ae0da1e..127393d 100644 Binary files a/Debug/SocketLibrary.pdb and b/Debug/SocketLibrary.pdb differ diff --git a/Server/Debug/Server.log b/Server/Debug/Server.log index 1a7fc6b..c7c9e4d 100644 --- a/Server/Debug/Server.log +++ b/Server/Debug/Server.log @@ -1,2 +1 @@ - server.cpp - Server.vcxproj -> C:\Users\bartool\MyData\MyCode\Cpp\VisualStudio_lap\LanChat\Debug\Server.exe + Server.vcxproj -> C:\Users\bartool\MyData\MyCode\Cpp\VisualStudio_lap\LanChat\Debug\Server.exe diff --git a/Server/Debug/Server.tlog/CL.read.1.tlog b/Server/Debug/Server.tlog/CL.read.1.tlog index 4218e93..370dcbc 100644 Binary files a/Server/Debug/Server.tlog/CL.read.1.tlog and b/Server/Debug/Server.tlog/CL.read.1.tlog differ diff --git a/Server/Debug/Server.tlog/link.read.1.tlog b/Server/Debug/Server.tlog/link.read.1.tlog index 20f6377..f26ce6f 100644 Binary files a/Server/Debug/Server.tlog/link.read.1.tlog and b/Server/Debug/Server.tlog/link.read.1.tlog differ diff --git a/Server/Debug/vc142.idb b/Server/Debug/vc142.idb index b8a3bb1..4aa1411 100644 Binary files a/Server/Debug/vc142.idb and b/Server/Debug/vc142.idb differ diff --git a/Server/Debug/vc142.pdb b/Server/Debug/vc142.pdb index 8ae89f3..a834f8d 100644 Binary files a/Server/Debug/vc142.pdb and b/Server/Debug/vc142.pdb differ diff --git a/Server/server.cpp b/Server/server.cpp index 2d86200..508eaab 100644 --- a/Server/server.cpp +++ b/Server/server.cpp @@ -1,6 +1,6 @@ //Server #include - +#include using namespace SocketLibrary; int main() @@ -8,15 +8,41 @@ int main() if (Network::Initialize() == true) { Log::Print("[SERVER] Winsok zostal zainicjolizowany. Mozemy dzialac."); - Socket socket; - if (socket.Create() == Result::Success) + + Socket socketTCP(Protocol::TCP); + Socket socketUDP(Protocol::UDP); + if (socketTCP.Create() == Result::Success) { + socketUDP.Create(); + socketUDP.Bind(Endpoint("0.0.0.0", 54001)); Log::Print("[SERVER] Utworzylem gniazdo."); - socket.Close(); + if (socketTCP.BindAndListen(Endpoint("127.0.0.1", 54000), 5) == Result::Success) + { + Log::Print("[SERVER] sukces"); + Socket newConnection; + if (socketTCP.Accpet(newConnection) == Result::Success) + { + Log::Print("[SERVER] accept: sukces"); + std::string buffer = ""; + int size = 0; + while (true) + { + newConnection.ReciveAll(buffer, size); + std::cout << "[" << size << "] "<< buffer << std::endl; + buffer = ""; + } + newConnection.Close(); + } + else + { + Log::PrintError("[SERVER] accept: porazka", -1); + } + } + socketTCP.Close(); } else { - Log::PrintError("[SERVER] Nie udalo mi sie utworzyc gniazda.", -1); + Log::PrintError("[SERVER] porazka", -1); } } diff --git a/SocketLibrary/Debug/SocketLibrary.idb b/SocketLibrary/Debug/SocketLibrary.idb index fa126dc..8c7cd09 100644 Binary files a/SocketLibrary/Debug/SocketLibrary.idb and b/SocketLibrary/Debug/SocketLibrary.idb differ diff --git a/SocketLibrary/Debug/SocketLibrary.log b/SocketLibrary/Debug/SocketLibrary.log index f80f970..4948a0d 100644 --- a/SocketLibrary/Debug/SocketLibrary.log +++ b/SocketLibrary/Debug/SocketLibrary.log @@ -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 diff --git a/SocketLibrary/Debug/SocketLibrary.pdb b/SocketLibrary/Debug/SocketLibrary.pdb index ae0da1e..127393d 100644 Binary files a/SocketLibrary/Debug/SocketLibrary.pdb and b/SocketLibrary/Debug/SocketLibrary.pdb differ diff --git a/SocketLibrary/Debug/SocketLibrary.tlog/CL.command.1.tlog b/SocketLibrary/Debug/SocketLibrary.tlog/CL.command.1.tlog index 07d36d8..1ac9596 100644 Binary files a/SocketLibrary/Debug/SocketLibrary.tlog/CL.command.1.tlog and b/SocketLibrary/Debug/SocketLibrary.tlog/CL.command.1.tlog differ diff --git a/SocketLibrary/Debug/SocketLibrary.tlog/CL.read.1.tlog b/SocketLibrary/Debug/SocketLibrary.tlog/CL.read.1.tlog index f750538..0dcd849 100644 Binary files a/SocketLibrary/Debug/SocketLibrary.tlog/CL.read.1.tlog and b/SocketLibrary/Debug/SocketLibrary.tlog/CL.read.1.tlog differ diff --git a/SocketLibrary/Debug/SocketLibrary.tlog/CL.write.1.tlog b/SocketLibrary/Debug/SocketLibrary.tlog/CL.write.1.tlog index 47be149..d491352 100644 Binary files a/SocketLibrary/Debug/SocketLibrary.tlog/CL.write.1.tlog and b/SocketLibrary/Debug/SocketLibrary.tlog/CL.write.1.tlog differ diff --git a/SocketLibrary/Debug/SocketLibrary.tlog/Lib-link.read.1.tlog b/SocketLibrary/Debug/SocketLibrary.tlog/Lib-link.read.1.tlog index 609a4df..5006b26 100644 Binary files a/SocketLibrary/Debug/SocketLibrary.tlog/Lib-link.read.1.tlog and b/SocketLibrary/Debug/SocketLibrary.tlog/Lib-link.read.1.tlog differ diff --git a/SocketLibrary/Debug/SocketLibrary.tlog/Lib-link.write.1.tlog b/SocketLibrary/Debug/SocketLibrary.tlog/Lib-link.write.1.tlog index 08ae053..e3323cb 100644 Binary files a/SocketLibrary/Debug/SocketLibrary.tlog/Lib-link.write.1.tlog and b/SocketLibrary/Debug/SocketLibrary.tlog/Lib-link.write.1.tlog differ diff --git a/SocketLibrary/Debug/SocketLibrary.tlog/Lib.command.1.tlog b/SocketLibrary/Debug/SocketLibrary.tlog/Lib.command.1.tlog index d4b8625..cfcebd1 100644 Binary files a/SocketLibrary/Debug/SocketLibrary.tlog/Lib.command.1.tlog and b/SocketLibrary/Debug/SocketLibrary.tlog/Lib.command.1.tlog differ diff --git a/SocketLibrary/Endpoint.cpp b/SocketLibrary/Endpoint.cpp new file mode 100644 index 0000000..ec35dbe --- /dev/null +++ b/SocketLibrary/Endpoint.cpp @@ -0,0 +1,79 @@ +#include "Endpoint.h" +//#include +#include "Log.h" +#include + +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)); +} diff --git a/SocketLibrary/Endpoint.h b/SocketLibrary/Endpoint.h new file mode 100644 index 0000000..ced84db --- /dev/null +++ b/SocketLibrary/Endpoint.h @@ -0,0 +1,26 @@ +#pragma once +#include +#include +#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 = {}; + }; +} \ No newline at end of file diff --git a/SocketLibrary/Protocol.h b/SocketLibrary/Protocol.h new file mode 100644 index 0000000..7d0bd7d --- /dev/null +++ b/SocketLibrary/Protocol.h @@ -0,0 +1,12 @@ +#pragma once + +namespace SocketLibrary +{ + + enum class Protocol + { + Unknown, + TCP, + UDP + }; +} \ No newline at end of file diff --git a/SocketLibrary/Socket.cpp b/SocketLibrary/Socket.cpp index 9da6cef..6adca2f 100644 --- a/SocketLibrary/Socket.cpp +++ b/SocketLibrary/Socket.cpp @@ -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; } diff --git a/SocketLibrary/Socket.h b/SocketLibrary/Socket.h index 0cbacaa..66ed5c5 100644 --- a/SocketLibrary/Socket.h +++ b/SocketLibrary/Socket.h @@ -2,22 +2,34 @@ #include #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; }; diff --git a/SocketLibrary/SocketLibrary.vcxproj b/SocketLibrary/SocketLibrary.vcxproj index d946323..8c3e403 100644 --- a/SocketLibrary/SocketLibrary.vcxproj +++ b/SocketLibrary/SocketLibrary.vcxproj @@ -142,15 +142,18 @@ + + + diff --git a/SocketLibrary/SocketLibrary.vcxproj.filters b/SocketLibrary/SocketLibrary.vcxproj.filters index 899d66a..5db31d9 100644 --- a/SocketLibrary/SocketLibrary.vcxproj.filters +++ b/SocketLibrary/SocketLibrary.vcxproj.filters @@ -36,6 +36,12 @@ Header Files + + Header Files + + + Header Files + @@ -47,5 +53,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/SocketLibrary/SocketOption.h b/SocketLibrary/SocketOption.h index 7e26577..cccdb2f 100644 --- a/SocketLibrary/SocketOption.h +++ b/SocketLibrary/SocketOption.h @@ -5,5 +5,6 @@ namespace SocketLibrary enum class SocketOption { TCP_NoDelay, + UDP_Broadcast, }; } \ No newline at end of file