Patrick’s development blog

Day 2: Handle code using classes

Posted in Uncategorized by Patrick on April 16, 2008

I’m currently trying to create a thread for the client that receives messages from the server. These messages are later going to be sent to all the clients currently connected to the server, but for now it only sends to one client. The client class is almost finished, there are only some small details left. I haven’t done much on the server part of the class yet, i’ll do that next time I work on this project.

This is how my client looks like right now.

#include
#include

#include “client.h”

SDL_Surface *screen = NULL;
SDL_Event event;

int main(int argc, char* args[]) {
if (SDLNet_Init() < 0) {
std::cout << SDLNet_GetError();
exit(1);
}

std::cout << “SDL_net was initialized sucessfully!” << std::endl;

IPaddress ip;
TCPsocket tcpsock;
const int PORT = 2000;
char buffer[512];

if (SDLNet_ResolveHost(&ip, “localhost”, PORT) < 0) {
std::cout << “SDLNet_ResolveHost: ” << SDLNet_GetError();
exit(1);
}

std::cout << “Resolved host at port ” << PORT << std::endl;

tcpsock = SDLNet_TCP_Open(&ip);
if (!tcpsock) {
std::cout << “SDLNet_TCP_Open: ” << SDLNet_GetError();
exit(1);
}

std::cout << “Connected to server using a TCP-socket on port ” << PORT << std::endl;

bool done = false;

Client client_h(tcpsock); // this class is used to receive messages from the server sort of

while (!done) {
std::cout <“;
std::cin.getline(buffer, 512);

if (SDLNet_TCP_Send(tcpsock, static_cast(buffer), 512) < 512) {
std::cerr << “SDLNet_TCP_Send: ” << SDLNet_GetError();
return 0;
}
}

SDLNet_TCP_Close(tcpsock);
SDLNet_Quit();

return 0;
}

I’ll write about SDL_Net in more detail another time. Right now i’m fully occupied with this project. Things are going more smootly now though.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: