Sending a message from server to client (done and done)

This commit is contained in:
Pontus Fransson 2013-11-19 13:42:50 +01:00
parent adcef51227
commit ac75e178ce
16 changed files with 299 additions and 17 deletions

View File

@ -1,5 +1,8 @@
#include "Connection.h"
#include <winsock2.h>
#include <iostream>
Connection::Connection()
{
mySocket = NULL;
@ -59,13 +62,11 @@ bool Connection::Connect(unsigned short port , const char serverName[])
//connection succesfull!
return true;
}
bool Connection::InitiateServer(unsigned short port)
{
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
mySocket = socket(AF_INET, SOCK_STREAM, 0);
if(mySocket == SOCKET_ERROR)
{
//Error opening socket!

View File

@ -7,7 +7,7 @@
#include "IConnection.h"
class Connection : public IConnection
class Connection : public ::Oyster::Network::IConnection
{
private:
int mySocket;
@ -23,6 +23,7 @@ class Connection : public IConnection
virtual bool Send(const char message[]);
virtual int Recieve(char message[]);
virtual int Listen();
};
#endif

View File

@ -5,18 +5,21 @@
#ifndef NETWORK_DEPENDENCIES_I_CONNECTION_H
#define NETWORK_DEPENDENCIES_I_CONNECTION_H
#include <winsock2.h>
#include <iostream>
class IConnection
namespace Oyster
{
namespace Network
{
class IConnection
{
public:
virtual bool Connect( unsigned short port, const char serverName[] ) = 0;
virtual bool InitiateServer( unsigned short port ) = 0;
virtual void Disconnect() = 0;
virtual bool Send( const char message[] ) = 0;
virtual int Recieve(char message[]) = 0;
virtual int Listen() = 0;
};
virtual bool InitiateServer( unsigned short port ) { return false; };
virtual int Listen() { return -1; };
virtual bool Connect( unsigned short port, const char serverName[] ) { return false; };
};
}
}
#endif

View File

@ -0,0 +1,28 @@
#include "Client.h"
Client::Client()
{
connection = new Connection();
}
Client::~Client()
{
delete connection;
}
bool Client::Connect(unsigned int port, char filename[])
{
connection->Connect(port, filename);
return true;
}
void Client::Send(char msg[])
{
connection->Send(msg);
}
void Client::Recv(char msg[])
{
connection->Recieve(msg);
}

View File

@ -0,0 +1,26 @@
#ifndef NETWORK_CLIENT_CLIENT_H
#define NETWORK_CLIENT_CLIENT_H
/////////////////////////////////////////////////////////////////////
// Created by Pontus 2013
/////////////////////////////////////////////////////////////////////
#include "../NetworkDependencies/Connection.h"
class Client
{
public:
Client();
~Client();
bool Connect(unsigned int port, char filename[]);
void Send(char msg[]);
void Recv(char msg[]);
private:
Connection* connection;
};
#endif

View File

@ -1,10 +1,45 @@
#include <iostream>
#include "Client.h"
#include <WinSock2.h>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
void ShutdownSockets();
bool InitSockets();
int main()
{
char msgRecv[255] = "\0";
InitSockets();
cout << "Client" << endl;
//Create Client
Client client;
//Connect to server
client.Connect(9876, "127.0.0.1");
//Recieve message
client.Recv(msgRecv);
//print message
cout << msgRecv << endl;
ShutdownSockets();
system("pause");
return 0;
}
bool InitSockets()
{
WSADATA wsaData;
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
}
void ShutdownSockets()
{
WSACleanup();
}

View File

@ -149,8 +149,12 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Client.cpp" />
<ClCompile Include="ClientMain.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@ -18,5 +18,13 @@
<ClCompile Include="ClientMain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Client.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -0,0 +1,16 @@
#include "Client.h"
Client::Client(unsigned int socket)
{
connection = new Connection(socket);
}
Client::~Client()
{
delete connection;
}
void Client::Send(char buffer[])
{
connection->Send(buffer);
}

View File

@ -0,0 +1,24 @@
#ifndef NETWORK_SERVER_CLIENT_H
#define NETWORK_SERVER_CLIENT_H
/////////////////////////////////////////////////////////////////////
// Created by Pontus 2013
/////////////////////////////////////////////////////////////////////
#include "../NetworkDependencies/Connection.h"
class Client
{
public:
Client(unsigned int socket);
~Client();
void Send(char buffer[]);
private:
Connection* connection;
};
#endif

View File

@ -0,0 +1,16 @@
#ifndef NETWORK_SERVER_ILISTENER_H
#define NETWORK_SERVER_ILISTENER_H
/////////////////////////////////////////////////////////////////////
// Created by Pontus 2013
/////////////////////////////////////////////////////////////////////
class IListener
{
public:
virtual bool Init(unsigned int port) = 0;
virtual int Accept() = 0;
};
#endif

View File

@ -0,0 +1,32 @@
#include "Listener.h"
Listener::Listener()
{
}
Listener::~Listener()
{
if(connection)
{
delete connection;
}
}
bool Listener::Init(unsigned int port)
{
connection = new Connection();
connection->InitiateServer(port);
return true;
}
int Listener::Accept()
{
int clientSocket = 0;
clientSocket = connection->Listen();
return clientSocket;
}

View File

@ -0,0 +1,25 @@
#ifndef NETWORK_SERVER_LISTENER_H
#define NETWORK_SERVER_LISTENER_H
/////////////////////////////////////////////////////////////////////
// Created by Pontus 2013
/////////////////////////////////////////////////////////////////////
#include "IListener.h"
#include "../NetworkDependencies/Connection.h"
class Listener
{
public:
Listener();
~Listener();
bool Init(unsigned int port);
int Accept();
private:
Connection* connection;
};
#endif

View File

@ -149,8 +149,15 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Client.cpp" />
<ClCompile Include="Listener.cpp" />
<ClCompile Include="ServerMain.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h" />
<ClInclude Include="IListener.h" />
<ClInclude Include="Listener.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@ -18,5 +18,22 @@
<ClCompile Include="ServerMain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Client.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Listener.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Listener.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="IListener.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -1,10 +1,49 @@
#include <iostream>
#include <WinSock2.h>
#include "Listener.h"
#include "Client.h"
using namespace std;
#pragma comment(lib, "ws2_32.lib")
void ShutdownSockets();
bool InitSockets();
int main()
{
cout << "Server" << endl;
if(!InitSockets())
{
cout << "Sockets failed to initialize" << endl;
}
//Create socket
Listener listener;
listener.Init(9876);
//Start listening
int clientSocket = listener.Accept();
//Accept a client
Client client(clientSocket);
//Send a message to that client
client.Send("asd");
ShutdownSockets();
system("pause");
return 0;
}
bool InitSockets()
{
WSADATA wsaData;
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
}
void ShutdownSockets()
{
WSACleanup();
}