Problem with SDL2_Net

Hey guys, I am building a kind of engine in SDL2 and its plugins and C language. It is going well. I’ve made almost all graphics functions, detections, mouse, keyboard. But the problem now is in the tcp connection.
I have created a struct named connection which has all the tcp and udp parameters. The connection goes well in both, client and server, while I am only doing an individual action. For example, if I send something from server to client, I receive it well, but if after receive I try to send an answer from client to server or vice-versa The program stops. Does somebody have any idea what this might be?

I am posting the code where I start the internet parameters and the other codes too. Thanks.

Code:

PLACE_CALL STATUS place_start (PLACE * to) {
if(to!=NULL) {
if(to->machine==NULL)to->machine = machine_standard(0);
if(SDL_Init(to->machine->INIT_FLAG)<0) {
fprintf(stderr,“SDL could not initialize! SDL_Error: %s\n”,SDL_GetError());
return(Off);
}
srand((unsigned)time(NULL));
to->machine->FRAME = SDL_CreateWindow(to->machine->TITLE,to->machine->X,to->machine->Y,to->machine->WIDTH,to->machine->HEIGHT,to->machine->VIDEO_FLAG);
if(to->machine->FRAME == NULL) {
fprintf(stderr,“Window could not be created! SDL_Error: %s\n”,SDL_GetError());
return(Off);
}
else {//Get window surface
to->machine->RENDERER = SDL_CreateRenderer(to->machine->FRAME,-1,SDL_RENDERER_TARGETTEXTURE);
if(to->machine->RENDERER==NULL) {
fprintf(stderr,“Renderer could not be created: %s\n”,SDL_GetError());
return(Off);
}
else {
SDL_SetRenderDrawColor(to->machine->RENDERER,0x0,0x0,0x0,0xFF);
//Initialize PNG loading
int imgFlags = IMG_INIT_JPG|IMG_INIT_PNG|IMG_INIT_TIF;
if(!( IMG_Init( imgFlags ) & imgFlags )) {
fprintf(stderr,“SDL_image could not initialize! SDL_image Error: %s\n”, IMG_GetError() );
return(Off);
}
}
}
if(Mix_OpenAudio(to->machine->audio->frequency,to->machine->audio->format,to->machine->audio->channel,to->machine->audio->buffer) != 0) {
fprintf(stderr,“Could not init audio!\n”);
}
if( TTF_Init() == -1 )fprintf(stderr,“Could not init ttf!\n”);
setlocale(LC_ALL,NULL);
#ifdef WIN32
to->text_info = text_set(“c:/windows/fonts/arial.ttf”,26,color_set(200,200,200,255));
#else
to->text_info = text_set(“usr/fonts/arial.ttf”,26,color_set(200,200,200,255));
#endif
SDL_StartTextInput();
if(SDLNet_Init()<0) {
fprintf(stderr,“Could not init the web\n”);
}
else {
if (!to->internet->hostname&&SDLNet_ResolveHost(&to->internet->ip,NULL,to->internet->port) < 0) {
fprintf(stderr, “SDLNet_ResolveHost: %s\n”, SDLNet_GetError());
}
if(!to->internet->hostname&&(to->internet->tcp_server = SDLNet_TCP_Open(&to->internet->ip))) {
fprintf(stdout,“TCP server opened: %s\n”,SDLNet_GetError());
}
if (to->internet->hostname&&SDLNet_ResolveHost(&to->internet->ip,to->internet->hostname,to->internet->port) < 0) {
fprintf(stderr, “SDLNet_ResolveHost: %s\n”, SDLNet_GetError());
}
if(to->internet->hostname&&(to->internet->tcp_client = SDLNet_TCP_Open(&to->internet->ip))) {
fprintf(stdout,“TCP client opened: %s\n”,SDLNet_GetError());
}
if((to->internet->udp = SDLNet_UDP_Open(to->internet->port))) {
fprintf(stderr,“UDP connected\n”);
}
to->internet->socket = SDLNet_AllocSocketSet(2);
if(!to->internet->socket) {
fprintf(stderr,“Could not alloc socket server: %s\n”, SDLNet_GetError());
}
if(!to->internet->hostname&&SDLNet_TCP_AddSocket(to->internet->socket,to->internet->tcp_server)==-1) {
fprintf(stderr,“Could not add socket: %s\n”,SDLNet_GetError());
}
if(to->internet->tcp_client&&SDLNet_TCP_AddSocket(to->internet->socket,to->internet->tcp_client)==-1) {
fprintf(stderr,“Could not add socket client: %s\n”,SDLNet_GetError());
}
}
}
return(On);
}

/// INTERNET FUNCTION BEGIN

PLACE_CALL STATUS internet_tcp_accept (PLACE * to) {
if(!to||!to->internet||!to->internet->socket)return(Off);
int numready = SDLNet_CheckSockets(to->internet->socket,(Uint32)-1);
if(numready<=0)return(Off);
if(SDLNet_SocketReady(to->internet->tcp_server)) {
if((to->internet->tcp_client=SDLNet_TCP_Accept(to->internet->tcp_server)))return(On);
}
return(Off);
}

PLACE_CALL STATUS internet_tcp_connect (PLACE * to) {
if(!to||!to->internet||!to->internet->tcp_client)return(Off);
if ((to->internet->ipRemote = SDLNet_TCP_GetPeerAddress(to->internet->tcp_client)))return(On);
return(Off);
}

PLACE_CALL STATUS internet_tcp_recv (TCPsocket sock, char ** buffer) {
if(!sock)return(Off);
Uint32 len,result;
len=SDL_SwapBE32(len);
result=SDLNet_TCP_Recv(sock,&len,sizeof(len));
if(result<sizeof(len))return(Off);
/* swap byte order to our local order /
len=SDL_SwapBE32(len);
/
check if anything is strange, like a zero length buffer /
if(!len)return(Off);
/
allocate the buffer memory /
if(!(buffer=(char)calloc(len,sizeof(char))))return(Off);
/
get the string buffer over the socket */
result=SDLNet_TCP_Recv(sock,*buffer,len);
if(result<len) {
free(*buffer);
buffer=NULL;
return(Off);
}
return(On);
}

PLACE_CALL STATUS internet_tcp_send (TCPsocket sock, char buffer) {
Uint32 len,result;
if(!sock||!buffer || !strlen(buffer))return(Off);
/
determine the length of the string /
len=strlen(buffer)+1; /
add one for the terminating NULL /
/
change endianness to network order /
len=SDL_SwapBE32(len);
/
send the length of the string /
result=SDLNet_TCP_Send(sock,&len,sizeof(len));
if(result<sizeof(len))return(Off);
/
revert to our local byte order /
len=SDL_SwapBE32(len);
/
send the buffer, with the NULL as well /
result=SDLNet_TCP_Send(sock,buffer,len);
if(result<len)return(Off);
/
return the length sent */
return(On);
}

PLACE_CALL const char * internet_machine_name(PLACE * to) {
if(!to||!to->internet)return(NULL);
return(SDLNet_ResolveIP(&to->internet->ip));
}

PLACE_CALL const char * internet_machine_ip(PLACE * to) {
char * ip = (char *)calloc(16,sizeof(char));
Uint32 ipaddr=SDL_SwapBE32(to->internet->ip.host);
sprintf(ip,"%.3d.%.3d.%.3d.%.3d",ipaddr>>24,(ipaddr>>16)&0xff,(ipaddr>>8)&0xff,ipaddr&0xff);
return(ip);
}

/// INTERNET FUNCTION END

Those are the engine’s function. And here it is the server code.

Code:

#include <place.h>

SPRITE * sprite_global = NULL;

static STATUS init_server(PLACE ** manager);

int main(int c,char ** v) {
PLACE * manager = NULL;
init_server(&manager);
STATUS running = On;
do {
if(internet_tcp_accept(manager)) {
if(internet_tcp_connect(manager)) {
fprintf(stdout,"\nHostname: %s\n",internet_machine_name(manager));
fprintf(stdout,“IP Address : %s\n”,internet_machine_ip(manager));
fprintf(stdout,“Connected to %x %d\n”,SDLNet_Read32(&manager->internet->ipRemote->host),SDLNet_Read16(&manager->internet->ipRemote->port));
}
else
fprintf(stderr, “*Error while connecting: %s\n”, SDLNet_GetError());
char * buffer = NULL;
while(internet_tcp_recv(manager->internet->tcp_client,&buffer)) {
if(strlen(buffer)==4) {
char exit[4] = {‘e’,‘x’,‘i’,‘t’};
int x;
for(x=0;x<4;x++){
if(tolower(buffer[x])==exit[x]){}
}
running = Off;
}
if(strlen(buffer)==15) {
char inveigh[15] = {‘v’,‘a’,‘i’,’ ‘,‘t’,‘o’,‘m’,‘a’,‘r’,’ ‘,‘n’,‘o’,’ ',‘c’,‘u’};
int x,status=1;
for(x=0;x<15;x++){
if(tolower(buffer[x])!=inveigh[x]){status=0;}
}
if(status){
fprintf(stdout,“Devolveu com for?a\n”);
internet_tcp_send(manager->internet->tcp_client,“Vai se foder\n”);
status = 0;
}
}
printf(“received %s %d\n”,buffer,strlen(buffer));
}
}
} while(running);
place_destroy(manager);
return(0);
}

static STATUS init_server(PLACE ** manager) {
MACHINE * machine = machine_set(0,“Sever2Client”,1024,768,0,0,SDL_INIT_AUDIO,SDL_WINDOW_HIDDEN,audio_standard());
*manager = place_set(0,machine,connection_set(NULL,32000),NULL);
if(!place_start(*manager)) {
fprintf(stderr,“Could not init the server\n”);
return(Off);
}
return(On);
}

And here it is the client’s code:

Code:

#include <place.h>

static STATUS client_init(PLACE ** where);

int main(int c,char ** v) {
PLACE * where = NULL;
if(!client_init(&where)) {
fprintf(stderr,“Could not init the fucking machine\n”);
exit(-1);
}
char message[1024];
STATUS running = On;
do {
printf(“Digite sua mensagem: “);
gets(message);
if(!internet_tcp_send(where->internet->tcp_client,message))fprintf(stderr,“Failed to send: %s\n”,connection_error());
char off[] = {‘e’,‘x’,‘i’,‘t’};
STATUS go_away = On;
int x;
for(x=0;x<4;x++){
if(tolower(message[x])!=off[x]){go_away = Off;}
}
if(go_away == On)running = Off;
/*
char * text = NULL;
if(internet_tcp_recv(where->internet->tcp_client,&text)) {
printf(”%s\n”,text);
}*/
} while(running);
place_destroy(where);
return(0);
}

static STATUS client_init(PLACE ** where) {
MACHINE * machine = machine_set(0,“Client2Sever”,1024,768,0,0,SDL_INIT_AUDIO,SDL_WINDOW_HIDDEN,audio_standard());
*where = place_set(0,machine,connection_set(“localhost”,32000),NULL);
if(!place_start(*where)) {
return(Off);
}
return(On);
}