packet size sdlnet

Started by
3 comments, last by Mr.L 12 years ago
i'm trying to increase the packet size of my programm, but if its higher than 1024bytes it doesn't work and if its lower, it beeps at the beginning.
what am I doing wrong

server code:

#include <SDL.h>
#include <SDL/SDL_net.h>
#include <stdlib.h>
#include <conio.h>
#define Psize 1024
SDL_Surface *screen;
TCPsocket server;
void init_sdl (void)
{
if (SDL_Init (SDL_INIT_VIDEO) < 0)
exit (-1);
atexit (SDL_Quit);
}
void init_net (void)
{
IPaddress addr;
if (SDLNet_Init () < 0)
{
printf ("ERR Net: %s\n", SDLNet_GetError ());
exit (-1);
}
if (SDLNet_ResolveHost (&addr, NULL, 2210) < 0)
{
printf ("ERR ResolveHost: %s\n", SDLNet_GetError ());
SDLNet_Quit ();
exit (-1);
}
server = SDLNet_TCP_Open (&addr);
if (server == NULL)
{
printf ("ERR TCP_Open: %s\n", SDLNet_GetError ());
SDLNet_Quit ();
exit (-1);
}
}
#ifdef _WIN32
#undef main
#endif
int main (void)
{
TCPsocket client = NULL;
SDL_Rect *rect;
int maxread;
init_sdl ();
init_net ();
SDLNet_SocketSet set;
printf ("Server-Socket ist offen\n");
while (client == NULL)
{
/* eine Sekunde warten */
SDL_Delay (1000);
client = SDLNet_TCP_Accept (server);
}
set = SDLNet_AllocSocketSet (1);if(!set){exit(0);}
if(SDLNet_TCP_AddSocket (set, client)!=1){exit(0);};
printf ("Client wurde akzeptiert\n");
/* Schicke erstes Rechteck */
rect = (SDL_Rect *)malloc (sizeof (SDL_Rect));
rect->x = 10;
rect->y = 20;
rect->w = 100;
rect->h = 200;
char data[Psize];
bool check=false;
char d[Psize];
bool g=false;
bool s=false;
char * datab="__________________";

SDLNet_TCP_Send (client, d, Psize);
char input;
system("color 5a");
while(true){
check=!check;
//char g[12];
//itoa(rand(),g,10);
//input=game();
SDLNet_CheckSockets(set,0);
//printf("lol");
//printf("%i",check);
sprintf(d,"ping%i\n",rand());
if (SDLNet_SocketReady (client))
{
if(check||g){
g=false;
s=true;
maxread = SDLNet_TCP_Recv (client, &data, sizeof(data));
//system("color 4a");
//printf("data:%s\n",data);
//char* ki;
//sprintf(ki,"color %i%i",(rand()%9)+1,(rand()%9)+1);
//system(ki);
//data;
printf("\n%s\n",data);
}
if(!check||s){
s=false;
g=true;
char buf[Psize];
static int pa=0;
pa++;
sprintf(buf,"pong%i",pa);
char b[Psize];
sprintf(b,"%c",input);
//printf(b);
SDLNet_TCP_Send (client, buf, sizeof (buf));}

}
printf(":");
//SDL_Delay(350);
//printf("data:\t%s\n",datab);
//datab="__________________";
}
printf ("I am Server. \n Read = %d\n", maxread);
printf ("x = %d, y = %d, w = %d, h = %d\n",
rect->x, rect->y, rect->w, rect->h);
SDLNet_TCP_Close (client);
SDLNet_TCP_Close (server);
return 0;
}



client code:

#include <SDL.h>
#include <SDL/SDL_net.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef _WIN32
#undef main
#endif
#define Psize 1024
bool field[9];
#include "draw.h"
int main (void)
{
for (int i=0;i<9;i++){field=false;}
IPaddress address;
TCPsocket sock_server;
SDLNet_SocketSet set;
if(SDL_Init (0) ==-1)
{
printf ("SDL_Init: %s\n", SDL_GetError ());
exit (1);
}
if(SDLNet_Init () ==-1)
{
printf ("SDLNet_Init: %s\n", SDLNet_GetError ());
exit(1);
}
if (SDLNet_ResolveHost (&address, "95.174.229.136", 2210) == -1)
{
printf ("SDLNet_ResolveHost: %s\n", SDLNet_GetError ());
SDLNet_Quit ();
exit (1);
}
sock_server = SDLNet_TCP_Open (&address);
if (!sock_server)
{
printf ("SDLNet_TCP_Open: %s\n", SDLNet_GetError ());
SDLNet_Quit ();
exit (1);
}
set = SDLNet_AllocSocketSet (1);
printf ("Init finished. Connected to server. Send Data after 5 Seconds...\n");
if (SDLNet_TCP_AddSocket (set, sock_server )!= 1)
{
printf ("SDLNet_TCP_AddSocket: %s\n", SDLNet_GetError ());
exit (1);
}
/*SDL_Delay (5000);
*/
printf("._.");
char data[Psize];
char buf[Psize];
bool c=false;
bool s=false;
bool g=false;
//SDLNet_TCP_Send (sock_server,"loool", Psize);
//time_t start = time(NULL) * 1000;
system("color e1");
while(true){
//int start=SDL_GetTicks();
SDLNet_CheckSockets (set, 0);
//printf("%i",start);
c=!c;
//printf("%ics\n",SDLNet_SocketReady(sock_server));
if(SDLNet_SocketReady(sock_server)){
if(!c||g){
SDLNet_TCP_Recv (sock_server, &data, sizeof(data));
s=true;
g=true;
//printf("data got\n");
printf("\n%s\n",data);
}
if(c||s){
s=false;
g=true;
static int pa=0;
pa++;
sprintf(buf,"pong%i",pa);
SDLNet_TCP_Send (sock_server, buf, sizeof(buf));
}
}

printf(":");
//SDL_Delay(12);
//draw(data[0]);
//printf("version 1.1");

}

SDLNet_TCP_Close (sock_server);
return 0;
}
Advertisement
Hello Mr.L.
I can see that you are a beginner, here are some general advice:
Don't use 2 spaces indents, it makes the code unreadable, just 4 or 8 spaces (most people including myself prefer 4 spaces).
You have a malloc in your main, which does not make any sense, in most case you use malloc to allocate memory on the heap when you need a piece of memory to live outside the function. The main function is your highest function and thus allocating stuff on the heap there makes no sense, try googling "stack vs heap" to learn more about this.
You have very bad variable names like, a, b, and g, I have no idea what they are used for, please rename that into a descriptive name what they actually represent, like has_more_packets or similar.
In your server, you do

while(true) {
..
}
Do more stuff....

Do you realize that true will always be true, so whatever code you have in "Do more stuff" will never happen.

In your server, you set check, a and g to false.
Then you set check to !check which means check is set to true.
Then if the client is ready, you check if (check or g) which means you check for true or false, which will execute this clause.
Then you check if (!check or s), since you set s to true in previous clause this will also execute.
They way your program is construct is messy, I know that you are new in programming so I suggest you buy this book: http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882, and when you read it, you refactor your code so it becomes easier to understand you will likely find why your program behaves like it does.
okay it's not my best programm i guess.
That with the variables was just because of laziness, the malloc and the Rectangle were a relict that i forgot to remove.
But I'm ashamed because of the check, now i removed and it works faster now.

Thanks

That with the variables was just because of laziness


You are posting on a public message board, asking complete strangers for help.
Being lazy in posting your question means that you are saying "I value my own time more than I value the time of those answering my question."
For someone who knows little, who wants to get help from those who know more, it's un-likely that that attitude is going to get you very far.

So, for any code that you post, I suggest that you do the following:
1) Check the return values. Put in assert() statements if you know what they're supposed to return.
2) Print the input/output of each function. Post a log of the prints together with the code, so the reader can follow along.
3) Give each variable a descriptive name. This will reduce the amount of effort a potential answerer will have to spend to understand your program, and thus increase the likelihood that you will get a good answer.
4) Also describe what you expect the program to do, what the program seems to be doing based on your observations, and what problem solving tactics you've already tried and what their result was.

If you follow this advice, you're likely to get much better answers. You're also likely to actually solve the problem yourself as part of clearly expressing the problem in a post -- happens to me all the time!
enum Bool { True, False, FileNotFound };
sorry, next time, i will do.

This topic is closed to new replies.

Advertisement