Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/ClientSockets/LinuxImp/LinuxClientSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@


int LinuxClientSocket::Configuration(int protocol) {
_socket = socket(AF_INET, SOCK_STREAM, protocol);
_protocol = protocol;
switch (protocol) {
case IPPROTO_TCP:
_socket = socket(AF_INET, SOCK_STREAM, protocol);
break;
case IPPROTO_UDP:
_socket = socket(AF_INET, SOCK_DGRAM, protocol);
break;
default:
return INVALID_SOCKET;
}
if (_socket == INVALID_SOCKET) {
Close();
return INVALID_SOCKET;
Expand Down Expand Up @@ -38,7 +48,8 @@ void LinuxClientSocket::Close() {
close(_socket);
}

int LinuxClientSocket::lConfiguration(int socket) {
int LinuxClientSocket::Configuration(int socket, int protocol) {
_protocol = protocol;
_socket = socket;
return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion src/ClientSockets/LinuxImp/LinuxClientSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class LinuxClientSocket : public IClientSocket{
private:
int _socket;
sockaddr_in _sockAddr;
int _protocol;

public:
int Configuration(int protocol) override;
Expand All @@ -19,7 +20,7 @@ class LinuxClientSocket : public IClientSocket{
void Close() override;

public:
int lConfiguration(int socket);
int Configuration(int socket, int protocol);
};


Expand Down
20 changes: 16 additions & 4 deletions src/ClientSockets/WinsockImp/WindowsClientSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ int WindowsClientSocket::Configuration(int protocol) {
return INVALID_SOCKET;
}

_socket = ::socket(AF_INET,SOCK_STREAM, protocol);
_protocol = protocol;
switch (protocol) {
case IPPROTO_TCP:
_socket = socket(AF_INET, SOCK_STREAM, protocol);
break;
case IPPROTO_UDP:
_socket = socket(AF_INET, SOCK_DGRAM, protocol);
break;
default:
return INVALID_SOCKET;
};

if (_socket == INVALID_SOCKET){
Close();
return INVALID_SOCKET;
Expand All @@ -27,13 +38,13 @@ int WindowsClientSocket::Connect(const int port, const char *address) {
}

int WindowsClientSocket::Send(const void *data, const int size) {
if (::send(_socket, (char*)data, size, 0 ) == SOCKET_ERROR)
if (send(_socket, (char*)data, size, 0 ) == SOCKET_ERROR)
return SOCKET_ERROR;
return 0;
}

int WindowsClientSocket::Receive(const void *data, const int size) {
if (::recv(_socket, (char*)data, size, 0) == SOCKET_ERROR)
if (recv(_socket, (char*)data, size, 0) == SOCKET_ERROR)
return SOCKET_ERROR;
return 0;
}
Expand All @@ -42,7 +53,8 @@ void WindowsClientSocket::Close() {
::closesocket(_socket);
}

int WindowsClientSocket::Configuration(SOCKET socket) {
int WindowsClientSocket::Configuration(SOCKET socket, int protocol) {
_protocol = protocol;
_socket = socket;
return 0;
}
4 changes: 2 additions & 2 deletions src/ClientSockets/WinsockImp/WindowsClientSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class WindowsClientSocket : public IClientSocket{
private:
SOCKET _socket;
SOCKADDR_IN _addrIn;

int _protocol;

public:
int Configuration(int protocol) override;
Expand All @@ -19,7 +19,7 @@ class WindowsClientSocket : public IClientSocket{
void Close() override;

public:
int Configuration(SOCKET socket);
int Configuration(SOCKET socket, int protocol);
};


Expand Down
14 changes: 12 additions & 2 deletions src/ServerSocket/LinuxImp/LinuxServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
#include "../../ClientSockets/LinuxImp/LinuxClientSocket.h"

int LinuxServerSocket::Configuration(int protocol) {
_socket = socket(AF_INET, SOCK_STREAM, protocol);
_protocol = protocol;
switch (protocol) {
case IPPROTO_TCP:
_socket = socket(AF_INET, SOCK_STREAM, protocol);
break;
case IPPROTO_UDP:
_socket = socket(AF_INET, SOCK_DGRAM, protocol);
break;
default:
return INVALID_SOCKET;
}
if (_socket == INVALID_SOCKET){
Close();
return INVALID_SOCKET;
Expand Down Expand Up @@ -37,7 +47,7 @@ IClientSocket *LinuxServerSocket::Accept() {
return nullptr;

LinuxClientSocket* clientSocket = new LinuxClientSocket();
clientSocket->lConfiguration(client);
clientSocket->Configuration(client, _protocol);
return clientSocket;
}

Expand Down
1 change: 1 addition & 0 deletions src/ServerSocket/LinuxImp/LinuxServerSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class LinuxServerSocket : public IServerSocket{
private:
int _socket;
sockaddr_in _sockAddr;
int _protocol;
public:
int Configuration(int protocol) override;
int Bind(const int port, const char* address) override;
Expand Down
14 changes: 12 additions & 2 deletions src/ServerSocket/WindowsImp/WindowsServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ int WindowsServerSocket::Configuration(int protocol) {
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
return INVALID_SOCKET;

_socket = ::socket(AF_INET, SOCK_STREAM, protocol);
_protocol = protocol;
switch (protocol) {
case IPPROTO_TCP:
_socket = socket(AF_INET, SOCK_STREAM, protocol);
break;
case IPPROTO_UDP:
_socket = socket(AF_INET, SOCK_DGRAM, protocol);
break;
default:
return INVALID_SOCKET;
}
if (_socket == INVALID_SOCKET) {
Close();
return INVALID_SOCKET;
Expand Down Expand Up @@ -41,7 +51,7 @@ IClientSocket *WindowsServerSocket::Accept() {
return nullptr;

WindowsClientSocket* clientSocket = new WindowsClientSocket;
clientSocket->Configuration(client);
clientSocket->Configuration(client, _protocol);
return clientSocket;
}

Expand Down
1 change: 1 addition & 0 deletions src/ServerSocket/WindowsImp/WindowsServerSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class WindowsServerSocket : public IServerSocket{
private:
SOCKET _socket;
SOCKADDR_IN _addrIn;
int _protocol;
public:
int Configuration(int protocol) override;
int Bind(const int port, const char* address) override;
Expand Down
65 changes: 61 additions & 4 deletions tests/LinuxTests/testOfLinuxClient/TestOfLinuxClientSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ TEST(TestsOfLinuxClientSocket, Creation){
clientSocket->Close();
}

TEST(TestsOfLinuxClientSocket, Configure){
TEST(TestsOfLinuxClientSocket, ConfigureTCP){
IClientSocket* clientSocket = new LinuxClientSocket();
EXPECT_NO_THROW(clientSocket->Configuration(IPPROTO_TCP));
clientSocket->Close();
}

TEST(TestsOfLinuxClientSocket, Connect){
TEST(TestsOfLinuxClientSocket, ConnectTCP){
IClientSocket* clientSocket = new LinuxClientSocket();
IServerSocket* serverSocket = new LinuxServerSocket();

Expand All @@ -30,7 +30,7 @@ TEST(TestsOfLinuxClientSocket, Connect){
clientSocket->Close();
serverSocket->Close();
}
TEST(TestsOfLinuxClientSocket, Send){
TEST(TestsOfLinuxClientSocket, SendTCP){
IClientSocket* clientSocket = new LinuxClientSocket();
IServerSocket* serverSocket = new LinuxServerSocket();
char data = 7,
Expand All @@ -47,7 +47,7 @@ TEST(TestsOfLinuxClientSocket, Send){
serverSocket->Close();
}

TEST(TestsOfLinuxClientSocket, SendAndCheckData){
TEST(TestsOfLinuxClientSocket, SendAndCheckDataTCP){
IClientSocket* clientSocket = new LinuxClientSocket();
IServerSocket* serverSocket = new LinuxServerSocket();
char data = 7,
Expand All @@ -66,6 +66,63 @@ TEST(TestsOfLinuxClientSocket, SendAndCheckData){
serverSocket->Close();
}


TEST(TestsOfLinuxClientSocket, ConfigureUDP){
IClientSocket* clientSocket = new LinuxClientSocket();
EXPECT_NO_THROW(clientSocket->Configuration(IPPROTO_UDP));
clientSocket->Close();
}

TEST(TestsOfLinuxClientSocket, ConnectUDP){
IClientSocket* clientSocket = new LinuxClientSocket();
IServerSocket* serverSocket = new LinuxServerSocket();

serverSocket->Configuration(IPPROTO_UDP);
serverSocket->Bind(8950, localhost);
serverSocket->Listen(1);
clientSocket->Configuration(IPPROTO_UDP);

EXPECT_EQ(clientSocket->Connect(8950, localhost), 0);
clientSocket->Close();
serverSocket->Close();
}
TEST(TestsOfLinuxClientSocket, SendUDP){
IClientSocket* clientSocket = new LinuxClientSocket();
IServerSocket* serverSocket = new LinuxServerSocket();
char data = 7,
fakeData =10;

serverSocket->Configuration(IPPROTO_UDP);
serverSocket->Bind(8950, localhost);
serverSocket->Listen(1);
clientSocket->Configuration(IPPROTO_UDP);
clientSocket->Connect(8950, localhost);
clientSocket->Send(&data, sizeof(data));
EXPECT_NO_THROW(serverSocket->Accept()->Receive(&fakeData, sizeof(char)));
clientSocket->Close();
serverSocket->Close();
}

TEST(TestsOfLinuxClientSocket, SendAndCheckDataUDP){
IClientSocket* clientSocket = new LinuxClientSocket();
IServerSocket* serverSocket = new LinuxServerSocket();
char data = 7,
fakeData =10;

serverSocket->Configuration(IPPROTO_UDP);
serverSocket->Bind(8950, localhost);
serverSocket->Listen(1);
clientSocket->Configuration(IPPROTO_UDP);
clientSocket->Connect(8950, localhost);
clientSocket->Send(&data, sizeof(data));
serverSocket->Accept()->Receive(&fakeData, sizeof(char));

EXPECT_EQ(data, fakeData);
clientSocket->Close();
serverSocket->Close();
}


int main(int argc, char* argv[]){
localhost = findLocalIp();
::testing::InitGoogleTest(&argc, argv);
Expand Down
71 changes: 66 additions & 5 deletions tests/LinuxTests/testOfLinuxServer/TestOfLinuxServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ TEST(TestsOfLinuxServerSocket, Creation){
serverSocket->Close();
}

TEST(TestsOfServerLinuxSocket, Configure){
TEST(TestsOfServerLinuxSocket, ConfigureTCP){
IServerSocket* serverSocket = new LinuxServerSocket();
EXPECT_NO_THROW(serverSocket->Configuration(IPPROTO_TCP));
serverSocket->Close();
}

TEST(TestsOfServerLinuxSocket, Bind){
TEST(TestsOfServerLinuxSocket, BindTCP){
IServerSocket* serverSocket = new LinuxServerSocket();

serverSocket->Configuration(IPPROTO_TCP);
Expand All @@ -26,7 +26,7 @@ TEST(TestsOfServerLinuxSocket, Bind){
serverSocket->Close();
}

TEST(TestsOfServerLinuxSocket, Listen){
TEST(TestsOfServerLinuxSocket, ListenTCP){
IServerSocket* serverSocket = new LinuxServerSocket();

serverSocket->Configuration(IPPROTO_TCP);
Expand All @@ -36,7 +36,7 @@ TEST(TestsOfServerLinuxSocket, Listen){
serverSocket->Close();
}

TEST(TestsOfServerLinuxSocket, Accept){
TEST(TestsOfServerLinuxSocket, AcceptTCP){
IServerSocket* serverSocket = new LinuxServerSocket();
IClientSocket* clientSocket = new LinuxClientSocket();

Expand All @@ -52,7 +52,7 @@ TEST(TestsOfServerLinuxSocket, Accept){
serverSocket->Close();
}

TEST(TestOfServerLinuxSocket, Recive){
TEST(TestOfServerLinuxSocket, ReciveTCP){
IServerSocket* serverSocket = new LinuxServerSocket();
IClientSocket* clientSocket = new LinuxClientSocket();
char data = 0,
Expand All @@ -71,6 +71,67 @@ TEST(TestOfServerLinuxSocket, Recive){
serverSocket->Close();
}


TEST(TestsOfServerLinuxSocket, ConfigureUDP){
IServerSocket* serverSocket = new LinuxServerSocket();
EXPECT_NO_THROW(serverSocket->Configuration(IPPROTO_UDP));
serverSocket->Close();
}

TEST(TestsOfServerLinuxSocket, BindUDP){
IServerSocket* serverSocket = new LinuxServerSocket();

serverSocket->Configuration(IPPROTO_UDP);

EXPECT_EQ(serverSocket->Bind(35098,localhost), 0);
serverSocket->Close();
}

TEST(TestsOfServerLinuxSocket, ListenUDP){
IServerSocket* serverSocket = new LinuxServerSocket();

serverSocket->Configuration(IPPROTO_TCP);
serverSocket->Bind(12498, localhost);

EXPECT_EQ(serverSocket->Listen(1), 0);
serverSocket->Close();
}

TEST(TestsOfServerLinuxSocket, AcceptUDP){
IServerSocket* serverSocket = new LinuxServerSocket();
IClientSocket* clientSocket = new LinuxClientSocket();

serverSocket->Configuration(IPPROTO_UDP);
serverSocket->Bind(12498, localhost);
serverSocket->Listen(1);
clientSocket->Configuration(IPPROTO_UDP);
clientSocket->Connect(12498, localhost);


EXPECT_NO_THROW(serverSocket->Accept());
clientSocket->Close();
serverSocket->Close();
}

TEST(TestOfServerLinuxSocket, ReciveUDP){
IServerSocket* serverSocket = new LinuxServerSocket();
IClientSocket* clientSocket = new LinuxClientSocket();
char data = 0,
reciveData = 1;

serverSocket->Configuration(IPPROTO_UDP);
serverSocket->Bind(12498, localhost);
serverSocket->Listen(1);
clientSocket->Configuration(IPPROTO_UDP);
clientSocket->Connect(12498, localhost);
IClientSocket* acceptSocket = serverSocket->Accept();
clientSocket->Send(&data, sizeof(char));

EXPECT_NO_THROW(acceptSocket->Receive(&reciveData, sizeof(char)));
clientSocket->Close();
serverSocket->Close();
}

int main(int argc, char* argv[]){
localhost = findLocalIp();
::testing::InitGoogleTest(&argc, argv);
Expand Down
Loading