Caute,
mam nasledujuce 2 male programy - server a klient, na networking tu pouzivam SDL_net. Server by mal poslat 4x strukturu packet a klient by ju mal 4x prijat.
packet.h
#ifndef PACKET_H
#define PACKET_H
#define BLOCK_SIZE 65536
struct packet
{
unsigned char m_buffer[BLOCK_SIZE];
};
#endif
server.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <SDL/SDL.h>
#include <SDL/SDL_net.h>
#include "packet.h"
using namespace std;
TCPsocket g_serverSocketDescriptor = 0;
TCPsocket g_clientSocketDescriptor = 0;
IPaddress g_serverIP;
IPaddress * g_clientIP = 0;
void prepareConnection()
{
if (SDL_Init(0) == -1)
{
cerr << "SDL_Init: " << SDL_GetError() << endl;
exit(1);
}
if (SDLNet_Init() == -1)
{
cerr << "SDLNet_Init: " << SDLNet_GetError() << endl;
exit(2);
}
if (SDLNet_ResolveHost(&g_serverIP, 0, 2000) < 0)
{
cerr << "SDLNet_ResolveHost: " << SDLNet_GetError() << endl;
exit(3);
}
}
bool acceptConnection()
{
g_clientSocketDescriptor = SDLNet_TCP_Accept(g_serverSocketDescriptor);
return (g_clientSocketDescriptor) ? true : false;
}
void openSocket()
{
g_serverSocketDescriptor = SDLNet_TCP_Open(&g_serverIP);
if (!g_serverSocketDescriptor)
{
cerr << "SDLNet_TCP_Open: " << SDLNet_GetError() << endl;
exit(4);
}
}
void closeSocket()
{
if (g_clientSocketDescriptor)
SDLNet_TCP_Close(g_clientSocketDescriptor);
}
void closeConnection()
{
closeSocket();
SDLNet_TCP_Close(g_serverSocketDescriptor);
}
void send(const void * data, int len)
{
cout << "~ send\n";
if (SDLNet_TCP_Send(g_clientSocketDescriptor, data, len) < len)
{
cerr << "SDLNet_TCP_Send: " << SDLNet_GetError() << endl;
exit(6);
}
}
void recv(void * data, int len)
{
cout << "~ recv\n";
if (SDLNet_TCP_Recv(g_clientSocketDescriptor, data, len) < len)
{
cerr << "SDLNet_TCP_Recv: " << SDLNet_GetError << endl;
exit(7);
}
}
int main()
{
prepareConnection();
openSocket();
cout << "Waiting for client...\n";
while (!acceptConnection())
{
SDL_Delay(100);
continue;
}
cout << "Client connected\n";
packet p;
for (int i = 0; i < 4; i++)
send(&p, sizeof(packet));
closeConnection();
SDLNet_Quit();
SDL_Quit();
return 0;
}
client.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <SDL/SDL.h>
#include <SDL/SDL_net.h>
#include "packet.h"
using namespace std;
TCPsocket g_serverSocketDescriptor = 0;
IPaddress g_serverIP;
void prepareConnection()
{
if (SDL_Init(0) == -1)
{
cerr << "SDL_Init: " << SDL_GetError() << endl;
exit(1);
}
if (SDLNet_Init() == -1)
{
cerr << "SDLNet_Init: " << SDLNet_GetError() << endl;
exit(2);
}
if (SDLNet_ResolveHost(&g_serverIP, "127.0.0.1", 2000) < 0)
{
cerr << "SDLNet_ResolveHost: " << SDLNet_GetError() << endl;
exit(3);
}
}
void openSocket()
{
g_serverSocketDescriptor = SDLNet_TCP_Open(&g_serverIP);
if (!g_serverSocketDescriptor)
{
cerr << "SDLNet_TCP_Open: " << SDLNet_GetError() << endl;
exit(4);
}
}
void closeConnection()
{
if (g_serverSocketDescriptor)
SDLNet_TCP_Close(g_serverSocketDescriptor);
}
void send(const void * data, int len)
{
cout << "~ send\n";
if (SDLNet_TCP_Send(g_serverSocketDescriptor, data, len) < len)
{
cerr << "SDLNet_TCP_Send: " << SDLNet_GetError() << endl;
exit(6);
}
}
void recv(void * data, int len)
{
cout << "~ recv\n";
if (SDLNet_TCP_Recv(g_serverSocketDescriptor, data, len) < len)
{
cerr << "SDLNet_TCP_Recv: " << SDLNet_GetError() << endl;
exit(7);
}
}
int main()
{
prepareConnection();
openSocket();
packet p;
for (int i = 0; i < 4; i++)
recv(&p, sizeof(packet));
closeConnection();
SDLNet_Quit();
SDL_Quit();
return 0;
}
kompilacia:
g++ server.cpp -o server -lSDL -lSDL_net
g++ client.cpp -o client -lSDL -lSDL_net
vystup:
$ ./server
Waiting for client...
Client connected
~ send
~ send
~ send
SDLNet_TCP_Send: accept() failed
$ ./client
~ recv
~ recv
SDLNet_TCP_Recv:
(ano, u SDLNet_TCP_Recv je chybova hlaska prazdna)
Z nejakeho dovodu ma klient problem prijat vsetky data, co vedie ku volaniu exit(). Pokial viem tak recv caka dovtedy, dokym mu nepridu data o velkosti len - to je predsa jedna z hlavnych uloh TCP. Niekedy to zbehne bez erroru, ale vacsinou vyhodi tuto chybu. Takze chcel by som sa opytat preco to robi resp. nerobi a ako to opravit.
Predom dakujem za odpovede