Socket Programing

BlitzMax Forums/BlitzMax Programming/Socket Programing

RexRhino(Posted 2005) [#1]
I am wondering how to create a TCP server in Blitzmax?

I noticed the TSocketStream class, but not sure how it works. It uses TStream, so I assume once the connections are set up it works like a stream, but I am not sure how to set up connections.

There is a function CreateClient(server, port), which seems self explainitory enough... except how do we know if the connection fails? How do we know when the connection is broken? Does the function block until it recieves the data it needs?

Also, for creating a server, there is Create(socket), but how does this work? Normally, you would have a seperate server socket object, and you would then call a function that would return a socket object if there was a connection request.

The source code is there, but without proper documentation it seems pretty difficult to do anything with sockets.


bradford6(Posted 2005) [#2]
I would just consider it WIP for now. I am sure there will be many more additions in the category of networking.


AntonyWells(Posted 2005) [#3]
I agree with rex's angst.. Would it have killed mark to comment these socket functions with their meaning at least? It's too confusing atm even trying to work out network stuff(On the surface at least, without docs..), which is a shame..(And that's an understatement. Considering my current game is primarly supposed to be network based..fat chance of that happening atm.)


jhague(Posted 2005) [#4]
I have created a TCP client in BM, but not a server. You can use TSocketStream (there's an httpstream module which uses TSocketStream, and I used it as an example). I found TSocketStream to be a bit ugly, so I pulled out the lower-level calls from it and just use those. It's still only a couple of lines to set-up a socket.

I think you can use the lower-level calls to set-up a server, but I haven't tried it.


Tibit(Posted 2005) [#5]
I wish Bmax will support Networking similar to B3d or better. We have threads now so if anyone manages to do sever-client with low level commands it would be fun to test =)

Junkster would you like to post your TCP client code? And the server, did you happen to do it in B3D/Bplus?


bradford6(Posted 2005) [#6]
one way around this would be for someone to write a Python Wrapper and then we can use Twisted Matrix.
http://www.python.org
http://twistedmatrix.com/

Twisted currently supports the following protocols, all implemented in pure Python, most of them as both servers and clients:

Jabber
SIP
FTP
SSH, SFTP
IMAP
HTTP
NNTP
SOCKSv4 (server only)
SMTP
IRC
telnet
POP3
AOL's instant messaging TOC
OSCAR, used by AIM as well as ICQ (client only)
DNS
MouseMan
finger
Echo, discard, chargen and friends
Twisted Perspective Broker


Why reinvent the Wheel?

(that said,
there will still be a need for peer-to-peer networking code built in to BMAX)


Dirk Krause(Posted 2005) [#7]
A python wrapper would be a _very_ nice thing.


ImaginaryHuman(Posted 2005) [#8]
I'm looking forward to seeing some nice networking functionality built into BlitzMax. I definitely need to consider how to make my software designed to work well and take advantage of a network. So far all I've been able to do is read various articles here and there about general network/internet programming, TCP/UDP, lag, cheating in net-games and so on.... all okay on the theory but it'd be useful to have the code to put behind it now so that I can structure my app appropriately internally.


Jeroen(Posted 2005) [#9]
Bradford6, GOOD idea.


Kanati(Posted 2005) [#10]
lack of networking is the only reason I don't use ibp very much... I too was pretty disappointed to see that only the basest of networking calls were included with bmax at launch.


jhague(Posted 2005) [#11]
Here's a very thin layer over the low-level socket functions for writing TCP clients:
Function Connect(server$, port)
	Local addr_type, addr_len
	Local addrs:Byte Ptr Ptr = gethostbyname_(server, addr_type, addr_len)
	If addrs = Byte Ptr Ptr(0) Return 
	If addr_type <> AF_INET_ Return -1
	
	Local sock = socket_(addr_type, SOCK_STREAM_, 0)
	If sock < 0 Return sock
	
	If connect_(sock,addrs[0], addr_type, addr_len, port) < 0
		closesocket_ sock
		Return -1
	EndIf

	Return sock
End Function

Function ReadSocket(socket, buf:Byte Ptr, count)
	Return recv_(socket, buf, count, 0) 
End Function

Function WriteSocket(socket, buf:Byte Ptr, count)
	Return send_(socket, buf, count, 0)
End Function

Function CloseSocket(socket)
	If socket closesocket_(socket)
End Function
You open a socket like this:
Global Socket = Connect("localhost", Port)

If Socket < 0 Then
	Print "Didn't work."
	CloseSocket(Socket)
	End
EndIf
Once the socket is open, you read from it with ReadSocket. That function waits until either data is received or the socket is closed.

You can also use TSocketStream and do this in essentially one line, but I like seeing what's going on behind the scenes in this case.

Hope this helps!


Jeremy Alessi(Posted 2005) [#12]

We have threads now so if anyone manages to do sever-client with low level commands it would be fun to test =)



Huh?

Oh I see ... Ant added a thread module ;) Good stuff!


Dirk Krause(Posted 2005) [#13]
TsocketStream example, runs fine with the Twisted simpleserver.
Strict

Local stream:TStream=TSocketStream.CreateClient( "localhost",8000 )

stream.WriteLine "This is a test~n"

local buffer:String
While Not Eof( stream )
  buffer = stream.ReadLine()
  Print buffer
  If Not buffer Exit
Wend


CloseStream stream



Vertex(Posted 2005) [#14]
Hi!
http://blitzbasic.com/Community/posts.php?topic=43533
Has the same functionality as BlitzClassic with all udp and tcp commands.
cu olli


QuietBloke(Posted 2005) [#15]
I have been looking into trying my hand at something like this. I just want to write a simple chat server. Ive seen loads of examples written in C but Im having trouble writing any sort of code in BMax to do it.
In some other thread about non-blocking accepts it was mentioned to use Select_ with zero timeout. But looking at that it expects 4 params 3 of which are pointers to sets of sockets. I cant for the life of me get my head round how I can create these socket sets to use it.
So... has anyone got an example(s) they wouldnt mind sharing or... is there another way... or... will BMax implement something simpler in the near future that I can wait for.


RGR(Posted 2005) [#16]
@Vertex
please if you post a link can you use www.blitzbasic.com instead of blitzbasic.com - don't know if its only here but I get an "Internal Error" Message
If I copy and paste and put www. in front then IE reacts fine


Beaker(Posted 2005) [#17]
http://www.blitzbasic.com/Community/posts.php?topic=43533


RGR(Posted 2005) [#18]
Thanks Beaker ... although I really don't know why you did that ... as you could read, I followed that link with Copy and Paste in a new IE-Window anyway.

The reason why I asked Vertex to use www. is: he is the only one who doesn't use www. and ALL his Links I have seen on other pages I could not follow with one click ...
Maybe others don't get that error with their browsers?
But if - then it is not useful to place links you cannot follow.

So, Beaker, what was your intention? Are you adding links that work to all of his Posts now? Was it the typical "Help" as I often see in these forums? You had to react, right? And feel so satisfied now?


QuietBloke(Posted 2005) [#19]
My mistake.. I tried the link supplied but I just got an internal error and I assumed the post had been deleted or something. I even put the www prefix in and it still returned an internal error but when I changed the blitzbasic to blitzmax I can now see the thread. <shrug>.
Anyway.. Now things are looking much clearer. Thanx Vertex. I havent had time to look over your NetWork library in detail yet and Im a complete Newbie when it comes to this stuff but it looks like what you have done will make my life a LOT easier.. I might even understand it now :)


Sarge(Posted 2005) [#20]
Look in pub.Stdc in stcd.c you will see all the connect, listen, accept, bind ect