Networking issue

hey guys,

here’s the setting: i’m tryin’ to write some netcode for a really simple
server<->client application with sdl_net.
unfortunately i get loads of strange segfaults with my code (not used to c++
and it’s pointer-handling).
i think all i need is some really simple example code for this situation:
i got a working udpsocket in game.udpsocket and a channel in game.channel
(which is already bound to the correct address). now i want to send a packet,
here’s my code:

UDPpacket *packet = SDLNet_AllocPacket( 20 );

Uint8 tmp;

tmp = atoi( game.me.name );

packet->data = &tmp;
packet->len = sizeof( game.me.name );

SDLNet_UDP_Send( game.udpsock, game.channel, packet );

-> nothing happens at all :confused: (packet->data is empty for some reason)

yea well, i love it when these thing for which you plan like 15 minutes take
the whole day :slight_smile:

take care …–
Jonas Huckestein

Pub. PGP Key: http://pgp.upb.de:11371/pks/lookup?op=get&search=0xD2BAE645

"There was once a cross-eyed teacher who couldn’t control his pupils."
Another one: “I was wondering why the baseball kept getting bigger. Then it
hit me.” and “A bicycle can’t stand on its own. It’s two-tired.”
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20050312/8a4077d7/attachment.pgp

hey guys,

here’s the setting: i’m tryin’ to write some netcode for a really simple
server<->client application with sdl_net.
unfortunately i get loads of strange segfaults with my code (not used to c++
and it’s pointer-handling).
i think all i need is some really simple example code for this situation:
i got a working udpsocket in game.udpsocket and a channel in game.channel
(which is already bound to the correct address). now i want to send a packet,
here’s my code:

UDPpacket *packet = SDLNet_AllocPacket( 20 );

Uint8 tmp;

tmp = atoi( game.me.name );

packet->data = &tmp;
packet->len = sizeof( game.me.name );

SDLNet_UDP_Send( game.udpsock, game.channel, packet );

-> nothing happens at all :confused: (packet->data is empty for some reason)

yea well, i love it when these thing for which you plan like 15 minutes take
the whole day :slight_smile:

Networking is like that, yeah it is… take a look at net2 at
http://gameprogrammer.com/programming.html

	Bob PendletonOn Sat, 2005-03-12 at 21:17 +0100, Jonas Huckestein wrote:

take care …


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

is there a simple sample somewwhere to send an arbitrary piece f data
between 2 known ip addresses?

I have a simple text based RPC protocol that simply sends strings back
and forth.
It is very simple, including the TCP/IP code.

You can get it like this:

  1. cvs -d:pserver:anonymous at cvs.sf.net:/cvsroot/new-rpc login
    (password: just hit enter)
  2. cvs -d:pserver:anonymous at cvs.sf.net:/cvsroot/new-rpc checkout new-rpc
  3. cd new-rpc
  4. make

ChrisOn Mon, 14 Mar 2005 20:05:32 +0000, Brian Barrett <brian.ripoff at gmail.com> wrote:

is there a simple sample somewwhere to send an arbitrary piece of data
between 2 known ip addresses?


Chris Nystrom
http://www.newio.org/~ccn
AIM: nystromchris

UDPpacket *packet = SDLNet_AllocPacket( 20 );
Uint8 tmp;

tmp = atoi( game.me.name );
packet->data = &tmp;
packet->len = sizeof( game.me.name );

erm - you are pointing to a char (tmp) bot say it’s length is sizeof
game.me.name … that does sound like an error.
len should be 1 (if it’s in byte):

tmp = (char)atoi(game.me.name);
packet->data = &tmp;
packet->len = 1;

SDLNet_UDP_Send( game.udpsock, game.channel, packet );
-> nothing happens at all :confused: (packet->data is empty for some reason)

There are some tools around where you can have a look at your
outgoing/incoming network packets (afair) - but I’ve never used one of

  • so - maybe someone knows one?

I’ve written my first SDL_Net client yesterday (TCP connection to a
java server) and it works really well. If you like I can send you my
code.

BR Arne

could you post your code, if you don’t mind. i know about tcp/ip and
udp, but not really how to use them. a practical example could be
useful…

could you post your code, if you don’t mind. i know about tcp/ip and
udp, but not really how to use them. a practical example could be
useful…

Well ok - It’s not exactly the code I actually use because for that I
would have to put a whole lot more classes here, so I stripped it down
to the essentials:

C++ Code first:

IPaddress m_serverAddress;
TCPsocket m_socket;

SDLNet_Init(); // somewhere to put …

//------------------------------------------------------------------------

bool connect() {

/*
	Well - ok that three lines are to get the servers name and port which  
  • in my case
    come from an XML-config file. Just change your values for serverName
    and serverPort
    */
    CConfigManager *conf = CConfigManager::getInstance();
    string serverName = conf->getString(“network”, “server”, “dns”);
    short serverPort = (short)conf->getInt(“network”, “server”, “port”);

    // This here gives you the IP-Adress of the server named in serverName
    if(SDLNet_ResolveHost(&m_serverAddress, serverName.c_str(),
    serverPort) == -1) {
    cout << “Could not resolve host (” << serverName << “).” << endl <<
    "SDL says: " << SDLNet_GetError() << endl;
    return false;
    }

    // This opens the connection
    if (!(m_socket = SDLNet_TCP_Open(&m_serverAddress))) {
    cout << "Could not open connection to " << serverName << “:” <<
    serverPort << “.” << endl << "SDL says: " << SDLNet_GetError() << endl;
    return false;
    }

    cout << "Connected to " << serverName << “:” << serverPort << “.” <<
    endl;
    return true;
    }

//


void disconnect() {
// Plain and simple - close your socket
SDLNet_TCP_Close(m_socket);
cout << “Disconnected.” << endl;
}

//


bool sendString(string &message) {
// Now let’s send a text over this connection
int send = SDLNet_TCP_Send(m_socket, message.c_str(),
message.length());
/*
My code looks a lot different here but this is the key function:
I’m not sure if length() exists (still looking for a good reference)
but it should be clear what I mean =)
*/

return (send<payloadlen);

}

I haven’t yet implemented a recieve Function because we haven’t yet
defined a protocol, but that should be straight forward reading from a
"stream".
Now to the Java-Server - that’s a bit easier

server.java:

import java.net.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Vector;

public class server implements Runnable {

public server(int _port) {
	try {
		m_serverSocket = new ServerSocket(_port); // Open a Socket
		m_running = false;
	}
	catch (Exception e) {
		System.out.println("Could not open serversocket for  

port"+String.valueOf(_port)+".\nEvent says: "+e.getMessage());
}
}

public void run() {
	try {
		System.out.println("Waiting for connection ...");
		// Nur eine Connection erlaubt

		Socket clientConnection = m_serverSocket.accept(); // This blocks  

the function (thread in this case) until someone connected
System.out.println(“Huston we’ve got contact …”);

		// now we have to get a couple of streams because we want to read  

from our connection
OutputStream snd_stream = clientConnection.getOutputStream();
InputStream rcv_stream = clientConnection.getInputStream();

		m_running = true;
		
		String message;
		// some converting
		BufferedReader rcv = new BufferedReader(new  

InputStreamReader(rcv_stream));

		System.out.println("Ready for action.");
		// Main loop
		while (m_running) {
			message = rcv.readLine(); // now that's easy, read until \n
			if (message.equals("quit"))
				stop();
			else
				System.out.println(message);
		}
		
		System.out.println("Cleaning up...");
		
		// close the connection
		clientConnection.close();
		m_serverSocket.close();
		System.out.println("Bye");
	}
	catch (Exception e) {
		System.out.println("Ooups ...\n"+e.getMessage());
	}
}

// to stop our thread
public void stop() {
	m_running = false;
}


private ServerSocket m_serverSocket;
private boolean m_running;

}

//


java_server_dummy.java:

public class java_server_dummy {

 public static void main (String args[]) {
	System.out.println("Here we go ...");
	server s1 = new server(1234);
	Thread s1Thread = new Thread(s1);
	s1Thread.start();
}

}

What all that stuff does - well it’s simply a “client sends text to
server” app. There is no feedback from the server, but that should be
easy to implement.
Most of the C++ code is copied from the SDL_net docs and slightly
modified.
I hope that helps. Otherwise have a look at the SDL_net docs and/or
look up sockets on google.

BR Arne