<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[sdlnet beidseitiges check sockets]]></title><description><![CDATA[<p>irgendwas komisches läuft mit sdl_net:<br />
Wenn ich ich entweder beim sdl client oder server(tcp) über checksocket und socketready überprüfe ob das socket bereit ist, funktioniert das programm ganz gut, aber sobald beide die verbindung prüfen, gibt es kein freizeichen mehr, woran liegt das, dh wie kann ich das lösen</p>
<pre><code class="language-cpp">#include &lt;SDL.h&gt;
#include &lt;SDL/SDL_net.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#ifdef _WIN32
#undef main
#endif
int main (void)
{ 
  IPaddress address;
  TCPsocket sock_server;
  SDLNet_SocketSet set;

  if(SDL_Init (0) ==-1)
    {
      printf (&quot;SDL_Init: %s\n&quot;, SDL_GetError ());
      exit (1);
    }
  if(SDLNet_Init () ==-1)
    {
      printf (&quot;SDLNet_Init: %s\n&quot;, SDLNet_GetError ());
     exit(1);
    }
  if (SDLNet_ResolveHost (&amp;address, &quot;213.221.194.72&quot;, 10000) == -1)
    {
      printf (&quot;SDLNet_ResolveHost: %s\n&quot;, SDLNet_GetError ());
      SDLNet_Quit ();
      exit (1);
    }
  sock_server = SDLNet_TCP_Open (&amp;address);
  if (!sock_server)
    {
      printf (&quot;SDLNet_TCP_Open: %s\n&quot;, SDLNet_GetError ());
      SDLNet_Quit ();
      exit (1);
    }
  set = SDLNet_AllocSocketSet (1);
  printf (&quot;Init finished. Connected to server. Send Data after 5 Seconds...\n&quot;);
  if (SDLNet_TCP_AddSocket (set, sock_server )!= 1)
        {
          printf (&quot;SDLNet_TCP_AddSocket: %s\n&quot;, SDLNet_GetError ());
          exit (1);
        }
  /*SDL_Delay (5000);
*/
  char data[128];
  while(true){
    printf(&quot;%ics\n&quot;,SDLNet_CheckSockets (set, 0));
  if(SDLNet_SocketReady(sock_server)){
SDLNet_TCP_Send (sock_server, &quot;pong\n&quot;, 128);
  SDLNet_TCP_Recv (sock_server, &amp;data, 128);
  printf(&quot;%s\n&quot;,data);
  }
  printf(&quot;lol\n&quot;);

  }

  SDLNet_TCP_Close (sock_server);
  return 0;
}
</code></pre>
<pre><code class="language-cpp">#include &lt;SDL.h&gt;
#include &lt;SDL/SDL_net.h&gt;
#include &lt;stdlib.h&gt;

SDL_Surface *screen;
TCPsocket server;

void init_sdl (void)
{
	if (SDL_Init (SDL_INIT_VIDEO) &lt; 0)
		exit (-1);
	atexit (SDL_Quit);
}

void init_net (void)
{
	IPaddress addr;
	if (SDLNet_Init () &lt; 0)
	{
		printf (&quot;ERR Net: %s\n&quot;, SDLNet_GetError ());
		exit (-1);
	}
	if (SDLNet_ResolveHost (&amp;addr, NULL, 10000) &lt; 0)
	{
		printf (&quot;ERR ResolveHost: %s\n&quot;, SDLNet_GetError ());
		SDLNet_Quit ();
		exit (-1);
	}
	server = SDLNet_TCP_Open (&amp;addr);
	if (server == NULL)
	{
		printf (&quot;ERR TCP_Open: %s\n&quot;, 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 (&quot;Server-Socket ist offen\n&quot;);
	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 (&quot;Client wurde akzeptiert\n&quot;);
	/* Schicke erstes Rechteck */
	rect = (SDL_Rect *)malloc (sizeof (SDL_Rect));
	rect-&gt;x = 10;
	rect-&gt;y = 20;
	rect-&gt;w = 100;
	rect-&gt;h = 200;
	char data[128];

	while(true){

		//char g[12];
		//itoa(rand(),g,10);

			printf (&quot;%i&quot;,SDLNet_CheckSockets(set,0));
			//printf(&quot;lol&quot;);

		char d[33];

		sprintf(d,&quot;ping%i\n&quot;,rand());
		if (SDLNet_SocketReady (client))
            {

		maxread = SDLNet_TCP_Recv (client, &amp;data, 128);
		SDLNet_TCP_Send (client, d, 128);

		printf(&quot;%s\n&quot;,data);
		}

			printf(&quot;lol\n&quot;);

	}
	printf (&quot;I am Server. \n Read = %d\n&quot;, maxread);
	printf (&quot;x = %d, y = %d, w = %d, h = %d\n&quot;,
		rect-&gt;x, rect-&gt;y, rect-&gt;w, rect-&gt;h);
	SDLNet_TCP_Close (client);
	SDLNet_TCP_Close (server);
	return 0;

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/301044/sdlnet-beidseitiges-check-sockets</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 08:35:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/301044.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 17 Mar 2012 22:02:21 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to sdlnet beidseitiges check sockets on Sat, 17 Mar 2012 22:03:36 GMT]]></title><description><![CDATA[<p>irgendwas komisches läuft mit sdl_net:<br />
Wenn ich ich entweder beim sdl client oder server(tcp) über checksocket und socketready überprüfe ob das socket bereit ist, funktioniert das programm ganz gut, aber sobald beide die verbindung prüfen, gibt es kein freizeichen mehr, woran liegt das, dh wie kann ich das lösen</p>
<pre><code class="language-cpp">#include &lt;SDL.h&gt;
#include &lt;SDL/SDL_net.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#ifdef _WIN32
#undef main
#endif
int main (void)
{ 
  IPaddress address;
  TCPsocket sock_server;
  SDLNet_SocketSet set;

  if(SDL_Init (0) ==-1)
    {
      printf (&quot;SDL_Init: %s\n&quot;, SDL_GetError ());
      exit (1);
    }
  if(SDLNet_Init () ==-1)
    {
      printf (&quot;SDLNet_Init: %s\n&quot;, SDLNet_GetError ());
     exit(1);
    }
  if (SDLNet_ResolveHost (&amp;address, &quot;213.221.194.72&quot;, 10000) == -1)
    {
      printf (&quot;SDLNet_ResolveHost: %s\n&quot;, SDLNet_GetError ());
      SDLNet_Quit ();
      exit (1);
    }
  sock_server = SDLNet_TCP_Open (&amp;address);
  if (!sock_server)
    {
      printf (&quot;SDLNet_TCP_Open: %s\n&quot;, SDLNet_GetError ());
      SDLNet_Quit ();
      exit (1);
    }
  set = SDLNet_AllocSocketSet (1);
  printf (&quot;Init finished. Connected to server. Send Data after 5 Seconds...\n&quot;);
  if (SDLNet_TCP_AddSocket (set, sock_server )!= 1)
        {
          printf (&quot;SDLNet_TCP_AddSocket: %s\n&quot;, SDLNet_GetError ());
          exit (1);
        }
  /*SDL_Delay (5000);
*/
  char data[128];
  while(true){
    printf(&quot;%ics\n&quot;,SDLNet_CheckSockets (set, 0));
  if(SDLNet_SocketReady(sock_server)){
SDLNet_TCP_Send (sock_server, &quot;pong\n&quot;, 128);
  SDLNet_TCP_Recv (sock_server, &amp;data, 128);
  printf(&quot;%s\n&quot;,data);
  }
  printf(&quot;lol\n&quot;);

  }

  SDLNet_TCP_Close (sock_server);
  return 0;
}
</code></pre>
<pre><code class="language-cpp">#include &lt;SDL.h&gt;
#include &lt;SDL/SDL_net.h&gt;
#include &lt;stdlib.h&gt;

SDL_Surface *screen;
TCPsocket server;

void init_sdl (void)
{
	if (SDL_Init (SDL_INIT_VIDEO) &lt; 0)
		exit (-1);
	atexit (SDL_Quit);
}

void init_net (void)
{
	IPaddress addr;
	if (SDLNet_Init () &lt; 0)
	{
		printf (&quot;ERR Net: %s\n&quot;, SDLNet_GetError ());
		exit (-1);
	}
	if (SDLNet_ResolveHost (&amp;addr, NULL, 10000) &lt; 0)
	{
		printf (&quot;ERR ResolveHost: %s\n&quot;, SDLNet_GetError ());
		SDLNet_Quit ();
		exit (-1);
	}
	server = SDLNet_TCP_Open (&amp;addr);
	if (server == NULL)
	{
		printf (&quot;ERR TCP_Open: %s\n&quot;, 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 (&quot;Server-Socket ist offen\n&quot;);
	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 (&quot;Client wurde akzeptiert\n&quot;);
	/* Schicke erstes Rechteck */
	rect = (SDL_Rect *)malloc (sizeof (SDL_Rect));
	rect-&gt;x = 10;
	rect-&gt;y = 20;
	rect-&gt;w = 100;
	rect-&gt;h = 200;
	char data[128];

	while(true){

		//char g[12];
		//itoa(rand(),g,10);

			printf (&quot;%i&quot;,SDLNet_CheckSockets(set,0));
			//printf(&quot;lol&quot;);

		char d[33];

		sprintf(d,&quot;ping%i\n&quot;,rand());
		if (SDLNet_SocketReady (client))
            {

		maxread = SDLNet_TCP_Recv (client, &amp;data, 128);
		SDLNet_TCP_Send (client, d, 128);

		printf(&quot;%s\n&quot;,data);
		}

			printf(&quot;lol\n&quot;);

	}
	printf (&quot;I am Server. \n Read = %d\n&quot;, maxread);
	printf (&quot;x = %d, y = %d, w = %d, h = %d\n&quot;,
		rect-&gt;x, rect-&gt;y, rect-&gt;w, rect-&gt;h);
	SDLNet_TCP_Close (client);
	SDLNet_TCP_Close (server);
	return 0;

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2192464</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2192464</guid><dc:creator><![CDATA[alterbro]]></dc:creator><pubDate>Sat, 17 Mar 2012 22:03:36 GMT</pubDate></item></channel></rss>