Fixade crash och flyttade initsockets, shutdownsockets.

Moved initsocket and shutdownsockets to WinsockFunctions.
This commit is contained in:
Pontus Fransson 2013-11-25 20:27:23 +01:00
parent 6b57815bb8
commit 45c145439a
9 changed files with 57 additions and 52 deletions

View File

@ -85,11 +85,8 @@ bool Connection::Send(const unsigned char message[])
{
int nBytes;
unsigned long messageSize = strlen((char*)message);
int optlen = sizeof(int);
int optval = 0;
getsockopt(this->socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
messageSize = 255;
messageSize = 1600;
nBytes = send(this->socket, (char*)message , messageSize, 0);
if(nBytes == SOCKET_ERROR)
{
@ -97,6 +94,8 @@ bool Connection::Send(const unsigned char message[])
return false;
}
std::cout << "Size of the sent data: " << nBytes << " bytes" << std::endl;
return true;
}
@ -104,7 +103,7 @@ int Connection::Recieve(unsigned char message[])
{
int nBytes;
nBytes = recv(this->socket, (char*)message , 255, 0);
nBytes = recv(this->socket, (char*)message, 1600, 0);
if(nBytes == SOCKET_ERROR)
{
//Recv failed
@ -118,6 +117,11 @@ int Connection::Recieve(unsigned char message[])
int Connection::Listen()
{
int optlen = sizeof(int);
int optval;
int Return = getsockopt(this->socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
std::cout << optval << std::endl;
int clientSocket;
if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
{

View File

@ -99,12 +99,12 @@ void MessageHeader::PackFloat(float i, unsigned char msg[])
void MessageHeader::PackFloat(float i[], unsigned int elementCount, unsigned char msg[])
{
//Pack number of elements
PackUnsignedInt(elementCount, &msg[size]);
PackUnsignedInt(elementCount, msg);
//Pack all elements
for(int j = 0; j < elementCount; j++)
{
PackFloat(i[j], &msg[size]);
PackFloat(i[j], msg);
}
}
@ -204,12 +204,12 @@ float* MessageHeader::UnpackFloat(unsigned int& elementCount, unsigned char msg[
{
float* i;
elementCount = UnpackUnsignedInt(&msg[size]);
elementCount = UnpackUnsignedInt(msg);
i = new float[elementCount];
for(int j = 0; j < elementCount; j++)
{
i[j] = UnpackFloat(&msg[size]);
i[j] = UnpackFloat(msg);
}
return i;

View File

@ -166,6 +166,7 @@
<ClCompile Include="Messages\MessageTest.cpp" />
<ClCompile Include="Packing.cpp" />
<ClCompile Include="Translator.cpp" />
<ClCompile Include="WinsockFunctions.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Connection.h" />
@ -179,6 +180,7 @@
<ClInclude Include="ITranslate.h" />
<ClInclude Include="Protocols.h" />
<ClInclude Include="Translator.h" />
<ClInclude Include="WinsockFunctions.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -8,6 +8,7 @@
<ClCompile Include="Packing.cpp" />
<ClCompile Include="Translator.cpp" />
<ClCompile Include="Listener.cpp" />
<ClCompile Include="WinsockFunctions.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Connection.h" />
@ -21,5 +22,6 @@
<ClInclude Include="Messages\MessagesInclude.h" />
<ClInclude Include="Listener.h" />
<ClInclude Include="IListener.h" />
<ClInclude Include="WinsockFunctions.h" />
</ItemGroup>
</Project>

View File

@ -16,7 +16,7 @@ namespace Oyster
class Translator : public ITranslate
{
public:
Translator () { msg = new unsigned char[256]; };
Translator () { msg = new unsigned char[1601]; };
~Translator() { if(msg != NULL) { delete [] this->msg; }};
unsigned char* Pack (Protocols::ProtocolHeader &header );

View File

@ -0,0 +1,13 @@
#include "WinsockFunctions.h"
#include <WinSock2.h>
bool InitSockets()
{
WSADATA wsaData;
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
}
void ShutdownSockets()
{
WSACleanup();
}

View File

@ -0,0 +1,11 @@
#ifndef NETWORK_DEPENDENCIES_WINSOCK_FUNCTIONS_H
#define NETWORK_DEPENDENCIES_WINSOCK_FUNCTIONS_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
void ShutdownSockets();
bool InitSockets();
#endif

View File

@ -1,18 +1,17 @@
#include <iostream>
#include "Client.h"
#include <WinSock2.h>
#include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h"
#include "..\NetworkDependencies\Translator.h"
#include "..\NetworkDependencies\Protocols.h"
#include "Client.h"
#pragma comment(lib, "ws2_32.lib")
using namespace std;
using namespace Oyster::Network::Protocols;;
using namespace Oyster::Network::Client;
#pragma comment(lib, "ws2_32.lib")
void ShutdownSockets();
bool InitSockets();
void chat(Client &client);
int main()
@ -37,22 +36,11 @@ int main()
return 0;
}
bool InitSockets()
{
WSADATA wsaData;
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
}
void ShutdownSockets()
{
WSACleanup();
}
void chat(Client &client)
{
Oyster::Network::Translator *t = new Oyster::Network::Translator();
unsigned char msgRecv[2560] = "\0";
unsigned char msgRecv[1601] = "\0";
string msgSend = "";
ProtocolSet* set = new ProtocolSet;
@ -82,8 +70,9 @@ void chat(Client &client)
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ' ;
//cout << set->Protocol.pTest->f[i] << ' ' ;
}
cout << endl;
break;
}
@ -100,8 +89,6 @@ void chat(Client &client)
msgSend = "ERROR!";
}
test.packageType = package_type_test;
test.size = msgSend.length();
test.textMessage = msgSend;
unsigned char *message = t->Pack(test);

View File

@ -1,24 +1,21 @@
#include <iostream>
#include <WinSock2.h>
#include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h"
#include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/Translator.h"
#include "Client.h"
#include "../NetworkDependencies/Packing.h"
using namespace std;
using namespace Oyster::Network::Server;
#pragma comment(lib, "ws2_32.lib")
void ShutdownSockets();
bool InitSockets();
#include "../NetworkDependencies/Translator.h"
using namespace std;
using namespace Oyster::Network::Server;
using namespace Oyster::Network;
using namespace ::Protocols;
int main()
{
unsigned char* recvBuffer = new unsigned char[5000];
unsigned char* recvBuffer = new unsigned char[1601];
cout << "Server" << endl;
Translator t;
@ -46,9 +43,9 @@ int main()
ProtocolTest test;
test.clientID = 0;
test.textMessage = "hej";
test.numOfFloats = 500;
test.numOfFloats = 350;
test.f = new float[test.numOfFloats];
float temp = 500.456f;
float temp = 395.456f;
for(int i = 0; i < test.numOfFloats; i++)
{
test.f[i] = temp;
@ -57,7 +54,7 @@ int main()
recvBuffer = t.Pack(test);
client1.Send(recvBuffer);
while(1)
{
client1.Recv(recvBuffer);
@ -94,14 +91,3 @@ int main()
system("pause");
return 0;
}
bool InitSockets()
{
WSADATA wsaData;
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
}
void ShutdownSockets()
{
WSACleanup();
}