Networking

Hi. (new-b warning)

Im pretty new to network programming.
Do someone know a easy start network tutorial…?
Since I’ve heard there is no tutorials for SDL_Net I have
to begin with another api.

someone??

/lazyman

Viktor Linder wrote:

Hi. (new-b warning)

Im pretty new to network programming.
Do someone know a easy start network tutorial…?
Since I’ve heard there is no tutorials for SDL_Net I have
to begin with another api.

someone??

/lazyman

I have put up API docs and some demo code, I am writing more demo code, and from that code I will base a tutorial hopefully.
meanwhile point your browser at: http://jonatkins.org/SDL_net--
-==-
Jon Atkins
http://jonatkins.org/

Hiya,
For a well written document on networking check out Beej’s Networking
Guide here…
http://www.bitafterbit.com/english/c/networking/index.html

Also, I wrote some sample code along time ago for a simple client/server
test.
It uses UDP packets, the server just loops till it receives 1 packet then
quits.
Its not the most in-depth examples, but it may give a little light on the
subject.

Good Luck,

Si.

------client code------------
#include <stdio.h>
#include “stdlib.h”
#include “SDL.h”
#include “SDL_net.h”

/* Static Vars */
static UDPsocket udpsock;
static SDLNet_SocketSet socketset;
static UDPpacket **packets;

int main(int argc, char argv[])
{
/
Vars */
IPaddress destinationIP;
Uint16 bindedchannel;

/* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
	fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
	exit(1);
}
atexit(SDL_Quit);

/* Initialize the network */
if ( SDLNet_Init() < 0 ) {
	exit(1);
}
atexit(SDLNet_Quit);

/* Allocate a vector of packets for client messages */
/* 4 packet of 255 length */
packets = SDLNet_AllocPacketV(4, 255);
if ( packets == NULL ) {
	exit(1);
}
/* Create the IP address */
SDLNet_ResolveHost(&destinationIP, "127.0.0.1", 5150);
if ( destinationIP.host == INADDR_NONE ) {
	exit(1);
} else {
	/* Open a listening UDPsocket - Any port(0) */
	udpsock = SDLNet_UDP_Open(0);
	if ( udpsock == NULL ) {
		exit(1);
	}
}
/* Allocate the socket set for polling the network - 1 set is needed. */
socketset = SDLNet_AllocSocketSet(1);  
if ( socketset == NULL ) {
	exit(1);
}

/* Bind the IP & port to the UDPsocket : Is this needed? */
bindedchannel = SDLNet_UDP_Bind(udpsock, -1, &destinationIP);

/* Add the updSocket to the socketset */
SDLNet_UDP_AddSocket(socketset, udpsock);

/* Copy the information into the UDP Buffer */
memcpy(packets[0]->data, "Hello", 5);
packets[0]->len = 5;

/* Actually send the data */
SDLNet_UDP_Send(udpsock, bindedchannel, packets[0]);

/* Close the network connections */
if ( udpsock != NULL ) {
	SDLNet_UDP_Close(udpsock);
	udpsock = NULL;
}
if ( socketset != NULL ) {
	SDLNet_FreeSocketSet(socketset);
	socketset = NULL;
}
if ( packets != NULL ) {
	SDLNet_FreePacketV(packets);
	packets = NULL;
}

return 0;

}

---------Server code--------
#include <stdio.h>
#include “stdlib.h”
#include “SDL.h”
#include “SDL_net.h”

/* Static Vars */
static UDPsocket udpsock;
static SDLNet_SocketSet socketset;
static UDPpacket **packets;

int main(int argc, char *argv[])
{
int PacketIn;
int SocketsReady;
int done;
SDL_Event event;

/* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
	fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
	exit(1);
}
atexit(SDL_Quit);

/* Initialize the network */
if ( SDLNet_Init() < 0 ) {
	exit(1);
}
atexit(SDLNet_Quit);

/* Allocate a vector of packets for receiving messages */
/* 4 of 255 length */
packets = SDLNet_AllocPacketV(4, 255);
if ( packets == NULL ) {
	exit(1);
}
/* Open a listening UDPsocket */
udpsock = SDLNet_UDP_Open(5150);
if ( udpsock == NULL ) {
	exit(1);
}

/* Allocate the socket set for polling the network - 1 set is needed. */
socketset = SDLNet_AllocSocketSet(1);  
if ( socketset == NULL ) {
	exit(1);
}

/* Add the updSocket to the socketset */
SDLNet_UDP_AddSocket(socketset, udpsock);

done = 0;
while (!done) {
/* Poll socket to see if socket is ready /
SDLNet_CheckSockets(socketset, 0);
if ( SDLNet_SocketReady(udpsock) ) {
/
Actually receive the data */
PacketIn = SDLNet_UDP_Recv(udpsock, packets); // Tell it to receive
only 1 - Recv. RevV - More than 1 and then PacketIn gives total read.
if (PacketIn != 0) {
printf(“PacketIn = %d\n”, PacketIn);
printf(“Channel = %d\n”, packets[0]->channel);
printf(“Packet received from : %d:%d\n”, packets[0]->address.port,
packets[0]->address.host);
printf(“Packet data length = %d\n”, packets[0]->len);
packets[PacketIn]->len = 0;
done = 1;
}
}
} /
End of main loop */

/* Close the network connections */
if ( udpsock != NULL ) {
	SDLNet_UDP_Close(udpsock);
	udpsock = NULL;
}
if ( socketset != NULL ) {
	SDLNet_FreeSocketSet(socketset);
	socketset = NULL;
}
if ( packets != NULL ) {
	SDLNet_FreePacketV(packets);
	packets = NULL;
}

return 0;

}

It’s great, I learned a lot, but in Section 3.1.7 (SDLNet_Read32()),
you wrote: “Get a 32bit (a short on 32bit systems)…” Should be
"Get a 32bit (a long on 32bit systems)…"On Sat, Jan 12, 2002 at 04:33:28AM -0500, Jonathan Atkins wrote:

I have put up API docs and some demo code, I am writing more demo code, and
from that code I will base a tutorial hopefully.
meanwhile point your browser at: http://jonatkins.org/SDL_net


People that can’t find something to live for always seem to find something to
die for. The problem is, they usually want the rest of us to die for it too.

Jakob Kosowski wrote:> On Sat, Jan 12, 2002 at 04:33:28AM -0500, Jonathan Atkins wrote:

I have put up API docs and some demo code, I am writing more demo code, and
from that code I will base a tutorial hopefully.
meanwhile point your browser at: http://jonatkins.org/SDL_net

It’s great, I learned a lot, but in Section 3.1.7 (SDLNet_Read32()),
you wrote: “Get a 32bit (a short on 32bit systems)…” Should be
"Get a 32bit (a long on 32bit systems)…"

thanks, cut and pasted those ReadWrite sections and missed a few :wink:
also the address has changed, it’s now located at http://jcatki.2y.net/SDL_net


-==-
Jon Atkins
http://jonatkins.org/