Packet receiving

BlitzMax Forums/BlitzMax Beginners Area/Packet receiving

AltanilConard(Posted 2007) [#1]
For my game I want a network code that sends and receives databanks. The client creates a databank, then uses PokeByte to insert a header, wich will define the packet type, into the databank, after that it inserts the rest of the packetdata using PokeInt and then sends it to the server.

The databanks I'm sending are 500 bytes. In my old (BlitzBasic) code, the server could use "If ReadAvail(Stream) => 500" to check if there were any packets on the stream to read, the problem is I don't know how to do this with BlitzMax.

Could anyone give me some guidelines, or an example of how to do this?

Thanks in advantage.


kronholm(Posted 2007) [#2]
Just use socketreadavail function.


AltanilConard(Posted 2007) [#3]
Thanks for the input, I looked into the command but don't really know how to use it.
I quickly made a small example to show you guys what I mean:

SERVER
'Server
Global Server:TSocket
Global Connection:TSocket
Global Stream:TSocketStream
Global ReceiveBank:TBank=CreateBank(500)

Server=CreateTCPSocket()
BindSocket(Server,7002)
SocketListen(Server)
Print "Server started."

While Not KeyHit(KEY_ESCAPE)
	Connection=SocketAccept(Server)
	
	If Connection
		Stream=CreateSocketStream(Server)
		Print "New connection."
		
		If SocketReadAvail(Server) => 400
			ReadBank(ReceiveBank,Stream,0,500)
			Local id:Byte=PeekByte(ReceiveBank,0)
			If id=7 Print "Received login packet."
		EndIf
	EndIf
Wend

CLIENT
'Client
Global Socket:TSocket
Global Stream:TSocketStream
Global SendBank:TBank=CreateBank(500)

Graphics 320,240

Socket=CreateTCPSocket()
If ConnectSocket(Socket,HostIp("localhost"),7002)
	Stream=CreateSocketStream(Socket)
	DrawText("Connected",0,0) ; Flip
Else
	RuntimeError("Could not connect to server.")
EndIf

PokeByte(SendBank,0,7)
WriteBank(SendBank,Stream,0,500)
Cls
DrawText("Login packet sent",0,0) ; Flip

WaitKey()
End


I know that code is wrong, but I hoped someone could show me what exactally is wrong about it, and maybe point me in the right direction.

Thanks


spacerat(Posted 2007) [#4]
Try doing SocketReadAvail(connection)... but it looks like you're doing it mostly correctly to me.


AltanilConard(Posted 2007) [#5]
That didn't work, the client does send the packet, and the server does know that a client connected, but for some reason it doesn't receive the packet, maybe I have to do something with 'While not Eof(Stream)...', but that makes my server crash.
Shouldn't I be checking if there is reading available on the Stream in stead of the Socket? (If that is possible).

The mistake could also be somewhere else in the code, but I can't seem to figure it out, some more help would be appreciated.

Conard.


kronholm(Posted 2007) [#6]
socketreadavail returns the size in bytes of what's on the socket. So try with 'if socketreadavail(server/socket) > 0' and then do a 'for local i=0 to socketreadavail(server/socket)' loop to read as many bytes as there are on the socket


AltanilConard(Posted 2007) [#7]
Ok, this is what I came up with:
'Server
Global Server:TSocket
Global Connection:TSocket
Global Stream:TSocketStream
Global ReceiveBank:TBank=CreateBank(500)
Global ByteArray:Byte[]

Server=CreateTCPSocket()
BindSocket(Server,7002)
SocketListen(Server)
Print "Server started."

While Not KeyHit(KEY_ESCAPE)
	Connection=SocketAccept(Server)
	
	If Connection
		Stream=CreateSocketStream(Server)
		Print "New connection."
		
		If SocketReadAvail(Connection) > 0
			Print "Reading available."
			Local ToRead:Int=SocketReadAvail(Connection)
			ByteArray=New Byte[ToRead]
			For Local i:Int = 0 To ToRead
				ByteArray[i]=ReadByte(Stream)
			Next
			If ByteArray[0]=7 Print "Received login packet."
		EndIf
	EndIf
Wend

The server never even prints 'Reading available', I also tried to make the client write some bytes to the server using 'WriteByte()' but that didn't change anything.


Filax(Posted 2007) [#8]
Did you find a solution ? because TCP with blitzmax is a little
bit obscur for me :/


Dreamora(Posted 2007) [#9]
BNetEx from Vertex should help out.

OO Version of Blitz3D like UDP and TCP commands.
UDP and TCP streams which are extended from the regular stream class.


Filax(Posted 2007) [#10]
Yes but i want send bank over internet? any idea?


Dreamora(Posted 2007) [#11]
And where is the problem there?


AltanilConard(Posted 2007) [#12]
I now use a simple lib rich d made (http://blitzmax.com/codearcs/codearcs.php?code=1974)
Here is a small test I made a while ago:
Client:


Server:


In both examples you will have to add rich d's code.
It uses databanks to send packets from client to server. If you press 'S' on the server the client will notify you it has received a packet, if you press 'S' on the client the server will print the first byte of the packet.


Filax(Posted 2007) [#13]
Many thanks for the examples :)