Socket Programming

BlitzMax Forums/BlitzMax Programming/Socket Programming

Banshee(Posted 2007) [#1]
I've been trying to get my head around Blitz Max's socket programming of late, and i'm getting very confused with it. I have a very procedural approach to programming, I dont much hold with OO and because of that i'm struggling with the stuff in the code archives.

I'm very good with Blitz3D's networking, I can use either UDP or TCP, assemble my streams byte by byte, close connections, send / receive messages - i've written everything from a multi-threaded web server to games to server management tools to game mods and even once a multi-player game 'AI' that connected to multiplayer LAN games.

But BlitzMax... *boggle*

I've hunted for tutorials, experimented, and read through the code archives and i'm still stuck! I can't seem to fathom out the basic concept of sockets.

As far as I can figure out I create a socket, and then I bind the socket to a port (which begs questions about why I need to create the socket in the first place, but i'm sure it's logical to somebody more knowledgeable than me).

On the server I then repeatedly check socketListen on the socket to see if data has arisen, or should this be the bound socket?

Once a connection is detected it returns a new socket for that connection, so I bind that socket to a stream with createSocketStream, and I then read off that data with readShort/Int/Line etc, and check for more available content with SocketReadAvail() I think, although maybe I need a stream command for that.

On the client side I create a socket, bind it to the send port, then I connect it to the remoteIP and the receive port at the other end, which creates a new socket.

I then bind a stream to the new socket and write data to it with writebyte/int/line - but how do I send this stream?

I'm so confused by all of this, but I want to learn. I really dont want to use networking libraries because I want to master it for myself, but i'm so confused !

Is there a tutorial on network coding in BlitzMax anywhere?

Help please. I'm too stupid to figure this out.


Dreamora(Posted 2007) [#2]
Networking with BM means GNet ^^

If you are good in B3D networking thought, I would look at Vertex BNetEx (as well to get your head around the socket stuff if you want to do low level programming)


tonyg(Posted 2007) [#3]
CopyStream?
I struggled with the same thing and came up with this as very simple send/receive.


Dirk Krause(Posted 2007) [#4]
Also there is a tutorial here:
http://www.blitzmax.com/codearcs/codearcs.php?code=1555#comments


SculptureOfSoul(Posted 2007) [#5]
Don't bind your client side sockets. It's bad practice. The OS will handle assigning the socket when it's connected, and this will guarantee that it's on a free port whereas if you bind it you could very well run into conflicts with other running programs.


Banshee(Posted 2007) [#6]
Thank you for all the help, it seems the command I was missing, or rather, misunderstood, was flushStream - which sends the data queued rather than trashes it. I was being foiled by native English!


Banshee(Posted 2007) [#7]
I'm not quite there yet though, I can't seem to figure out how to double check if there is data available for reading. Using both SocketReadAvail() and the .size() method on the stream both return 0, is there not a way to double check a network stream has any content before reading it?

Thank you.


SculptureOfSoul(Posted 2007) [#8]
I didn't use Socketsreams in my program so I'm not sure where they diverge from standard raw sockets. SocketReadAvail() does work with raw sockets, but my guess is that the socketstream is doing something like reading all of the information available on the socket into a stream buffer - so hence SocketReadAvail() finds nothing sitting on the standard socket buffer since the stream has already read it.

I'm assuming you'll have to use stream commands on the socketstream to test if it's empty. I'd try calling EOF() on the stream and see if that works...i.e if it returns false there *should* be data sitting there for you.

If that doesn't work, you'll have to experiment with some other stream functions (checking if streampos = streamsize, check if readbyte throws an exception, etc.)


SculptureOfSoul(Posted 2007) [#9]
Whoops, I missed that you said you were checking .size() on the stream. Hmm. Actually, I was wrong about the notion that a socketstream might automatically read the data. After taking a peek at the source, it looks like it should be sitting on the socket.

So calling MySocketStream.Socket().ReadAvail()
always returns 0? Are you sure data is being sent to it? I know that's a silly question but y'never know.

Also ignore my Eof() surmisings. All it does in the case of a socketstream is let you know if it's connected.

SocketReadAvail(MySocketStream.Socket()) or the more OOP version of the code I posted above *should* be returning a non 0 value when there is data to be read.


skn3(Posted 2007) [#10]
I have created, or am creating a tcpserver and tcpclient layer for raw sockets. Shame you cant get yer hands on it cos its for an inhouse thing at work lol (making a peer to peer network for a remote management tool). I might post a modified version once it is finished though. If you have any questions in general id be happy to help.


Banshee(Posted 2007) [#11]
Thank you so much for the various help, particularly SculptureOfSoul as that has really helped me get a grip on it and I now have a multi-client server model running and i'm assembling my streams byte by byte just how I wanted. I think previously I was checking the readavail() of the wrong socket, but now it is all working and i'm very grateful to all of you :).

*hugs*

(it's ok, i'm a girl, hugging is not gay)


SculptureOfSoul(Posted 2007) [#12]
Glad that my musings were of some help. Network programming can be quite tricky (I know I've got a lot to learn yet!)